前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用AVAudioPlayer播放音乐文件

使用AVAudioPlayer播放音乐文件

作者头像
EltonZheng
发布2021-01-22 07:52:00
8200
发布2021-01-22 07:52:00
举报

AVAudioPlayer 提供了大量的特性,包括暂停播放,调整音量,监控音频的峰值和均值等等。 我们看下面的例子:

代码语言:javascript
复制
AVAudioPlayer  *player;
NSString       *path;
 
// 设置音乐文件路径
path = [[NSBundle mainBundle] pathForResource:@"sound-file" ofType:@"mp3"];
 
// 判断是否可以访问这个文件
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) 
{    
  // 设置 player
  player = [[AVAudioPlayer alloc] initWithContentsOfURL:
     [NSURL fileURLWithPath:path] error:&error];
 
  // 调节音量 (范围从0到1)
  player.volume = 0.4f;
 
  // 准备buffer,减少播放延时的时间      
  [player prepareToPlay];
 
  // 设置播放次数,0为播放一次,负数为循环播放
  [player setNumberOfLoops:0];
 
  [player play];    
 
}    
 
...
 
// 清理工作
if (player != nil)
{
  if (player.isPlaying == YES)
    [player stop];
  [player release];
}

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档