iOS xcode 4.6将音频添加到App

时间:2013-03-05 23:52:35

标签: iphone ios objective-c xcode

我正在尝试在我的应用程序的后台播放音频文件但它没有播放。我的代码是:

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正在播放”,但我听不到它在播放。我的音量很高,因为我在播放时听到我​​的视频播放。有什么想法吗?

1 个答案:

答案 0 :(得分: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?");
}

P.S。,不要将Objective-C变量名称大写(例如“soundID”而不是“SoundID”。