Sound does not play with AVAudioPlayer

I searched, and I believe that my problem is completely unique. I know about Simulator 5.1 error when using AVAudioPlayer, which is not my problem. I am working on an iOS 5.1 device.

Here is my header file:

#import <UIKit/UIKit.h> #import <Foundation/Foundation.h> #import <AVFoundation/AVAudioPlayer.h> -(IBAction)pushBell; @end 

and my implementation file:

 #import "BellViewController.h" @interface BellViewController () @end @implementation BellViewController -(IBAction)pushBell { NSString *soundPath =[[NSBundle mainBundle] pathForResource:@"bell1" ofType:@"caf"]; NSURL *soundURL = [NSURL fileURLWithPath:soundPath]; NSError *error; AVAudioPlayer *theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:&error]; [theAudio setDelegate:self]; [theAudio setNumberOfLoops:0]; [theAudio play]; } @end 

And I have a button in my .xib that plays the file.

So that I refer to my audio file correctly, I intentionally print the wrong file name and really get an exception. To make sure the file is playing, I sent it to myself and played it on the device.

When I press the button, I don’t hear anything. There are no errors. I do not know what is wrong.

Additional Information iPhone 4 with iOS 5.1 Xcode 4.3.2 Audio is about 700 Kbps, converted to .caf format using afconvert from a large .wav file.

+8
ios iphone audio avaudioplayer core-audio
source share
6 answers

solvable.

1.- Check if your file has been added to “Copy package resources” in “Build phases”. Copy bundle resources

2.- ViewController.m

 #import <AVFoundation/AVAudioPlayer.h> @interface ViewController () @property (nonatomic, strong) AVAudioPlayer *theAudio; - (IBAction)pushBell; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; NSString *soundPath =[[NSBundle mainBundle] pathForResource:@"beep-beep" ofType:@"caf"]; NSURL *soundURL = [NSURL fileURLWithPath:soundPath]; NSError *error = nil; self.theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:&error]; } - (IBAction)pushBell { [self.theAudio play]; } 

3.- Connect IBAction to your button in Interface Builder.

Done.

+20
source share

That was my problem, so I just post it here if it helps someone!

Believe it or not, it seems that there is an error (in iOS 5.1 on iPad 2, at least), where if you have the “Disabled” side switch installed and it is turned off, set the side switch to block rotation, AVAudioPlayer does NOT PLAY, except through the headphones!

So, I had to go to the Settings app, switch the switch to “Mute”, then turn on the iPod, switch it back to “Block turn”, and my application started working correctly.

+18
source share

Your theAudio AVAudioPlayer freed before it even starts playing (since it stands out as a local variable inside the pushBell method.

Change your code to:

header file:

 #import <UIKit/UIKit.h> #import <Foundation/Foundation.h> #import <AVFoundation/AVAudioPlayer.h> @interface BellViewController:UIViewController @property (nonatomic, strong) AVAudioPlayer *theAudio; -(IBAction)pushBell; @end 

implementation file:

 #import "BellViewController.h" @implementation BellViewController @synthesize theAudio = _theAudio; - (void)viewDidLoad { if (!theAudio) { NSString *soundPath =[[NSBundle mainBundle] pathForResource:@"bell1" ofType:@"caf"]; NSURL *soundURL = [NSURL fileURLWithPath:soundPath]; NSError *error; _theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:&error]; [self.theAudio setDelegate:self]; [self.theAudio setNumberOfLoops:0]; [self.theAudio prepareToPlay]; } } -(IBAction)pushBell { [self.theAudio play]; } @end 

I assume BellController is a UIViewController , if it's a different type of class, just modify the code accordingly.

+14
source share

I think this is a memory problem, you should create an instance of AVAudioPlayer *theAudio as a global class object and initialize it in the init method. Then just run the [theAudio play]; method [theAudio play]; in the method -(IBAction)pushBell .

I answered a similar problem related to playing system sound (actually this is what you are trying to do), check that my answer is below:

Unable to play system sound after switching to iOS 5

By the way, consider playing a bell as a system sound, AVAudioPlayer more suitable for playing sound tracks rather than bells and effects.

I hope this helps.

+4
source share

I agree with hooleyhoop. Try

 NSLog(@"%@", error.userInfo) 

to find out what comes out.

Another alternative would be to set the player object in viewDidLoad or initWithNib and save it as properties on your object. That way you can use something like [self.theAudio prepareToPlay];

To avoid downloading sound every time you press a button, this can save some time if it is high-quality audio. Then just call [self.theAudio play]; into your IBAction method.

Sorry if this answer is a bit messy - typing on the iPad, you cannot double-check it correctly.

+2
source share

You need to give it some time to prepare the pronunciation, as it is probably buffered from the URL.

The code:

 [theAudio prepareToPlay]; [self performSelector:@selector(playMusic) withObject:nil afterDelay:3.0]; 
+1
source share

All Articles