iOS开发-音乐播放

十度 IOS 2015年12月01日 收藏

现在的各种App大行其道,其实常用也就是围绕着吃喝玩乐基本的需求,视频,音乐在智能手机出现之前更是必不可少的功能,每个手机都会有一个自带的音乐播放器,当然公众也有自己的需求所以也就造就了各种音乐播放软件,自己下午闲来无事简单的写了一个随机播放音乐的Demo,iOS中有三种播放音频的方式AVAudioPlayer、音频服务、音频队列。另外两种暂时没有用到,就简单的练手了一下AVAudioPlayer,还是开始正题吧;

1.新建项目或者在原有项目重新弄一个页面,先看页面:

 

2.导入几首自己喜欢的歌曲:

3.导入AVFoundation/AVFoundation.h,对四个按钮进行事件操作,一个AVAudioPlayer只能对应一个URL,因此播放其他歌曲的时候需要情况一下;

定义两个成员变量,并且初始化成员变量:

  1. @interface MusicViewController ()
  2. @property (nonatomic,strong)AVAudioPlayer *player;
  3. @property (nonatomic,strong)NSArray *musicArr;
  4. @end

 viewDidLoad实例化数组:

  1. - (void)viewDidLoad {
  2. [super viewDidLoad];
  3. // Do any additional setup after loading the view.
  4. self.musicArr=@[@"潮湿的心.mp3",@"爱拼才会赢.mp3",@"给我一个理由忘记.mp3"];
  5. [self prepareMusic:self.musicArr[1]];
  6. }
  7. - (void)prepareMusic:(NSString *)path{
  8. //1.音频文件的url路径
  9. NSURL *url=[[NSBundle mainBundle]URLForResource:path withExtension:Nil];
  10. //2.实例化播放器
  11. _player=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:Nil];
  12. //3.缓冲
  13. [_player prepareToPlay];
  14. }

4.四个对应事件的代码:

随机:

  1. - (IBAction)random:(id)sender {
  2. [self prepareMusic:self.musicArr[arc4random()%3]];
  3. [_player play];
  4. }

播放:

  1. - (IBAction)play:(id)sender {
  2. //播放
  3. [_player play];
  4. }

暂停:

  1. - (IBAction)pause:(id)sender {
  2. //暂停
  3. [_player pause];
  4. }

停止:

  1. - (IBAction)stop:(id)sender {
  2. //停止
  3. [_player stop];
  4. }

5.设置循环次数,开始播放时间,设置音量

  1. //设置音量
  2. [_player setVolume:0.6];
  3. //设置当前播放事件
  4. [_player setCurrentTime:60];
  5. //设置循环次数
  6. [_player setNumberOfLoops:2];

MusicViewController.m中的代码:

  1. //
  2. // MusicViewController.m
  3. // MyPicture
  4. //
  5. // Created by keso on 15/1/17.
  6. // Copyright (c) 2015年 keso. All rights reserved.
  7. //
  8.  
  9. #import "MusicViewController.h"
  10. #import <AVFoundation/AVFoundation.h>
  11.  
  12.  
  13. @interface MusicViewController ()
  14. @property (nonatomic,strong)AVAudioPlayer *player;
  15. @property (nonatomic,strong)NSArray *musicArr;
  16. @end
  17.  
  18. @implementation MusicViewController
  19.  
  20. - (void)viewDidLoad {
  21. [super viewDidLoad];
  22. // Do any additional setup after loading the view.
  23. self.musicArr=@[@"潮湿的心.mp3",@"爱拼才会赢.mp3",@"给我一个理由忘记.mp3"];
  24. [self prepareMusic:self.musicArr[1]];
  25. }
  26. - (void)prepareMusic:(NSString *)path{
  27. //1.音频文件的url路径
  28. NSURL *url=[[NSBundle mainBundle]URLForResource:path withExtension:Nil];
  29. //2.实例化播放器
  30. _player=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:Nil];
  31. //3.缓冲
  32. [_player prepareToPlay];
  33. //设置音量
  34. [_player setVolume:0.6];
  35. //设置当前播放事件
  36. [_player setCurrentTime:60];
  37. //设置循环次数
  38. [_player setNumberOfLoops:2];
  39. }
  40. - (IBAction)random:(id)sender {
  41. [self prepareMusic:self.musicArr[arc4random()%3]];
  42. [_player play];
  43. }
  44. - (IBAction)play:(id)sender {
  45. //播放
  46. [_player play];
  47. }
  48.  
  49. - (IBAction)stop:(id)sender {
  50. //停止
  51. [_player stop];
  52. }
  53. - (IBAction)pause:(id)sender {
  54. //暂停
  55. [_player pause];
  56. }
  57.  
  58. - (void)didReceiveMemoryWarning {
  59. [super didReceiveMemoryWarning];
  60. // Dispose of any resources that can be recreated.
  61. }
  62.  
  63. /*
  64. #pragma mark - Navigation
  65.  
  66. // In a storyboard-based application, you will often want to do a little preparation before navigation
  67. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  68. // Get the new view controller using [segue destinationViewController].
  69. // Pass the selected object to the new view controller.
  70. }
  71. */
  72.  
  73. @end

其实需要设置还有很多,播放出现异常,或者被更高级别的系统任务打断,可以通过设置相应的委托处理对应的的情形,Demo很小,iOS很多东西都是这样,概念很多,调用的时候根本都不需要写几行代码,iOS的模拟器播放的效果还是非常出色的~

 由于是播放音乐,无法模拟效果,大概试验一下,应该没有什么问题~