AVAudioPlayer does not play sound

I am working on an iOS application that should play some sounds using the AVFoundation framework. The workspace structure in Xcode 4 contains two projects:

  • Workspace
    • The application itself (main project)
    • Utility library

After creating the utility library, it creates a static library, which is used as a framework in the main application.

Thus, when you try to play sound inside the main application using the code below, it works as expected.

 NSString *resourcePath = [[NSBundle mainBundle] resourcePath]; NSString *path = [NSString stringWithFormat:@"%@/sound.mp3", resourcePath]; NSURL *url = [NSURL fileURLWithPath:path]; NSError *error = nil; AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error]; [audioPlayer play]; 

On the contrary, when you try to play exactly the same sound (or any other) inside the utility library using the same code as above, the sound does not play at all, even if the error is zero, and the values โ€‹โ€‹of the audio player properties are correct (number of channels, duration) .

I made sure that the AVFoundation is in both projects.

In addition, my class uses the AVAudioPlayerDelegate protocol and implements these two methods:

 - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag; - (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error; 

None of these methods are called after trying to play sound.

If I use the AudioToolbox structure instead, then it plays the sound. But I am interested in using AVFoundation for several reasons.

Any idea what is going on? Am I missing something about AVFoundation ? Could this be due to using AVAudioPlayer from inside the static library?

Thanks in advance.

+51
objective-c xcode4 automatic-ref-counting avfoundation audio
Nov 04 2018-11-11T00:
source share
9 answers

I found a solution, and this is due to something that I did not mention and did not think about: I compile using Automatic Reference Counting (ARC).

ARC inserts a release call to the audio player, so it is released immediately after exiting the method in which it is created. It has nothing to do with AVAudioPlayer , but with ARC.

To solve this problem, I just created the AVAudioPlayer attribute for the class that deals with sounds so that it is no longer released by ARC. Well, at least it wasn't released until an instance of this class was released.

For more information on ARC, see Automatic Link Counting: Zeroing Weak Links with details on why I had this problem.

+136
Dec 07 2018-11-12T00:
source share
โ€” -

Yes, absolutely; ARC was the cause of my code.

I introduced the property:

 @property (strong, nonatomic) AVAudioPlayer *audioPlayer; 

and it did all right.

+32
Apr 18 2018-12-12T00:
source share

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]; 
+28
Mar 07 '14 at 7:19
source share

Have you configured 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 this helps.

+15
Dec 05 '11 at 11:30
source share

Even with the audioPlayer var class declared in the class, it will not play. At least on the physical device iOS 10 iPhone 5c.

play() true , so there are no errors.

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

 let session = AVAudioSession.sharedInstance() try! session.setCategory(AVAudioSessionCategoryPlayback) 
+1
Nov 05 '17 at 20:52
source share

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

0
Jan 23 '14 at 17:25
source share

Fast decision:

  import AVFoundation // define your player in class var player = AVAudioPlayer() override func viewDidLoad() { super.viewDidLoad() } 
0
Feb 23 '16 at 16:12
source share

I had the same problem. I solved this by adding the line below in the .h file:

 @property (strong, nonatomic) AVAudioPlayer *audioPlayer; 

And in the .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]; 
0
Jun 29 '16 at 10:03
source share

Please stay on the page for some time, I encountered the same problem. and use sleep to solve this problem.




 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); 



-one
Oct. 16 '14 at 5:50
source share