现在的各种App大行其道,其实常用也就是围绕着吃喝玩乐基本的需求,视频,音乐在智能手机出现之前更是必不可少的功能,每个手机都会有一个自带的音乐播放器,当然公众也有自己的需求所以也就造就了各种音乐播放软件,自己下午闲来无事简单的写了一个随机播放音乐的Demo,iOS中有三种播放音频的方式AVAudioPlayer、音频服务、音频队列。另外两种暂时没有用到,就简单的练手了一下AVAudioPlayer,还是开始正题吧;
1.新建项目或者在原有项目重新弄一个页面,先看页面:
2.导入几首自己喜欢的歌曲:
3.导入AVFoundation/AVFoundation.h,对四个按钮进行事件操作,一个AVAudioPlayer只能对应一个URL,因此播放其他歌曲的时候需要情况一下;
定义两个成员变量,并且初始化成员变量:
@interface MusicViewController () @property (nonatomic,strong)AVAudioPlayer *player; @property (nonatomic,strong)NSArray *musicArr; @end
viewDidLoad实例化数组:
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [email protected][@"潮湿的心.mp3",@"爱拼才会赢.mp3",@"给我一个理由忘记.mp3"]; [self prepareMusic:self.musicArr[1]]; } - (void)prepareMusic:(NSString *)path{ //1.音频文件的url路径 NSURL *url=[[NSBundle mainBundle]URLForResource:path withExtension:Nil]; //2.实例化播放器 _player=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:Nil]; //3.缓冲 [_player prepareToPlay]; }
4.四个对应事件的代码:
随机:
- (IBAction)random:(id)sender { [self prepareMusic:self.musicArr[arc4random()%3]]; [_player play]; }
播放:
- (IBAction)play:(id)sender { //播放 [_player play]; }
暂停:
- (IBAction)pause:(id)sender { //暂停 [_player pause]; }
停止:
- (IBAction)stop:(id)sender { //停止 [_player stop]; }
5.设置循环次数,开始播放时间,设置音量
//设置音量 [_player setVolume:0.6]; //设置当前播放事件 [_player setCurrentTime:60]; //设置循环次数 [_player setNumberOfLoops:2];
MusicViewController.m中的代码:
// // MusicViewController.m // MyPicture // // Created by keso on 15/1/17. // Copyright (c) 2015年 keso. All rights reserved. // #import "MusicViewController.h" #import <AVFoundation/AVFoundation.h> @interface MusicViewController () @property (nonatomic,strong)AVAudioPlayer *player; @property (nonatomic,strong)NSArray *musicArr; @end @implementation MusicViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [email protected][@"潮湿的心.mp3",@"爱拼才会赢.mp3",@"给我一个理由忘记.mp3"]; [self prepareMusic:self.musicArr[1]]; } - (void)prepareMusic:(NSString *)path{ //1.音频文件的url路径 NSURL *url=[[NSBundle mainBundle]URLForResource:path withExtension:Nil]; //2.实例化播放器 _player=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:Nil]; //3.缓冲 [_player prepareToPlay]; //设置音量 [_player setVolume:0.6]; //设置当前播放事件 [_player setCurrentTime:60]; //设置循环次数 [_player setNumberOfLoops:2]; } - (IBAction)random:(id)sender { [self prepareMusic:self.musicArr[arc4random()%3]]; [_player play]; } - (IBAction)play:(id)sender { //播放 [_player play]; } - (IBAction)stop:(id)sender { //停止 [_player stop]; } - (IBAction)pause:(id)sender { //暂停 [_player pause]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end
其实需要设置还有很多,播放出现异常,或者被更高级别的系统任务打断,可以通过设置相应的委托处理对应的的情形,Demo很小,iOS很多东西都是这样,概念很多,调用的时候根本都不需要写几行代码,iOS的模拟器播放的效果还是非常出色的~
由于是播放音乐,无法模拟效果,大概试验一下,应该没有什么问题~
时间: 2024-10-12 16:56:23