AVAudioPlayer working in Simulator, but not on device
Posted
by cannyboy
on Stack Overflow
See other posts from Stack Overflow
or by cannyboy
Published on 2010-06-15T17:26:41Z
Indexed on
2010/06/15
19:52 UTC
Read the original article
Hit count: 409
My mp3 playing code is:
NSError *error;
soundObject = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:audioPathString] error:&error];
if (soundObject == nil) NSLog(@"%@", [error description]);
soundObject.delegate = self;
soundObject.numberOfLoops = 0;
soundObject.volume = 1.0;
NSLog(@"about to play");
[soundObject prepareToPlay];
[soundObject play];
NSLog(@"[soundObject play];");
The mp3 used to play fine, and it still does on the simulator. But not on the device.
I've recently added some sound recording code (not mine) to the software. It uses AudioQueue stuff which is slightly beyond me. Does that conflict with AVAudioPlayer? Or what could be the problem? I've noticed that as soon as the audiorecording code starts working, I can't adjust the volume on the device anymore, so maybe it blocks the audio playback?.
EDIT
The solution seems to be to put this in my code. I put it in applicationDidFinishLaunching:
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayAndRecord error: nil];
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);
The first line allows both play and record, whilst the other lines apparently reroute things to make the volume louder.
All audio code is voodoo to me.
© Stack Overflow or respective owner