我正在写一个iPhone应用程序,当某件事发生时,就会播放声音。我尝试使用不同的WAV,MP3,M4A文件,但有间歇性的问题。下面的代码在模拟器中运行良好,但MP3不能在iPhone上播放。(3GS,如果重要的话)。有什么想法吗?我已将mp3文件更改为wav,它将适用于下面提供的文件(knock_knock),但不适用于其他文件。可能是它的编码方式?
任何帮助都是非常感谢的。谢谢
代码如下:
SystemSoundID soundID;
NSString *soundFile;
if (something == somethingelse) {
soundFile = [[NSBundle mainBundle] pathForResource:@"knock_knock" ofType:@"wav"];
}
else {
soundFile = [[NSBundle mainBundle] pathForResource:@"strike" ofType:@"mp3"];
}
AudioServicesCreateSystemSoundID((CFURLRef)
[NSURL fileURLWithPath:soundFile]
, &soundID);
AudioServicesPlaySystemSound(soundID);发布于 2010-01-22 02:16:37
系统声音必须解压缩。我遇到了这个问题,并将代码更改为使用AVAudioSession和AVAudioPlayer。在我的头顶上,像这样的东西(需要更好的错误检查)
-(void)load {
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: [self path]];
if (fileURL) {
AVAudioPlayer *newPlayer =
[[AVAudioPlayer alloc] initWithContentsOfURL: fileURL
error: nil];
[fileURL release];
[self stop];
self.player = newPlayer;
[newPlayer release];
[self.player prepareToPlay];
}
}
-(void)play {
if (self.player == nil)
[self load];
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *err = nil;
[audioSession setCategory: AVAudioSessionCategoryAmbient error:&err];
if(err){
NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
return;
}
[audioSession setActive:YES error:&err];
self.player.currentTime = 0;
[self.player play];
}https://stackoverflow.com/questions/2111293
复制相似问题