Note that there are some explanatory texts on larger screens.

plurals
  1. POAVAudioPlayer plays in iOS Simulator but not on the iPhone
    text
    copied!<p>I'm building an app which has to play a mp3 file. For that issue, I'm using AVAudioPlayer following the indications I have been reading from others post. But my problem is, the code (I have attached bellow) works on the iOS Simulator, but when I try to use it in my iPhone, it doesn't work... I hope any of you could help me with this issue.</p> <p><strong>File .h:</strong></p> <pre><code>#import "UIKit/UIKit.h" #import "AVFoundation/AVFoundation.h" @interface ViewController : UIViewController &lt;AVAudioPlayerDelegate&gt; @property (strong, nonatomic) AVAudioPlayer *player; @end </code></pre> <p><strong>File .m:</strong></p> <pre><code>#import "ViewController.h" @implementation ViewController @synthesize player; - (void)viewDidLoad { [super viewDidLoad]; NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"beep_7" ofType:@"mp3"]; NSLog(@"Audio path: %@", soundFilePath); NSError *error; self.player =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:soundFilePath] error:&amp;error]; if (error) { NSLog(@"Error in audioPlayer: %@",[error localizedDescription]); } else { [self.player setDelegate:self]; [self.player setNumberOfLoops:3]; //just to make sure it is playing my file several times self.player.volume = 1.0f; if([self.player prepareToPlay]) //It is always ready to play NSLog(@"It is ready to play"); else NSLog(@"It is NOT ready to play "); if([self.player play]) //It is always playing NSLog(@"It should be playing"); else NSLog(@"An error happened"); } } -(void)audioPlayerDecodeErrorDidOccur: (AVAudioPlayer *)player error:(NSError *)error { NSLog(@"audioPlayerDecodeErrorDidOccur -&gt; Error in audioPlayer: %@",[error localizedDescription]); } -(void)audioPlayerDidFinishPlaying: (AVAudioPlayer *)player successfully:(BOOL)flag{ NSLog(@"audioPlayerDidFinishPlaying"); } -(void)audioPlayerBeginInterruption:(AVAudioPlayer *)player{ NSLog(@"audioPlayerBeginInterruption"); } -(void)audioPlayerEndInterruption:(AVAudioPlayer *)player{ NSLog(@"audioPlayerEndInterruption"); } @end </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload