一. 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. java中的三种取整函数

        舍掉小数取整:Math.floor(3.5)=3 四舍五入取整:Math.rint(3.5)=4 进位取整:Math.ceil(3.1)=4

  2. Liferay 6.2 改造系列之七:关闭使用条款确认、密码提醒、新用户强制修改密码等功能

    关闭使用条款确认: 在/portal-master/portal-impl/src/portal.properties配置文件中,有如下配置: # # Set this to true if all ...

  3. SpringMVC解析5-DispatcherServlet逻辑细节

    MultipartContent类型的request处理 对于请求的处理,spring首先考虑的是对于Multipart的处理,如果是MultipartContent类型的request,则转换req ...

  4. css整理-05 边框,背景和浮动,定位

    边框 样式:border-style hidden, dotted, dashed, solid , double, groove, ridge, inset, outset 最不可预测的是doubl ...

  5. 20145223《Java程序程序设计》第7周学习总结

    20145223 <Java程序设计>第7周学习总结 教材学习内容总结 ·时间的度量: (1)格林威治标准时间 (2)世界时 (3)国际原子时 (4)世界协调时间 (5)Unix时间 (6 ...

  6. js三级省市区选择

    <!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. C#,往线程里传参数的方法总结

    Thread (ParameterizedThreadStart) 初始化 Thread 类的新实例,指定允许对象在线程启动时传递给线程的委托.   Thread (ThreadStart) 初始化 ...

  8. 设置随机启动--《用delphi开发共享软件》-15.1任务管理器

    在设置窗体中 chkAutoStart: TCheckBox; 在设置窗体中 chkAutoStart: TCheckBox; procedure TFrmSetup.FormCreate(Sende ...

  9. http://blog.csdn.net/chenleixing/article/details/43740759

    http://blog.csdn.net/chenleixing/article/details/43740759

  10. Redis Java API

    package cn.ac.iscas.pebble.dc.redispool; import java.io.File; import java.io.FileOutputStream; impor ...