博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS开发-音乐播放
阅读量:6653 次
发布时间:2019-06-25

本文共 3299 字,大约阅读时间需要 10 分钟。

现在的各种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.    self.musicArr=@[@"潮湿的心.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 
@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. self.musicArr=@[@"潮湿的心.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的模拟器播放的效果还是非常出色的~

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

转载地址:http://abjto.baihongyu.com/

你可能感兴趣的文章
java导出Excel工具类
查看>>
malloc calloc realloc,new区别联系以及什么时候用
查看>>
用ES6巧妙的解决传统面试中的算法小问题!
查看>>
php socket 编程(一)
查看>>
SDUT 简单枚举类型——植物与颜色
查看>>
Windows 下配置Git
查看>>
HTML5触摸事件(touchstart、touchmove和touchend)
查看>>
Vue项目中的mock数据
查看>>
关于Scott用户
查看>>
web.py框架之i18n支持
查看>>
PHP cURL
查看>>
Python 5 面向对象进阶
查看>>
PHP中的验证码类(验证码功能设计之二)
查看>>
迭代器
查看>>
移动互联网企业“六步”轻松实现移动安全战略部署!
查看>>
MySQL存储过程-遍历游标的例子
查看>>
Algs4-1.4.24扔鸡蛋
查看>>
对象转换成XML数据形式
查看>>
Linux下的压缩zip,解压缩unzip命令详解及实例
查看>>
工作周记 - 第三周 (2016/06/06 - 2016/06/8) 端午快乐
查看>>