iOS -- AVAudioPlayer播放音乐
一. AVAudioPlayer:

声明音乐控件AVAudioPlayer,必须声明为全局属性变量,否则可能不会播放,AVAudioPlayer只能播放本地音乐
//获取音乐文件路径
NSURL *soundUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:_fileName ofType:@"mp3"]]; if (!_BGMPlayer) { _asset = [[AVURLAsset alloc] initWithURL:soundUrl options:nil];
//实例化音乐播放控件
_BGMPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:_asset.URL error:nil];
}
if (_BGMPlayer != nil) {
//缓冲播放
_BGMPlayer.delegate = self;
[_BGMPlayer prepareToPlay];
_BGMPlayer.numberOfLoops = -;
[_BGMPlayer play];
}else{
NSLog(@"初始化失败");
}
二. AVURLAsset
创建一个由URL标识的代表任何资源的asset对象
_asset = [[AVURLAsset alloc] initWithURL:soundUrl options:nil];
//实例化音乐播放控件
_audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:_asset.URL error:nil];
三. AVAudioPlayerDelegate
/* audioPlayerBeginInterruption: is called when the audio session has been interrupted while the player was playing. The player will have been paused. */
//产生中断后调用这个函数,通常暂停播放
- (void)audioPlayerBeginInterruption:(AVAudioPlayer *)player
{
[self.playerpause];
} /* audioPlayerEndInterruption:withOptions: is called when the audio session interruption has ended and this player had been interrupted while playing. */
/* Currently the only flag is AVAudioSessionInterruptionFlags_ShouldResume. */
//恢复中断后调用这个函数,继续播放
- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player withOptions:(NSUInteger)flags
{
[self.playerplay];
} //解码出错后调用这个函数
- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error //播放结束后调用这个函数
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
四. 参考链接
http://www.cnblogs.com/QianChia/p/5771149.html
http://www.jianshu.com/p/cc79c45b4ccf
http://www.360doc.com/content/15/1214/23/20918780_520470043.shtml
https://segmentfault.com/a/1190000004049416?_ea=471318
iOS -- AVAudioPlayer播放音乐的更多相关文章
- iOS音频与视频的开发(一)-使用AVAudioPlayer播放音乐、使用AVPlayerViewController播放视频
iOS的多媒体支持非常强大,它提供了多套支持多媒体的API,无论是音频.视频的播放,还是录制,iOS都提供了多种API支持.借助于这些API的支持,iOS应用既可以查看.播放手机相册中的照片.视频,也 ...
- iOS-----使用AVAudioPlayer播放音乐
使用AVAudioPlayer播放音乐 AVAudioPlayer是一个属于AVFoundation.framework的类.它作用类似于一个功能强大的播放器.AVAudioPlayer支持广泛的音频 ...
- iOS AVAudioPlayer播放音频时声音太小
iOS AVAudioPlayer播放音频时声音太小 //引入AVFoundation类库,设置播放模式就可以了 do { try AVAudioSession.sharedInstance().ov ...
- IOS AVAUDIOPLAYER 播放器使用
1. 导入 AVFoundation.framework 2.导入头文件 #import <AVFoundation/AVFoundation.h> 3. player = [[AVAu ...
- iOS8 用AVAudioPlayer播放音乐(Swift)
AVAudioPlayer 类提供了播放音频文件的功能,在本次教程中,我们将对一个音乐文件进行播放暂停和停止操作,此外还会显示标题和播放时间.本次教程使用iOS8和Xcod6.3.1 打开Xcode创 ...
- iOS后台播放音乐
iOS实现在后台播放音乐 iOS4之后就支持后台播放音频了.只需下面两步就可以实现后台播放音频操作了. 1. 在Info.plist中,添加"Required background mode ...
- AVAudioPlayer播放音乐
1:首先创建一个新的项目,继承自UIViewController 2:导入框架AVFoundation.framework 右键工程名,在Build Phases的Link Binary With L ...
- ios学习:AVAudioPlayer播放音乐文件及读取ipod库中的音乐文件
首先要导入AVFoundation框架及 #import <AVFoundation/AVFoundation.h>头文件 注意:要在真机上调试 下面是ipad上的调试效果 下面是代码,代 ...
- iOS 后台播放音乐
在info.plist文件中添加 下面是后台播放音频的完整测试代码: 引入文件<AVFoundation/AVFoundation.h> //后台播放音频设置 AVAudioSession ...
随机推荐
- [转载]explicit关键字
本文转自http://www.programlife.net/cpp-explicit-keyword.html. 其实explicit主要用于防止隐式转换,用于修饰构造函数.复制构造函数[注意:一般 ...
- RecyclerView 介绍 02 – 重要概念
几个概念 RecyclerView是一个ViewGroup: LayoutManager控制RecyclerView的ChildView的布局显示,childview由Recycler提供以及管理: ...
- js:方法1. 数组
Array.every() array.every(f); array.every(f, o); f(array[i], i, array) [1,2,3].every(function(x) { r ...
- 黑客语(Leet)
黑客语(Leet) Leet是从网络发展起来的一种文字书写方式.通常将英语中的字母替换为数字和特殊符号.这种方式被很多黑客组织所使用.由于具有隐密性,所以它也广泛被用于密码中.使用Leet书写的密 ...
- js兼容方法:事件添加|事件绑定|事件监听 addEvent
function addEvent(obj,sEvent,fn){ if(obj.attachEvent){ obj.attachEvent("on"+sEvent,fn); }e ...
- bean之间的关系:继承、依赖
继承 这里说的继承和java的继承是不一样的,不是父类子类.但思想很相似,是父bean和子bean 1.父bean是一个实例时.它本身是一个完整的bean 2.父bean是模板,抽象bean,不能被 ...
- [转] Spring MVC sample application for downloading files
http://www.codejava.net/frameworks/spring/spring-mvc-sample-application-for-downloading-files n this ...
- DP ZOJ 2745 01-K Code
题目传送门 题意:要求任意连续子序列中0和1的数量差不超过k的方案数 分析:想好状态其实不难.dp[i][j][k]表示考虑前i长度,后缀中最大的 sum(0) - sum(1) = j, sum ( ...
- iOS 'The sandbox is not sync with the Podfile.lock错误
出现以下错误时, diff: /../Podfile.lock: No such file or directory diff: Manifest.lock: No such file or dire ...
- javaEE基础
1.拦截器与过滤器 过滤器(filter),过滤器处于客户端与Web资源(Servlet.JSP.HTML)之间,客户端与Web资源之间的请求和响应都要通过过滤器进行过滤.如过滤编码,IP 拦截器(i ...