简体   繁体   English

iOS音频文件无法播放

[英]iOS Audio Files not playing

I am trying to play my audio file, which is 1.9 seconds, in my iOS App with these lines of code: 我正在尝试使用以下代码行在iOS应用中播放1.9秒的音频文件:

NSString *path  = [[NSBundle mainBundle] pathForResource:@"bell" ofType:@"wav"];
NSURL *pathURL = [NSURL fileURLWithPath : path];
SystemSoundID audioEffect;
AudioServicesCreateSystemSoundID((__bridge CFURLRef) pathURL, &audioEffect);
AudioServicesPlaySystemSound(audioEffect);
AudioServicesDisposeSystemSoundID(audioEffect);

The audio file never plays but the code gets called. 音频文件从不播放,但是代码被调用。

文件

EDIT: file bell.wav bell.wav: RIFF (little-endian) data, WAVE audio, Microsoft PCM, 16 bit, stereo 48000 Hz 编辑: file bell.wav bell.wav: RIFF (little-endian) data, WAVE audio, Microsoft PCM, 16 bit, stereo 48000 Hz

The problem is that you are disposing the sound before it has a chance to start playing. 问题在于您正在处理声音,然后才有机会开始播放。

My book teaches you how to do this. 我的书教你如何做。 Here is the code I provide in my book: 这是我在书中提供的代码:

void SoundFinished (SystemSoundID snd, void* context) {
    // NSLog(@"finished!");
    AudioServicesRemoveSystemSoundCompletion(snd);
    AudioServicesDisposeSystemSoundID(snd);
}

- (IBAction)doButton:(id)sender {
    NSURL* sndurl = [[NSBundle mainBundle] URLForResource:@"test" withExtension:@"aif"];
    SystemSoundID snd;
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)sndurl, &snd);
    AudioServicesAddSystemSoundCompletion(snd, nil, nil, SoundFinished, nil);
    AudioServicesPlaySystemSound(snd);
}

You can adapt that to use your sound and to trigger the playing in the way you need. 您可以对其进行调整以使用您的声音并以您需要的方式触发播放。

However, do note that you should not be using a 1.9 second sound as a system sound. 但是,请注意,您不应将1.9秒的声音用作系统声音。 It is much better to use an AVAudioPlayer (which my book also teaches you to do). 最好使用AVAudioPlayer(我的书也教您这样做)。

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

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