iOS 音频文件无法播放

标签 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

编辑: 文件 bell.wav bell.wav:RIFF(小端)数据、WAVE 音频、Microsoft PCM、16 位、立体声 48000 Hz

最佳答案

问题是您在声音有机会开始播放之前就处理掉了它。

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

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(我的书也教你这样做)。

关于iOS 音频文件无法播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20830249/

相关文章:

java - 触摸按钮时发出哔声

c++ - SDL_mixer WAVE 音频格式质量差

c - 如何在C中进行声音合成?

ios - (Swift 2) 如何在我的数据库中获取一个随机的 Realm 对象?

ios - Swift 2 Dictionary<String, AnyObject> 到 Struct getter/setter throwing Cannot subscript value ... with an index of type 'String'

ios - 静音音频 AVCaptureSession

iphone - 是否可以将外部摄像头模块连接到 iPhone 音频插孔?

ios - 有时应用程序崩溃 NSAttributedString 将 HTML 转换为属性字符串

ios - UIMenuController 未显示在 textView 中

ios - cellforrowatindexpath 混合单元格中的数据