我已经在这里查看了很多关于这个问题的答案,并且无法使用。
这是我的代码
- (void)playSoundWithOfThisFile:(NSString*)fileName {
NSString *soundFilePath = [NSString stringWithFormat:fileName,
[[NSBundle mainBundle] resourcePath]];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL
error:nil];
player.numberOfLoops = 1;
[player play];
}
当我浏览代码时,值如下
fileName = Mummy.mp3
`soundFilePath = Mummy.mp3` (I've had it Sounds/Mummy.mp3 and this doesn't work either)
soundFileURL = Mummy.mp3
- file:///
player = nil
非常感谢任何帮助。如果我抓住它的错误只是一个没有信息的通用错误
答案 0 :(得分:4)
尝试使用以下代码
SystemSoundID soundID;
NSString *soundFile = [[NSBundle mainBundle]
pathForResource:@"test" ofType:@"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)
[NSURL fileURLWithPath:soundFile], & soundID);
AudioServicesPlaySystemSound(soundID);
不要忘记导入
#import <AudioToolbox/AudioToolbox.h>
答案 1 :(得分:0)
试试这个。
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Mummy" ofType:@"mp3"];
NSURL *soundFileURL = [NSURL fileURLWithPath:filePath];
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL
error:nil];
player.numberOfLoops = 1;
[player play];
上的文档