8.29--多媒体--音频,音乐

音频播放

添加音频所需的条件:

1,音频时间小于30秒

2,数据格式为PCM,IMA4

3,音频文件后缀 .caf .iaf .wav

引入框架

#import <AudioToolbox/AudioToolbox.h>

回调函数

void audioPlay (SystemSoundID soundID,void *clientData)

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

获取音频路径

NSString *str=[[NSBundle mainBundle]pathForResource:@“—————“ ofType:nil];

路径转换为 NSURL

NSURL *url=[NSURL fileURLWithPath:str];

赋初始值

SystemSoundID soundID=0;

获得系统声音ID(创建)

AudioServicesCreateSystemSoundID((__bridge CFURLRef _Nonnull)(url), &soundID);

播放完成的回调函数(监听播放,相当于添加事件)

AudioServicesAddSystemSoundCompletion(soundID, NULL, NULL, audioPlay, NULL);

播放

AudioServicesPlaySystemSound(soundID);

音乐播放

引入框架 #import <AVFoundation/AVFoundation.h>

设置类属性 @property(nonatomic,strong)AVAudioPlayer *player;

字符串接收音乐文件路径 NSString *str=[[NSBundle mainBundle]pathForResource:@“—————“ ofType:nil];

路径转换为 NSURL类型 NSURL *url=[NSURL fileURLWithPath:str];

_player=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];

循环次数,0-不循环,-1-无限循环,1,2,3-循环的次数

_player.numberOfLoops=0;

设置音量初始值

_player.volume=4;

加到缓冲区

[_player prepareToPlay];

添加按钮控制音乐的播放

UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(20, 20, 60, 30)];

button.backgroundColor=[UIColor lightGrayColor];

[button setTitle:@"播放" forState:UIControlStateNormal];

[button addTarget:self action:@selector(played:) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:button];

按钮的点击事件中用到的方法

[_player play];

转载于:https://my.oschina.net/shaosimingbaifeng/blog/739475

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值