首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AVAudioPlayer在模拟器中工作,但不在设备上工作

AVAudioPlayer在模拟器中工作,但不在设备上工作
EN

Stack Overflow用户
提问于 2010-06-15 17:26:41
回答 7查看 5.2K关注 0票数 18

我的mp3播放代码是:

代码语言:javascript
复制
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里:

代码语言:javascript
复制
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayAndRecord error: nil]; 
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);

第一条线允许播放和录音,而其他线路显然改变了事情,以使音量更大。

所有的音频代码对我来说都是巫毒。

EN

Stack Overflow用户

发布于 2010-06-15 18:04:19

我知道的一些代码是有效的:

代码语言:javascript
复制
- (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];

}
票数 2
EN
查看全部 7 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3047479

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档