}
代码验证,示例如下:
导入AVFoundation框架
导入要播放的音频文件
编辑控制器的.h文件如下:
// // ViewController.h // 音频播放器 // // Created by apple on 15/9/5. // Copyright (c) 2015年 LiuXun. All rights reserved. // #import <UIKit/UIKit.h> #import <AVFoundation/AVFoundation.h> @interface ViewController : UIViewController <AVAudioPlayerDelegate> { BOOL interrFlag; // 中断前的播放状态,如果正在播放赋值为YES } @property (nonatomic, strong) NSTimer *timer; // 不断循环执行某件事 @property (nonatomic, strong) UISlider *volumeSlider; // 用于调节声音 @property (nonatomic, strong) UISlider *currentSlider; // 用于调节进度 @property (nonatomic, strong ) UILabel *timeLabel; // 用于显示播放时间 @property (nonatomic, strong) UIButton *btnPauseAndPlay; // 播放与暂停按钮 @property (nonatomic, strong) AVAudioPlayer *avPlayer; // 把播放音频的类对象声明为全局变量 -(void) sliderAction:(UISlider *) sender; // 监听所有滑动条的事件 -(void)btnAction; // 监听按钮的事件 @end
// // ViewController.m // 音频播放器 // // Created by apple on 15/9/5. // Copyright (c) 2015年 LiuXun. All rights reserved. // #import "ViewController.h" #define WIDTH [UIScreen mainScreen].bounds.size.width #define HEIGHT [UIScreen mainScreen].bounds.size.height @interface ViewController () @end @implementation ViewController -(void) playAV // 把播放音频的功能封装在此方法中 { NSError *error; // 设置音频会话支持后台播放 [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&error]; // 获取指定音频文件的地址 NSString *path = [[NSBundle mainBundle] pathForResource:@"第一夫人" ofType:@"mp3"]; // 创建NSURL对象 NSURL *url = [NSURL fileURLWithPath:path]; // 因为方法名已经有file指明了协议头为本地文件 // 为音频播放类的对象开辟空间 self.avPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil]; _avPlayer.delegate = self; if (! self.avPlayer.playing) { [self.avPlayer play]; } } -(void) zjLayout // 此方法封装各种组件 { // 音量 UILabel * lblVolume = [[UILabel alloc] initWithFrame:CGRectMake(20, 30, WIDTH, WIDTH/6)]; lblVolume .text = @"音量"; lblVolume.textAlignment = NSTextAlignmentLeft; self.volumeSlider = [[UISlider alloc] initWithFrame:CGRectMake(WIDTH/5, 30, WIDTH*0.6, WIDTH/6)]; [self.view addSubview:self.volumeSlider]; [self.view addSubview:lblVolume]; self.volumeSlider.minimumValue = 0.0; // 因为音量的最值是0.0 self.volumeSlider.maximumValue = 1.0; // 音量的最大值是1 self.volumeSlider.tag = 1; [self.volumeSlider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventTouchUpInside]; //进度 UILabel * lblCurrent = [[UILabel alloc] initWithFrame:CGRectMake(20, 30+WIDTH/6, WIDTH, WIDTH/6)]; lblCurrent .text = @"进度"; lblCurrent.textAlignment = NSTextAlignmentLeft; self.currentSlider = [[UISlider alloc] initWithFrame:CGRectMake(WIDTH/5, 30+WIDTH/6, WIDTH*0.6, WIDTH/6)]; [self.view addSubview:lblCurrent]; [self.view addSubview:self.currentSlider]; self.currentSlider.minimumValue = 0.0; self.currentSlider.maximumValue = self.avPlayer.duration; // 播放的总时长 self.currentSlider.tag = 2; [self.currentSlider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventTouchUpInside]; // 播放暂停按钮 self.btnPauseAndPlay = [[UIButton alloc] initWithFrame:CGRectMake(20, 40+WIDTH/3, WIDTH/6, WIDTH/8)]; [self.btnPauseAndPlay setTitle:@"暂停" forState:UIControlStateNormal]; [self.btnPauseAndPlay addTarget:self action:@selector(btnAction) forControlEvents:UIControlEventTouchUpInside]; [self.btnPauseAndPlay setBackgroundColor:[UIColor darkGrayColor]]; [self.view addSubview:self.btnPauseAndPlay]; // 播放时间显示标签 self.timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 40+WIDTH/3+WIDTH/8, WIDTH, WIDTH/8)]; self.timeLabel .text = [NSString stringWithFormat:@"剩余时长: %.0f 已播放: %.0f",self.avPlayer.duration,0.0]; [self.view addSubview:self.timeLabel]; // 利用NStimer计时器来刷新时间标签,和滚动条的滚动值 self.timer =[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(timerAction) userInfo:nil repeats:YES]; } - (void)viewDidLoad { [super viewDidLoad]; [self playAV]; // 必须先调用播放音频的功能,然后才能给总时长duration赋值供组件使用 [self zjLayout]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(void)timerAction { // 剩余时间 double syTime = self.avPlayer.duration - self.avPlayer.currentTime; self.timeLabel.text = [NSString stringWithFormat:@"剩余时长: %.0f 已播放: %.0f",syTime,self.avPlayer.currentTime]; // 让滑动条跟随播放进度滚动 self.currentSlider.value = self.avPlayer.currentTime; } -(void) sliderAction:(UISlider *)sender // 监听所有滑动条的滑动事件 { switch (sender.tag) { case 1: self.avPlayer.volume = sender.value; break; case 2: self.avPlayer.currentTime = sender.value; break; } } -(void)btnAction // 此方法监听按钮的点击事件 { if (self.avPlayer.isPlaying) { [self.avPlayer pause]; [self.btnPauseAndPlay setTitle:@"播放" forState:UIControlStateNormal]; // 开始播放时重新启动计时器功能 self.timer =[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(timerAction) userInfo:nil repeats:YES]; } else { [self.avPlayer play]; [self.btnPauseAndPlay setTitle:@"暂停" forState:UIControlStateNormal]; [self.timer invalidate]; // 播放暂停时让计时器报废 } } // 以下处理AVAudioPlayerDelegate代理协议中方法 -(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag { NSLog(@"音频播放完成"); } -(void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error { NSLog(@"解码错误"); } -(void) audioPlayerBeginInterruption:(AVAudioPlayer *)player { // 当打电话时就打断了音频的播放,需要把它暂停 if (player.isPlaying) { // 当正在播放时,暂停即可 [self btnAction]; interrFlag = YES; } } -(void) audioPlayerEndInterruption:(AVAudioPlayer *)player withOptions:(NSUInteger)flags { // 当中断结束时,重返原来状态 if (interrFlag ) { [self btnAction]; interrFlag = NO; } } @end运行结果如下: