iOS 播放音乐

我们同样使用苹果提供的框架 AVFoundation.framework

首先,新建项目

给项目起名: TestAVGoundation

接下来导入framework 

以上界面如何打开,参照上一节  http://blog.csdn.net/lwjok2007/article/details/50419327

导入成功之后如下

项目结构

开始写代码之前,我们找一首歌曲放到项目中

这里我们放一首比较经典的歌曲 周华健的 朋友

同样我们还是打开项目默认生成的ViewController.m 在里面添加播放功能

首先,导入头文件

[objc]  view plain  copy
  1. #import <AVFoundation/AVFoundation.h>  
接下来,创建个控件

[objc]  view plain  copy
  1. @property (nonatomic,strongAVAudioPlayer *audioPlayer;//播放器  
  2. @property (strongnonatomicUIProgressView *playProgress;//播放进度  
  3. @property (strongnonatomicUIButton *playOrPause; //播放/暂停按钮(如果tag为0认为是暂停状态,1是播放状态)  
  4.   
  5. @property (strong ,nonatomicNSTimer *timer;//进度更新定时器  

初始化界面

[objc]  view plain  copy
  1. - (void)viewDidLoad {  
  2.     [super viewDidLoad];  
  3.     // Do any additional setup after loading the view, typically from a nib.  
  4.     self.view.backgroundColor=[UIColor lightGrayColor];  
  5.     [self initUserFace];  
  6.       
  7. }  
  8.   
  9. -(void)initUserFace{  
  10.       
  11.     //添加playProgress  
  12.       
  13.     _playProgress= [[UIProgressView alloc] initWithProgressViewStyle: UIProgressViewStyleDefault];  
  14.       
  15.     _playProgress.frame=CGRectMake(0100self.view.bounds.size.width36);  
  16.       
  17.     [self.view addSubview:_playProgress];  
  18.       
  19.     //添加播放按钮  
  20.     _playOrPause=[[UIButton alloc]initWithFrame:CGRectMake(015012036)];  
  21.     [_playOrPause setTitle:@"播放" forState:UIControlStateNormal];  
  22.     [_playOrPause setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];  
  23.     [_playOrPause addTarget:self action:@selector(playOrPauseAct:) forControlEvents:UIControlEventTouchUpInside];  
  24.     [self.view addSubview:_playOrPause];  
  25.       
  26. }  

添加几个播放,暂停,修改歌曲进度条显示的方法

[objc]  view plain  copy
  1. -(NSTimer *)timer{  
  2.     if (!_timer) {  
  3.         _timer=[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(updateProgress) userInfo:nil repeats:true];  
  4.     }  
  5.     return _timer;  
  6. }  
  7.   
  8. -(AVAudioPlayer *)audioPlayer{  
  9.     if (!_audioPlayer) {  
  10.         NSString *urlStr=[[NSBundle mainBundle]pathForResource:@"朋友.mp3" ofType:nil];  
  11.         NSURL *url=[NSURL fileURLWithPath:urlStr];  
  12.         NSError *error=nil;  
  13.         //初始化播放器,注意这里的Url参数只能时文件路径,不支持HTTP Url  
  14.         _audioPlayer=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:&error];  
  15.         //设置播放器属性  
  16.         _audioPlayer.numberOfLoops=0;//设置为0不循环  
  17.         _audioPlayer.delegate=self;  
  18.         [_audioPlayer prepareToPlay];//加载音频文件到缓存  
  19.         if(error){  
  20.             NSLog(@"初始化播放器过程发生错误,错误信息:%@",error.localizedDescription);  
  21.             return nil;  
  22.         }  
  23.     }  
  24.     return _audioPlayer;  
  25. }  
  26.   
  27.   
  28. /** 
  29.  *  播放音频 
  30.  */  
  31. -(void)play{  
  32.     if (![self.audioPlayer isPlaying]) {  
  33.         [self.audioPlayer play];  
  34.         self.timer.fireDate=[NSDate distantPast];//恢复定时器  
  35.     }  
  36. }  
  37.   
  38. /** 
  39.  *  暂停播放 
  40.  */  
  41. -(void)pause{  
  42.     if ([self.audioPlayer isPlaying]) {  
  43.         [self.audioPlayer pause];  
  44.         self.timer.fireDate=[NSDate distantFuture];//暂停定时器,注意不能调用invalidate方法,此方法会取消,之后无法恢复  
  45.           
  46.     }  
  47. }  
  48.   
  49. /** 
  50.  *  更新播放进度 
  51.  */  
  52. -(void)updateProgress{  
  53.     float progress= self.audioPlayer.currentTime /self.audioPlayer.duration;  
  54.     [self.playProgress setProgress:progress animated:true];  
  55. }  
  56.   
  57. #pragma mark - 播放器代理方法  
  58. -(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{  
  59.     NSLog(@"音乐播放完成...");  
  60.       
  61.     [_playOrPause setTitle:@"播放" forState:UIControlStateNormal];  
  62.       
  63. }  

我们给播放按钮添加点击事件

[objc]  view plain  copy
  1. -(void)playOrPauseAct:(UIButton *)sender{  
  2.     NSString *strPlay=sender.titleLabel.text;  
  3.     NSLog(@"strPlay=%@",strPlay);  
  4.     if ([strPlay isEqualToString:@"播放"]) {  
  5.         [sender setTitle:@"暂停" forState:UIControlStateNormal];  
  6.         [self play];  
  7.     }else{  
  8.         [sender setTitle:@"播放" forState:UIControlStateNormal];  
  9.         [self pause];  
  10.     }  
  11. }  

好了,到此 我们创建完成 可以运行试试


仔细的朋友可能发现我们的app播放音乐的过程中 如果切换到后台之后发现音乐暂停了  再次打开 又接着播放了

如果想要后台 也可以接着播放音乐 我们需要修改两个地方

1,打开项目 plist 文件

添加一项


2,打开ViewController.m 找到如下方法 添加一段

好了 试下后台运行
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值