AVAudioPlayer: MP3 file does not play on the device, but works in Simulator (iOS 6)

I am using the code below to play an MP3 file from AVAudioPlayer on iOS 6. It seems that the file is playing, but there is no sound on the device. However, it works great in iOS Simulator.

.H file:

#import "UIKit/UIKit.h" #import "AVFoundation/AVFoundation.h" @interface ViewController : UIViewController <AVAudioPlayerDelegate> @property (strong, nonatomic) AVAudioPlayer *player; @end 

.M file:

 #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; player =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:soundFilePath] error:&error]; if (error) { NSLog(@"Error in audioPlayer: %@",[error localizedDescription]); } else { [player setDelegate:self]; [player setNumberOfLoops:3]; //just to make sure it is playing my file several times player.volume = 1.0f; if([player prepareToPlay]) //It is always ready to play NSLog(@"It is ready to play"); else NSLog(@"It is NOT ready to play "); if([player play]) //It is always playing NSLog(@"It should be playing"); else NSLog(@"An error happened"); } } @end 
+4
source share
3 answers

The code you posted is great for me.

  • Are you sure you included beep_7.mp3 in your target?
  • Are you sure the file name is correct? iOS uses case-sensitive file system!
  • Are you sure your device is not disconnected?
  • Are you sure the file is a valid working MP3 file?
+5
source

Check if your device’s silent mode is on or off? I once had the same problem as you, after I turned off the wide screen mode, everything was solved!

+2
source

Have you set AudioSessionCategory?

 { NSError *setCategoryError = nil; BOOL success = [[AVAudioSession sharedInstance] setCategory: avaudiosessioncategoryplayandrecorderror: &setCategoryError]; } 
0
source

All Articles