我的mp3播放代码是:
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过去玩得很好,而在模拟器上仍然这样做。但不是在设备上。
我最近在软件中添加了一些录音代码(不是我的)。它使用的是AudioQueue的东西,稍微超出了我的范围。这和AVAudioPlayer有冲突吗?或者是什么问题?我注意到,一旦录音代码开始工作,我就不能再调整设备上的音量了,所以也许它会阻止音频播放?
编辑
解决方案似乎是把它放在我的代码中。我把它放在applicationDidFinishLaunching里:
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayAndRecord error: nil];
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);第一条线允许播放和录音,而其他线路显然改变了事情,以使音量更大。
所有的音频代码对我来说都是巫毒。
发布于 2010-06-15 18:04:19
我知道的一些代码是有效的:
- (void)playOnce:(NSString *)aSound {
// Gets the file system path to the sound to play.
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:aSound ofType:@"caf"];
// Converts the sound's file path to an NSURL object
NSURL *soundURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
self.soundFileURL = soundURL;
[soundURL release];
AVAudioPlayer * newAudio=[[AVAudioPlayer alloc] initWithContentsOfURL: soundFileURL error:nil];
self.theAudio = newAudio; // automatically retain audio and dealloc old file if new file is loaded
[newAudio release]; // release the audio safely
[theAudio prepareToPlay];
// set it up and play
[theAudio setNumberOfLoops:0];
[theAudio setVolume: volumeLevel];
[theAudio setDelegate: self];
[theAudio play];
}https://stackoverflow.com/questions/3047479
复制相似问题