简体   繁体   English

iOS xcode 4.6向应用程序添加音频

[英]iOS xcode 4.6 Adding Audio to App

I'm trying to play an audio file in the background of my app and it isn't playing. 我正在尝试在应用程序的背景中播放音频文件,但未播放。 The code that I have is: 我拥有的代码是:

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

The NSLog displays "HoHey.mp3 is playing" but I can't hear it playing. NSLog显示“ HoHey.mp3正在播放”,但我听不到声音。 My volume is turned up btw because I hear my videos play when I play them. 音量之所以调高,是因为播放视频时我听到了它们的声音。 Any ideas? 有任何想法吗?

You likely have an error somewhere along the way. 您可能会在途中某处出错。 Try code that looks like this: 尝试如下代码:

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, don't capitalize Objective-C variable names (eg " soundID " instead of " SoundID "). PS,请勿使用大写的Objective-C变量名称(例如,“ soundID ”而不是“ SoundID ”)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM