现在的各种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 <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.
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的模拟器播放的效果还是非常出色的~

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

iOS开发-音乐播放的更多相关文章

  1. iOS开发-音乐播放(AVAudioPlayer)

    现在的手机的基本上没有人不停音乐的,我们无法想象在一个没有声音的世界里我们会过的怎么样,国内现在的主流的主流网易云音乐,QQ音乐,酷狗,虾米,天天基本上霸占了所有的用户群体,不过并没有妨碍大家对音乐的 ...

  2. iOS开发—音乐的播放

    iOS开发—音乐的播放 一.简单说明 音乐播放用到一个叫做AVAudioPlayer的类,这个类可以用于播放手机本地的音乐文件. 注意: (1)该类(AVAudioPlayer)只能用于播放本地音频. ...

  3. Android 4.3实现类似iOS在音乐播放过程中如果有来电则音乐声音渐小铃声渐大的效果

    目前Android的实现是:有来电时,音乐声音直接停止,铃声直接直接使用设置的铃声音量进行铃声播放. Android 4.3实现类似iOS在音乐播放过程中如果有来电则音乐声音渐小铃声渐大的效果. 如果 ...

  4. iOS开发--音乐文件播放工具类的封装(包含了音效的封装)

    一.头文件 #import <Foundation/Foundation.h> #import <AVFoundation/AVFoundation.h> @interface ...

  5. iOS开发----音频播放、录音、视频播放、拍照、视频录制

    随着移动互联网的发展,如今的手机早已不是打电话.发短信那么简单了,播放音乐.视频.录音.拍照等都是很常用的功能.在iOS中对于多媒体的支持是非常强大的,无论是音视频播放.录制,还是对麦克风.摄像头的操 ...

  6. iOS在线音乐播放SZKAVPlayer(基于AVPlayer的封装)

    由于最近闲着没事,想找有关在线音乐播放的demo学习一下,在gitHub跟code4APP上面查找了很多帖子,结果很多在线音乐都是基于AudioStream实现的,我感觉用起来不太方便.后来突然发现, ...

  7. ios 视频音乐播放

    IOS开发小技巧(视频和音乐播放).IOS视频播放代码(添加MediaPlayer.framework和#import) -(void)playMovie:(NSString *)fileName{ ...

  8. iOS 简单音乐播放器 界面搭建

    如图搭建一个音乐播放器界面,具备以下几个简单功能: 1,界面协调,整洁. 2,点击播放,控制进度条. 3.三收藏歌曲,点击收藏,心形收藏标志颜色加深. 4,左右按钮,切换歌曲图片和标题. 5,点击中间 ...

  9. iOS 调用音乐播放以及视频播放器

    音乐播放 NSString *path = [[NSBundle mainBundle] pathForResource:@"预谋" ofType:@"mp3" ...

随机推荐

  1. eclipse文本编码格式修改为UTF-8

    1.windows->Preferences...打开"首选项"对话框,左侧导航树,导航到general->Workspace,右 侧Text file encodin ...

  2. Visual Studio 2017强制更新方法

    Visual Studio 2017强制更新方法   Visual Studio 2017更新时候,用户都是根据消息提示,进行更新.这样做的好处,就是微软可以分批下发升级包,避免集中更新.不过为了早点 ...

  3. 使用IIS实现反向代理

    IIS的反向代理是通过ARR模块来完成的,ARR模块需要另外安装,而且只能通过Web PlatForm Installer安装.关于安装来源与步骤,帖子已有很多,不做描述.启用“Application ...

  4. wikioi 1048 石子归并

    dp[i][j]=min(dp[i][j],dp[i][k],dp[k+1][j]+sum[i][j]); 表示i-j的最小合并代价. #include <iostream> #inclu ...

  5. SQL 死锁进程查询

    use master go declare @spid int,@bl int DECLARE s_cur CURSOR FOR ,blocked ) a ) b where a.blocked=sp ...

  6. jsoncpp 0.5 ARM移植

    1.下载jsonapp 0.5 https://github.com/open-source-parsers/jsoncpp 2.编译 platform 没有包含 arm 平台,需要把源码提取出来,独 ...

  7. HDU 4709 Herding (枚举)

    Herding Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  8. java对mongodb的and, in, or 经常使用操作

    DBCollection dbcon = null; DBObject query = new BasicDBObject(); BasicDBList values = new BasicDBLis ...

  9. Linux下open与fopen的区别

    int open(const char *path, int access,int mode)    path 要打开的文件路径和名称   access 访问模式,宏定义和含义如下:          ...

  10. Android学习网站(1)

    收集了一些比较好的Android学习网站,希望对大家有所帮助: 1.http://developer.android.com/ Android官方网站,可惜被屏蔽了,需要使用FQ软件 2.http:/ ...