美文网首页
AudioToolbox循环播放本地音频,简单使用

AudioToolbox循环播放本地音频,简单使用

作者: wenju | 来源:发表于2020-04-22 14:52 被阅读0次

导入AudioToolbox.framework

  • (void)playSoundEffect:(NSString*)name {

       //获取文件的路径
       NSString *audioFilePath = [[NSBundle mainBundle] pathForResource:name ofType:@"wav"];
        NSURL *fileUrl = [NSURL fileURLWithPath:audioFilePath];
        SystemSoundID soundID = 0;
        AudioServicesCreateSystemSoundID((__bridge CFURLRef)(fileUrl), &soundID);
        
        //播放并且监听是否播放完成
        AudioServicesPlaySystemSoundWithCompletion(soundID, ^{
          NSLog(@"播放完成回调");
          //设置播放完成之后外部回掉,不能直接播放,需配合AudioServicesPlaySystemSound(soundID)使    用
          AudioServicesAddSystemSoundCompletion(soundID, NULL, NULL,systemAudioCallback, NULL);
          AudioServicesPlaySystemSound(soundID);
      });
    

}

void systemAudioCallback(){
NSLog(@"播放完成2");
}

相关文章

网友评论

      本文标题:AudioToolbox循环播放本地音频,简单使用

      本文链接:https://www.haomeiwen.com/subject/qcyiihtx.html