iOS音频文件无法播放

时间:2013-12-29 21:27:19

标签: ios audio

我正在尝试使用以下代码行在我的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);

音频文件永远不会播放,但会调用代码。

file

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

1 个答案:

答案 0 :(得分:3)

问题是你在有机会开始播放之前处理声音。

我的书教你如何做到这一点。这是我在书中提供的代码:

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);
}

您可以调整它以使用您的声音并以您需要的方式触发播放。

但请注意,您不应将1.9秒的声音用作系统声音。最好使用AVAudioPlayer(我的书也会教你这么做)。

相关问题