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 ...
随机推荐
- LR检查点的if (status == LR_FAIL)写法
C语言例子:登录失败返回一个信息 int status = web_url("Login", "URL=https://secure.computing.com//log ...
- Java Math floor round ceil 函数
public final class Math extends Object public static double floor(double a) public static long round ...
- 把所有css放到一个css文件的格式
@charset "utf-8"; @import url("global.css"); @import url("common.css") ...
- HYSBZ 1588 营业额统计
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1588 题意:详见题面,中文 思路:平衡树的模板题. 可用Treap,Splay,Scape ...
- wpf 触发器,属性触发器,事件触发器,事件触发器。
<EventTrigger RoutedEvent="Mouse.MouseEnter"/> <DataTrigger Binding="{Bindin ...
- MFC 程序以管理员权限运行
首先,VS打开项目的属性 然后设置如图: 转载自:http://www.cnblogs.com/zzuhjf/archive/2012/09/12/2681548.html
- C# 字符串格式化
1.格式化货币(跟系统的环境有关,中文系统默认格式化人民币,英文系统格式化美元) string.Format("{0:C}",0.2) 结果为:¥0.20 (英文操作系统结果:$0 ...
- 编程之路 - 写给打算进入IT行业的新人们
IT=挨踢,这是IT人的自嘲,进入IT行业是有四五年了,也算得上是一个“老人”了吧,见了不少新人,面试了不少新人,也带了一些新人,多多少少还是有点发言权的. 关于书本 新人们常常会说我看了多少多少的书 ...
- BZOJ1481 : Navigation Game
设$f[i][j][k]$表示从最后一行某个$H$走到$(i,j)$且在第$i$行只经过了$(i,j)$,途中经过了$k$次$F$的最小代价. $A[i][j][k]$表示从下一行$\leq i$的某 ...
- hive0.12 rcfile gzip 测试
创建test_rc; 让后从老数据中插入test_rc中, select test_rc 中插入的数据,报如下错误: Failed with exception java.io.IOException ...