iOS 开发 - 播放器Demo开发记录

来源:互联网 发布:厦门华鑫网络 编辑:程序博客网 时间:2024/06/08 09:42
 

iOS 开发 - 播放器Demo开发记录

标签: ios
 518人阅读 评论(0) 收藏 举报
 分类:
 

首先

[objc] view plain copy
  1. #import <AVFoundation/AVFoundation.h>  


于viewDidLoad中
[objc] view plain copy
  1. //设置代理  
  2.     XYZAVAudioPlayer.delegate = self;  

创建音频类

[objc] view plain copy
  1. @interface ViewController () {  
  2.     AVAudioPlayer *XYZAVAudioPlayer;   //播放器player  
  3.     NSTimer *timer;                 //监控音频播放进度  
  4.     NSTimer *stoptimer;            //歌曲结束声量控制  
  5. }  

设置播放

[objc] view plain copy
  1. //从budle路径下读取音频文件  轻音乐 - 萨克斯回家 这个文件名是你的歌曲名字,mp3是你的音频格式  
  2.     NSString *string = [[NSBundle mainBundle] pathForResource:@"G.E.M.邓紫棋 - 喜欢你" ofType:@"mp3"];  
  3.     //把音频文件转换成url格式  
  4.     NSURL *url = [NSURL fileURLWithPath:string];  
  5.     //初始化音频类 并且添加播放文件  
  6.     XYZAVAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];  
  7.       
  8.     //设置音乐播放次数  -1为一直循环  
  9.     XYZAVAudioPlayer.numberOfLoops = -1;  
  10.       
  11.     //预播放  
  12.     [XYZAVAudioPlayer prepareToPlay];  
  13.       
  14.     //用NSTimer来监控音频播放进度  
  15.     timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self  
  16.                                            selector:@selector(playProgress)  
  17.                                            userInfo:nil repeats:YES];  
  18.     //初始化音量控制  
  19.     [self.VolumeSlider addTarget:self action:@selector(volumeChange)  
  20.                 forControlEvents:UIControlEventValueChanged];  
  21.     //设置最小音量  
  22.     self.VolumeSlider.minimumValue = 0.0f;  
  23.     //设置最大音量  
  24.     self.VolumeSlider.maximumValue = 10.0f;  
  25.     //初始化音量为多少  
  26.     self.VolumeSlider.value = 5.0f;  
  27.       
  28.     //声音开关控件(静音)  
  29.     [self.musicSwith addTarget:self action:@selector(onOrOff:) forControlEvents:UIControlEventValueChanged];  
  30.     //默认状态为打开  
  31.     self.musicSwith.on = YES;  
  32.       
  33.     //进度条slider控件  
  34.     [self.musicSlider addTarget:self action:@selector(playSlider) forControlEvents:UIControlEventValueChanged];  
  35.   
  36.     stoptimer = [NSTimer timerWithTimeInterval:0.1 target:self selector:@selector(selector) userInfo:nil repeats:YES];  

余下方法

[objc] view plain copy
  1. //进度条控制  
  2. -(void)playSlider {  
  3.     XYZAVAudioPlayer.currentTime = XYZAVAudioPlayer.duration * self.musicSlider.value;  
  4. }  
  5.   
  6. //播放  
  7. - (void)play  
  8. {  
  9.     [XYZAVAudioPlayer play];  
  10. }  
  11. //暂停  
  12. - (void)pause  
  13. {  
  14.     [XYZAVAudioPlayer pause];  
  15. }  
  16. //停止  
  17. - (void)stop  
  18. {  
  19.     XYZAVAudioPlayer.currentTime = 0;  //当前播放时间设置为0  
  20.     [XYZAVAudioPlayer stop];  
  21. }  
  22. //播放进度条  
  23. - (void)playProgress  
  24. {  
  25.     //通过音频播放时长的百分比,给progressview进行赋值;  
  26.     self.musicprogress.progress = XYZAVAudioPlayer.currentTime/XYZAVAudioPlayer.duration;  
  27.     NSLog(@"%f --- %f",XYZAVAudioPlayer.currentTime,XYZAVAudioPlayer.duration);  
  28.     self.startTime.text = [NSString stringWithFormat:@"%f",XYZAVAudioPlayer.currentTime];  
  29.     self.stopTime.text = [NSString stringWithFormat:@"%f",XYZAVAudioPlayer.duration];  
  30.   
  31.     self.musicSlider.value = XYZAVAudioPlayer.currentTime/XYZAVAudioPlayer.duration;  
  32. }  
  33.   
  34. //声音开关(是否静音)  
  35. - (void)onOrOff:(UISwitch *)sender  
  36. {  
  37.     XYZAVAudioPlayer.volume = sender.on;  
  38. }  
  39.   
  40. //播放音量控制  
  41. //待实现控制系统音量  
  42. - (void)volumeChange  
  43. {  
  44.     XYZAVAudioPlayer.volume = self.VolumeSlider.value;  
  45. }  
  46.   
  47. //播放完成时调用的方法  (代理里的方法),需要设置代理才可以调用  
  48. - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag  
  49. {  
  50.     [timer invalidate]; //NSTimer暂停   invalidate  使...无效;  
  51. }  
  52.   
  53. - (IBAction)StopBtn:(id)sender {  
  54.     [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(stopVolume:) userInfo:nil repeats:YES];  
  55.     XYZAVAudioPlayer.currentTime = 0;  //当前播放时间设置为0  
  56.     [XYZAVAudioPlayer stop];  
  57. }  
  58.   
  59. - (IBAction)PlayBtn:(id)sender {  
  60.     [XYZAVAudioPlayer play];  
  61. }  
  62. - (IBAction)PauseBtn:(id)sender {  
  63.     [XYZAVAudioPlayer pause];  
  64. }  

0 0
原创粉丝点击