.mp3 和 .wav 文件适用于模拟器,但不适用于设备
Posted
技术标签:
【中文标题】.mp3 和 .wav 文件适用于模拟器,但不适用于设备【英文标题】:.mp3 and .wav files works on simulator, but not on device 【发布时间】:2011-10-04 06:10:54 【问题描述】:我在搜索时遇到了非常常见的问题,但无法为我找到正确的答案。
这是我正在努力播放音频文件的一段代码。
在 viewDidLoad 方法中:
NSURL *buttonSound = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/button4.wav", [[NSBundle mainBundle] resourcePath]]];
NSURL *correctSound = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/key_drop1.wav", [[NSBundle mainBundle] resourcePath]]];
NSURL *wrongSound = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/sound8.mp3", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
ButtonPressedSound = [[AVAudioPlayer alloc] initWithContentsOfURL:buttonSound error:&error];
ButtonPressedSound.numberOfLoops = 0;
correctAnsweredSound = [[AVAudioPlayer alloc] initWithContentsOfURL:correctSound error:&error];
correctAnsweredSound.numberOfLoops = 0;
wrongAnsweredSound = [[AVAudioPlayer alloc] initWithContentsOfURL:wrongSound error:&error];
wrongAnsweredSound.numberOfLoops = 0;
在按下按钮的IBAction中m以这种方式调用“播放”。
[ButtonPressedSound play];
[self performSelector:@selector(stop) withObject:nil afterDelay:0.50];
[correctAnsweredSound play];
[self performSelector:@selector(stop) withObject:nil afterDelay:0.50];
[wrongAnsweredSound play];
[self performSelector:@selector(stop) withObject:nil afterDelay:1.0];
我的播放和停止方法是:
-(IBAction)play
self.ButtonPressedSound.currentTime = 0;
[self.ButtonPressedSound play];
self.correctAnsweredSound.currentTime = 0;
[self.correctAnsweredSound play];
self.wrongAnsweredSound.currentTime = 0;
[self.wrongAnsweredSound play];
-(IBAction)stop
[self.ButtonPressedSound stop];
[self.correctAnsweredSound stop];
[self.wrongAnsweredSound stop];
到目前为止我做了什么......
我已经搜索了这些链接,但没有得到我的代码的答案。
AVAudioPlayer only plays in simulator but not device why?! (iPhone-SDK)
mp3 Sounds Playing on simulator but not on Device (Audio BeatBox)
http://www.icodeblog.com/2009/05/04/iphone-game-programming-tutorial-part-4-basic-game-audio/
...........
我已经检查了所有的名字 n 没有任何拼写错误。
不知道哪里出错了,请帮忙!!!
【问题讨论】:
【参考方案1】:我以前也遇到过类似的问题,但这个解决方案只是我当时的一个随机猜测。
在真正的 iPhone 上,文件路径区分大小写。在模拟器上,它们不是。所以我会检查以确保您的文件路径完全正确,包括大小写。
【讨论】:
以上是关于.mp3 和 .wav 文件适用于模拟器,但不适用于设备的主要内容,如果未能解决你的问题,请参考以下文章