AVAudioPlayer.play() does not play sound

Why doesn’t the following code play a sound? It returns “true” for play(), but I cannot hear anything.

    let path = "/Users/account/Music/sound.mp3";
    let fileURL = NSURL(fileURLWithPath: path)
    var Player = AVAudioPlayer(contentsOfURL:fileURL, error:nil);

    Player.delegate = self;
    Player.prepareToPlay();
    Player.volume = 1.0;
    var res = Player.play();
    println(res);

If I use the following code instead, I can hear the sound.

  • Override ringer volume in iPhone apps
  • Check play state of AVPlayer
  • Swift Error : Use of module 'CMSampleBuffer' as a type
  • How to apply filter to Video real-time using Swift
  • Record Internal Sound iPhone SDK
  • how to find Bluetooth audio devices in iOS
  •     var inFileURL:CFURL = fileURL!;
        var mySound = UnsafeMutablePointer<SystemSoundID>.alloc(sizeof(SystemSoundID));
        AudioServicesCreateSystemSoundID(inFileURL, mySound);
        AudioServicesPlaySystemSound(mySound.memory)
    
    • OS X Yosemite 10.10.3
    • Xcode 6.2

    Solutions Collect From Internet About “AVAudioPlayer.play() does not play sound”

    The problem is that Player, your AVAudioPlayer, is a local variable. So it goes out of existence immediately – before it can even start playing, let alone finish playing.

    Solution: make it a property instead, so that it will persist.