116 音效&音乐

1.音效播放

音效播放常见函数总结
加载音效文件
AudioServicesCreateSystemSoundID(CFURLRef inFileURL, SystemSoundID *outSystemSoundID)

释放音效资源
AudioServicesDisposeSystemSoundID(SystemSoundID inSystemSoundID)

播放音效
AudioServicesPlaySystemSound(SystemSoundID inSystemSoundID)

播放音效带点震动
AudioServicesPlayAlertSound(SystemSoundID inSystemSoundID)

示例代码:


导入#import <AVFoundation/AVFoundation.h>
    // 1.获得音效文件的全路径
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"buyao.wav" withExtension:nil];

    // 2.加载音效文件, 创建音效ID(SoundID)
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)url, &_soundID);

    // 3.播放
    AudioServicesPlaySystemSound(_soundID);

2.音乐播放:
AVAudioPlayer的类, 能够用于播放本地音频文件

加载音乐文件
- (id)initWithContentsOfURL:(NSURL *)url error:(NSError **)outError;
- (id)initWithData:(NSData *)data error:(NSError **)outError;

准备播放(缓冲,提高播放的流畅性)
- (BOOL)prepareToPlay;

播放(异步播放)
- (BOOL)play;

是否正在播放
@property(readonly, getter=isPlaying) BOOL playing;

时长
@property(readonly) NSTimeInterval duration;

当前的播放位置
@property NSTimeInterval currentTime;

播放次数(-1代表无限循环播放,其他代表播放numberOfLoops+1次)
@property NSInteger numberOfLoops;

音量
@property float volume;

是否允许更改速率
@property BOOL enableRate;

播放速率(1是正常速率,0.5是一般速率,2是双倍速率)
@property float rate;

有多少个声道
@property(readonly) NSUInteger numberOfChannels;

示例代码

// 0.音频文件的URL
NSURL *url = [[NSBundle mainBundle] URLForResource:@"235319.mp3"withExtension:nil];

// 1.创建播放器(一个AVAudioPlayer只能播放一个URL)
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];

// 2.缓冲
[player prepareToPlay];

self.player = player;
//播放
[self.player play];
//暂停
[self.player pause];
//stop之后player应该销毁,否则会出现一些意外的错误
[self.player stop];

文件格式转换:

转换aiff格式
afconvert -f AIFF -d I8 filename

转换caf格式
afconvert -f caff -d aac -b 32000 filename

批量转换
find . -name '*.mp3' -exec afconvert -f caff -d aac -b 32000 {} \;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值