手机
当前位置:查字典教程网 >编程开发 >IOS开发 >iOS开发实现音频播放功能
iOS开发实现音频播放功能
摘要:音频播放1、介绍-功能介绍用于播放比较长的音频、说明、音乐,使用到的是AVFoundation-框架介绍*AVAudioPlayer*初始化...

音频播放

1、介绍

- 功能介绍

用于播放比较长的音频、说明、音乐 ,使用到的是AVFoundation

- 框架介绍

* AVAudioPlayer

* 初始化:

注意 :

(3)必须声明全局变量的音乐播放对象、或者是属性的音乐播放对象 才可以播放

(4)在退出播放页面的时候 一定要把播放对象置空 同时把delegate置空

导入框架:#import <AVFoundation/AVFoundation.h>

声明全局变量

@interface ViewController ()<AVAudioPlayerDelegate> { AVAudioPlayer *audioPlayer; } @end

音频的基本属性

预播放 [audioPlayer prepareToPlay];

获取 当前音乐的声道 audioPlayer.numberOfChannels

audioPlayer.currentTime //当前播放的时间

audioPlayer.playing//判断是否正在播放

audioPlayer.numberOfLoops ;//设置循环播放的此次

audioPlayer.duration 获得播放音频的时间

audioPlayer.pan 设置左右声道效果

audioPlayer.volume 设置音量 0.0- 1.0 是一个百分比

//设置速率 必须设置enableRate 为YES; audioPlayer.enableRate =YES; audioPlayer.rate =3.0;

音量 audioPlayer.volume = 0.1;

设置播放次数 负数是无限循环的 0 是一次 1是两次 依次类推 audioPlayer.numberOfLoops = 0;

获得当前峰值 audioPlayer peakPowerForChannel:2

平均峰值

audioPlayer averagePowerForChannel:2

音频播放的几个代理方法

//播放完成的时候调用

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{

NSLog(@"播放完成");

}

//解码出现错误的时候调用

- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError * __nullable)error{

}

//开始被打扰中断的时候调用

- (void)audioPlayerBeginInterruption:(AVAudioPlayer *)player{

}

//中断结束后调用

- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player withOptions:(NSUInteger)flags{

}

实例解析音频播放

初始化一个按钮

在ViewDidLoad中

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(100, 100, 100, 100); button.backgroundColor = [UIColor brownColor]; [button setTitle:@"Play" forState:UIControlStateNormal]; [button setTitle:@"Pause" forState:UIControlStateSelected]; [button addTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; [self playMusicWithName:@"TFBOYS-青春修炼手册.mp3"];

按钮的触发方法:

- (void)play:(UIButton *)sender{ sender.selected = !sender.selected; sender.selected != YES ? [audioPlayer pause]:[audioPlayer play]; }

- (void)playMusicWithName:(NSString *)name{ NSError *error; // 创建一个音乐播放对象 audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:[[NSBundle mainBundle]URLForResource:name withExtension:nil] error:&error]; if (error) { NSLog(@"%@",error); } 预播放 [audioPlayer prepareToPlay]; 播放 在这里不写在这 但它是必须的步骤 [audioPlayer play]; 获取 当前音乐的声道 NSLog(@"%ld",audioPlayer.numberOfChannels); durtion:获得播放音频的时间 设置声道 -1.0左 0.0中间 1.0右 audioPlayer.pan = 0.0; 音量 audioPlayer.volume = 0.1; 设置速率 必须设置enableRate为YES audioPlayer.enableRate = YES; 设置速率 0.5是一半的速度 1.0普通 2.0双倍速率 audioPlayer.rate = 1.0; currentTime 获得时间 获得峰值 必须设置meteringEnabled为YES audioPlayer.meteringEnabled = YES; 更新峰值 [audioPlayer updateMeters]; 获得当前峰值 NSLog(@"当前峰值:%f",[audioPlayer peakPowerForChannel:2]); NSLog(@"平均峰值%f",[audioPlayer averagePowerForChannel:2]); 设置播放次数 负数是无限循环的 0 是一次 1是两次 依次类推 audioPlayer.numberOfLoops = 0; 挂上代理 audioPlayer.delegate = self; }

【iOS开发实现音频播放功能】相关文章:

IOS实现简单的进度条功能

iOS开发中使用UIScrollView实现图片轮播和点击加载

使用UItableview在iOS应用开发中实现好友列表功能

IOS开发:提示框的正确实现方式

iOS开发中实现邮件和短信发送的简单示例

iOS开发基础之C语言

IOS开发第三方语音-微信语音

IOS程序开发之跳转短信发送界面实现发送短信功能

实例解析iOS app开发中音频文件播放工具类的封装

IOS开发实现录音功能

精品推荐
分类导航