AVAudioPlayer在模拟器中工作,但不在设备上
我的mp3播放代码是:
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];");
mp3过去玩得很好,仍在模拟器上 。但不在设备上。
The mp3 used to play fine, and it still does on the simulator. But not on the device.
我最近在软件中添加了一些录音代码(不是我的)。它使用的AudioQueue东西略微超出我的范围。这与AVAudioPlayer有冲突吗?或者可能是什么问题?我注意到,只要音频录制代码开始工作,我就无法再调整设备上的音量,所以它可能会阻止音频播放吗?
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?.
编辑
解决方案似乎是把它放在我的代码中。我把它放在applicationDidFinishLaunching中:
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.