一. 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播放音乐的更多相关文章

  1. iOS音频与视频的开发(一)-使用AVAudioPlayer播放音乐、使用AVPlayerViewController播放视频

    iOS的多媒体支持非常强大,它提供了多套支持多媒体的API,无论是音频.视频的播放,还是录制,iOS都提供了多种API支持.借助于这些API的支持,iOS应用既可以查看.播放手机相册中的照片.视频,也 ...

  2. iOS-----使用AVAudioPlayer播放音乐

    使用AVAudioPlayer播放音乐 AVAudioPlayer是一个属于AVFoundation.framework的类.它作用类似于一个功能强大的播放器.AVAudioPlayer支持广泛的音频 ...

  3. iOS AVAudioPlayer播放音频时声音太小

    iOS AVAudioPlayer播放音频时声音太小 //引入AVFoundation类库,设置播放模式就可以了 do { try AVAudioSession.sharedInstance().ov ...

  4. IOS AVAUDIOPLAYER 播放器使用

    1. 导入 AVFoundation.framework 2.导入头文件  #import <AVFoundation/AVFoundation.h> 3. player = [[AVAu ...

  5. iOS8 用AVAudioPlayer播放音乐(Swift)

    AVAudioPlayer 类提供了播放音频文件的功能,在本次教程中,我们将对一个音乐文件进行播放暂停和停止操作,此外还会显示标题和播放时间.本次教程使用iOS8和Xcod6.3.1 打开Xcode创 ...

  6. iOS后台播放音乐

    iOS实现在后台播放音乐 iOS4之后就支持后台播放音频了.只需下面两步就可以实现后台播放音频操作了. 1. 在Info.plist中,添加"Required background mode ...

  7. AVAudioPlayer播放音乐

    1:首先创建一个新的项目,继承自UIViewController 2:导入框架AVFoundation.framework 右键工程名,在Build Phases的Link Binary With L ...

  8. ios学习:AVAudioPlayer播放音乐文件及读取ipod库中的音乐文件

    首先要导入AVFoundation框架及 #import <AVFoundation/AVFoundation.h>头文件 注意:要在真机上调试 下面是ipad上的调试效果 下面是代码,代 ...

  9. iOS 后台播放音乐

    在info.plist文件中添加 下面是后台播放音频的完整测试代码: 引入文件<AVFoundation/AVFoundation.h> //后台播放音频设置 AVAudioSession ...

随机推荐

  1. Swift3.0语言教程分割字符串与截取字符串

    Swift3.0语言教程分割字符串与截取字符串 Swift3.0语言教程分割字符串 如果想要快速的创建一个数组,我们可以将字符串进行分割,分割后的内容将会生成一个数组.在NSString中有两个分割字 ...

  2. yii2.0 的数据的 增

    增加数据 /**     * 添加数据  controller 层     */ //引入对应的model类 use app\models\About; //定义对应的方法固定的actionxxxx ...

  3. DIjkstra(反向边) POJ 3268 Silver Cow Party || POJ 1511 Invitation Cards

    题目传送门 1 2 题意:有向图,所有点先走到x点,在从x点返回,问其中最大的某点最短路程 分析:对图正反都跑一次最短路,开两个数组记录x到其余点的距离,这样就能求出来的最短路以及回去的最短路. PO ...

  4. HTML5中lineCap端点样式遇到closePath()

    定义和用法 lineCap 属性设置或返回线条末端线帽的样式. 注释:"round" 和 "square" 会使线条略微变长. 默认值: butt JavaSc ...

  5. node.js第一次

    随着时代的变迁,日月星辰轮回,不断的有新的事物被创造于世,作为在这个世界活着的前端工程崽的我,最近又接触了一门新手艺“node.js”.自从它2009年诞生至今,被很多前端推崇,我起步已经晚了,还好它 ...

  6. .net平台下垃圾回收机制

    引言:使用c++进行编程,内存的处理绝对是让每个程序设计者最头疼的一块了.但是对于.net平台下使用c#语言开发系统,内存管理可以说已经不算是问题了.在.net平台下CLR负责管理内存,CLR中的垃圾 ...

  7. Intellij IDEA tomcat相关

    可能遇到的问题: tomcat7启动报错  java.lang.NoSuchMethodException: org.apache.catalina.deploy.WebXml addFilter h ...

  8. Eclipse: Resource is out of sync with the file system when publishing to tomcat server on Eclipse

    Link: http://stackoverflow.com/questions/4343735/avoiding-resource-is-out-of-sync-with-the-filesyste ...

  9. HTML5 的 applicationCache 应用程序缓存离线存储功能与 manifest 文件

    一. 实现 HTML5 applicationCache 的步骤 一般的操作步骤 1. 新建 manifest 文件 如文件名为  lzwme.manifest,内容配置参考如下: 01 CACHE ...

  10. Coder-Strike 2014 - Finals (online edition, Div. 2) A. Pasha and Hamsters

    水题 #include <iostream> #include <vector> #include <algorithm> using namespace std; ...