AVAudioPlayer not playing any sound

泄露秘密 提交于 2019-11-27 00:12:44

I found the solution and it's related to something I didn't mention and didn't think about: I'm compiling with Automatic Reference Counting (ARC).

ARC inserts a release call to the audio player, so it's deallocated right after leaving the method where it is created. It has nothing to do with AVAudioPlayer but with ARC.

In order to solve this issue, I just created an AVAudioPlayer attribute to the class that deals with sounds so that it is not released anymore by ARC. Well, at least, it's not released until the instance of this class is released.

For more information about ARC, refer to Automatic Reference Counting: Zeroing Weak References with details on why I had this issue.

Yes, absolutely; ARC was the reason with my code as well.

I introduced a property:

@property (strong, nonatomic) AVAudioPlayer *audioPlayer;

and that made everything work out fine.

use this it will work definitely....

AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayback error:nil];

NSURL *url = [NSURL fileURLWithPath:@"path of the sound file :)"];
_player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[_player setDelegate:self];
[_player prepareToPlay];
[_player play];

Have you configured the AVAudioSession?

I had a similar problem and fixed it using something like this:

AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:NULL];

or

[audioSession setCategory:AVAudioSessionCategoryPlayback error:NULL];

Hope it helps.

Even having audioPlayer var declared in the class scope, it won't play. At least on iOS 10 iPhone 5c physical device.

play() result is true so no errors appear to happen.

Had to add this (Swift 3, 4) and now it plays the sound correctly.

let session = AVAudioSession.sharedInstance()
try! session.setCategory(AVAudioSessionCategoryPlayback)

I have created open source project for simple audio player. Take a look at it if you have any problems with playing audio in iOS (in background too) : https://github.com/wzbozon/DKAudioPlayer

Please stay on the page for some time, I was facing same issue. and use sleep to resolve it.


NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                             pathForResource:@"sounds-1027-are-you-kidding"
                                             ofType:@"mp3"]];
        AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc]
                                      initWithContentsOfURL:url
                                      error:nil];
        [audioPlayer play];
        sleep(2);

I had the same issue. I had resolved it by adding below line in .h file:

@property (strong, nonatomic) AVAudioPlayer *audioPlayer;

And in .m file :

NSError *error;
NSString *soundFilePath = [NSString stringWithFormat:@"%@/dancepechance.mp3",
                               [[NSBundle mainBundle] resourcePath]];
    NSURL *url = [NSURL fileURLWithPath:soundFilePath];
    self.audioPlayer = [[AVAudioPlayer alloc]
                                  initWithContentsOfURL:url
                                  error:&error];
    NSLog(@"Error %@",error);
    [self.audioPlayer prepareToPlay];
    [self.audioPlayer play];

You can use AVPlayerViewController from AVKit instead for both audio and video. Code is available for both swift and objective c here.

Swift Solution:

  import AVFoundation

  // define your player in class
  var player = AVAudioPlayer()

  override func viewDidLoad() {
    super.viewDidLoad() 
  }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!