iOS开发实现音频播放功能

2025-05-29 0 92

音频播放

1、介绍

– 功能介绍

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

– 框架介绍

* AVAudioPlayer

* 初始化:

注意 :

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

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

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

声明全局变量

?

1

2

3

4

5

6

7

8

9
@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中

?

1

2

3

4

5

6

7

8

9
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"];

按钮的触发方法:

?

1

2

3

4
- (void)play:(UIButton *)sender{

sender.selected = !sender.selected;

sender.selected != YES ? [audioPlayer pause]:[audioPlayer play];

}

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47
- (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;

}

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 iOS开发实现音频播放功能 https://www.kuaiidc.com/93022.html

相关文章

猜你喜欢
发表评论
暂无评论