播放音频文件ios
来源:互联网 发布:农村淘宝报名入口 编辑:程序博客网 时间:2024/05/19 05:04
There are many ways to play audio on the Mac – System Sound Services, AVAudioPlayer, Audio Queue Services, and OpenAL. Without outside support libraries, the two easiest ways by far are System Sound Services and AVAudioPlayer – so let’s talk about when you would (and wouldn’t) want to use those, and how you can use them.
System Sound Services
System Sound Services provides an extremely easy way to play audio files. All you have to do is the following:
NSString *pewPewPath = [[NSBundle mainBundle] pathForResource:@"pew-pew-lei" ofType:@"caf"];NSURL *pewPewURL = [NSURL fileURLWithPath:pewPewPath];AudioServicesCreateSystemSoundID((CFURLRef)pewPewURL, &_pewPewSound);AudioServicesPlaySystemSound(_pewPewSound);
Doesn’t get much easier than that. However there are some strong drawbacks to this method:
- It only supports audio data formats linear PCM or IMA4.
- It only supports audio file formats CAF, AIF, or WAV.
- The sounds must be 30 seconds or less in length.
- And more – see the iPhone Application Programming Guide, page 149.
AVAudioPlayer
So what if you have an audio file encoded with AAC or MP3 that you want to play as background music? Another easy way to play music is via the AVAudioPlayer class. For the most part, it again is quite simple:
NSError *error;_backgroundMusicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:backgroundMusicURL error:&error];[_backgroundMusicPlayer prepareToPlay];[_backgroundMusicPlayer play];
However, the drawback of AVAudioPlayer is it is extremely slow. If you tap a button and try to trigger a sound with AVAudioPlayer, there will be a noticeable small delay. But if that doesn’t matter to you (like for starting up background music), AVAudioPlayer is a fine choice.
And there are a couple other things to keep in mind:
- If you’re playing background music, you should check to see if other audio (like the iPod) is playing first, so you don’t have two layers of music going on at once!
- If a phone call arrives and the user chooses “Decline”, by default your AVAudioPlayer will stop. You can start it back up again by registering for the AVAudioPlayerDelegate and starting the music back up again in the audioPlayerEndInterruption method.
Sample Code
I put together some sample code showing how to use System Sound Services and AVAudioPlayer that you might want to check out. Not only does it demonstrate those APIs, it has some mighty funky beats and a cool spaceship to boot. Pew-pew!
OpenAL
If you’re writing a game or another app where you want fine grained control of audio with low latency, you probably don’t want to use the above methods. Instead, you might want to useOpenAL, a cross-platform audio library supported by the iPhone.
OpenAL can be a beast with a steep learning curve. Luckily, Alex Restrepo has extendeda great example by gehacktes.net and made a nice Sound Engine library using OpenAL that you can either use in your projects or use as a reference.
Another option is the Cocos2D game library includes an extremely easy to use sound engine that makes playing audio a snap. You can learn how to use it in my tutorial onHow To Make a Simple iPhone Game With Cocos2D.
- 播放音频文件ios
- ios播放音频文件
- ios播放音频文件
- iOS音频文件播放切换扬声器
- IOS使用OpenAL播放音频文件
- iOS扫描本地音频文件及后台播放
- iOS 用 AVPlayer 播放一个本地音频文件
- 播放音频文件
- 播放音频文件
- iOS利用AudioQueue播放音频文件(mp3,aac,caf,wav等)
- iOS Dev (20) 用 AVAudioPlayer 播放一个本地音频文件
- iOS Dev (21) 用 AVPlayer 播放一个本地音频文件
- IOS MPMoviePlayerViewController 获取当前音频文件播放时间点
- IOS-播放沙盒目录及本地音频文件
- IOS音频6:之基于FFmpeg播放音频文件(一)
- IOS音频7:之基于FFmpeg播放音频文件(二)
- iOS开发:获取一个视频、音频文件的播放时长
- 详解iOS App中调用AVAudioPlayer播放音频文件的用法
- 网页信息采集工具
- win7下eclipse搭建android开发开台遇到的几个问题
- 生存方式与选择
- Android Service介紹
- Android 压缩解压zip文件
- 播放音频文件ios
- 自组织团队建设很容易吗? (问题与对策的思考)
- caf录音
- 冒泡排序算法:
- HashSet中为什么不允许放重复的值
- web页面的键盘响应
- hdoj1036简单题、格式化输出
- hdoj1021简单题、找规律
- hdoj1020简单的字符串处理