当我搜索它时,我有一个非常常见的问题,但我无法得到正确答案。
这是我努力播放音频文件的代码片段。
在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/
...........
我检查了所有名字,没有任何拼写错误。
不知道哪里出错了,请帮助!!!
答案 0 :(得分:0)
之前我遇到过类似的问题,但这个解决方案只是一个随机的猜测,当时对我有用。
在真正的iPhone上,文件路径区分大小写。在模拟器上,它们不是。所以我会检查以确保您的文件路径完全正确,包括大小写。