0

我正在尝试在我的应用程序的背景中播放音频文件,但它没有播放。我拥有的代码是:

SystemSoundID SoundID;
NSString *soundFile = [[NSBundle mainBundle] pathForResource:@"HoHey" ofType:@"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:soundFile], &SoundID);
AudioServicesPlaySystemSound(SoundID);
NSLog(@"%@ is playing\n", soundFile);

NSLog 显示“HoHey.mp3 正在播放”,但我听不到它在播放。顺便说一句,我的音量调高了,因为我在播放视频时会听到播放的声音。有任何想法吗?

4

1 回答 1

0

您可能在途中的某个地方出现错误。尝试如下所示的代码:

SystemSoundID soundID;
NSString *soundFile = [[NSBundle mainBundle] pathForResource:@"HoHey" ofType:@"mp3"];
if(soundFile)
{
    OSStatus status = AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:soundFile], &soundID);
    if(status == noErr)
    {
        AudioServicesPlaySystemSound(soundID);
        NSLog(@"%@ is playing\n", soundFile);
    } else {
        NSLog( @"no sound id created; error status code is %d", status);
    }
} else {
    NSLog( @"couldn't find sound file... is it really in your app bundle?");
}

PS,不要大写Objective-C 变量名(例如“ soundID”而不是“ SoundID”)。

于 2013-03-06T00:01:59.040 回答