iOS播放MP3音频

1.新建single view工程,导入AVFoundation库

2.ViewController.xib如图



3.ViewController.h文件

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #import <UIKit/UIKit.h>  
  2. #import <AVFoundation/AVFoundation.h>  
  3.   
  4. @interface ViewController : UIViewController {  
  5.     //进度  
  6.     IBOutlet UISlider *_proSlider;  
  7.     //声道  
  8.     IBOutlet UISlider *_panSlider;  
  9.     //速度  
  10.     IBOutlet UISlider *_speedSlider;  
  11.     //音量  
  12.     IBOutlet UISlider *_volSlider;  
  13.     IBOutlet UIProgressView *_proV;  
  14.     IBOutlet UIProgressView *_proV2;  
  15.       
  16.     AVAudioPlayer *_player;  
  17.       
  18.     NSTimer *_timer;  
  19. }  
  20.   
  21. - (IBAction)proSlider:(id)sender;  
  22. - (IBAction)panSlider:(id)sender;  
  23. - (IBAction)speedSlider:(id)sender;  
  24. - (IBAction)volSlider:(id)sender;  
  25.   
  26. - (IBAction)play:(id)sender;  
  27. - (IBAction)pause:(id)sender;  
  28.   
  29. @end  

4.ViewController.m文件
[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #import "ViewController.h"  
  2.   
  3. @implementation ViewController  
  4.   
  5. - (void)viewDidLoad  
  6. {  
  7.     [super viewDidLoad];  
  8.       
  9.     NSString *path = [[NSBundle mainBundle] pathForResource:@"Beat It" ofType:@"mp3"];  
  10. //    NSURL *url = [NSURL URLWithString:path];//不能这样写,因为是本地路径  
  11.     NSURL *url = [NSURL fileURLWithPath:path];//本地路径应该这样写  
  12.       
  13.     _player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];  
  14.     //触发play事件的时候会将mp3文件加载到内存中,然后再播放,所以开始的时候可能按按钮的时候会卡,所以需要prepare  
  15.     [_player prepareToPlay];  
  16.     //设置支持变速  
  17.     _player.enableRate = YES;  
  18.     //峰值和平均值  
  19.     _player.meteringEnabled = YES;  
  20.   
  21. }  
  22.   
  23. - (void)play:(id)sender  
  24. {  
  25.     //按播放,开始定时器  
  26.     _timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(refresh) userInfo:nil repeats:YES];  
  27.     [_player play];  
  28. }  
  29.   
  30. - (void)pause:(id)sender  
  31. {  
  32.     [_player pause];  
  33.     //定时器失效  
  34.     [_timer invalidate];  
  35. }  
  36.   
  37. - (void)refresh  
  38. {  
  39.     //每隔0.1秒刷新一次进度,当前时间/总时间  
  40.     float pro = _player.currentTime/_player.duration;  
  41.     [_proSlider setValue:pro animated:YES];  
  42.       
  43.     //averagePowerForChannel和peakPowerForChannel的属性分别为声音的最高振幅和平均振幅  
  44.     [_player updateMeters];//不刷新就永远是0  
  45.     float pead = ([_player peakPowerForChannel:0]+50)/50;//0左声道,1右声道  
  46.     float ave = ([_player averagePowerForChannel:0]+50)/50;//同上  
  47.     [_proV setProgress:pead animated:YES];  
  48.     [_proV2 setProgress:ave animated:YES];  
  49. }  
  50.   
  51. //进度  
  52. - (IBAction)proSlider:(id)sender  
  53. {  
  54.     //当前时间=总时间*slider.value;  
  55.     float curTime = _player.duration*_proSlider.value;  
  56.     [_player setCurrentTime:curTime];  
  57. }  
  58. //声道  
  59. - (IBAction)panSlider:(id)sender  
  60. {  
  61.     _player.pan = _panSlider.value;  
  62. }  
  63. //速度  
  64. - (IBAction)speedSlider:(id)sender  
  65. {  
  66.     _player.rate = _speedSlider.value;  
  67. }  
  68. //音量  
  69. - (IBAction)volSlider:(id)sender  
  70. {  
  71.     _player.volume = _volSlider.value;  
  72. }  
  73.   
  74. @end  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值