IPhone-developed audio 101 (part 3): Program-based audio playback

Source: Internet
Author: User

IPhoneAudio 101 for Development (part 3 ):ProgramAudio Playback

Translator: Xia laiyou

 

Disclaimer (read only !) : The original translations of all tutorials provided by this blog are from the Internet and are only for learning and communication purposes. Do not conduct commercial communications. At the same time, do not remove this statement when reprinting. In the event of any dispute, it has nothing to do with the owner of this blog and the person who published the translation. Thank you for your cooperation!

 

Original article URL:

Http://www.raywenderlich.com/259/audio-101-for-iphone-developers-playing-audio-programmatically

 

This articleArticleThis is the third article about the audio series. If you still need to know little about audio, You can first take a look at the first and second tutorials in this series.

So far, we have discussed the differences between file formats and data formats in this series, and how to convert and record audio files under MAC. Now we will make an interesting part-playing audio on your iPhone!

There are many ways to play audio on Mac-system sound services, avaudioplayer, audio queue services, and openal. If no external library file is supported, the simplest method is system sound serveices and avaudioplayer. So let's discuss when you can use them, when you can't use them, and how to use them.

 

 

System sound services

System sound serverice provides a simple method to play audio files. All you need to do is:

 
Nsstring * pewpewpath = [[nsbundle mainbundle] pathforresource: @ "pew-lei" oftype: @ "caf"]; nsurl * pewpewurl = [nsurl fileurlwithpath: pewpewpath]; audioservicescreatesystemsoundid (cfurlref) pewpewurl, & _ pewpewsound); audioservicesplaysystemsound (_ pewpewsound );

 

 

Note that you need to add audiotoolbox to use the above functions. and when converting from the file path (nsstring) to the URL (nsurl), use fileurlwithpath, otherwise it may return null.

All of the above are simple. However, this method has some disadvantages:

    • It only supports the linear PCM or ima4.
    • It only supports the format of audio files: CAF, AIF, or WAV.
    • The sound length must be less than 30 seconds.
    • For more information, see iPhone application programming guide, page 149.

Avaudioplayer

Therefore, if you want to use an AAC or mp3 file as the background music. Another simple method is to use the avaudioplayer class to play music. The most important thing is that it is actually very simple:

 
Nserror * error; _ backgroundmusicplayer = [[avaudioplayer alloc] callback: backgroundmusicurl error: & error]; [_ backgroundmusicplayer preparetoplay]; [_ backgroundmusicplayer play];

 

 
 

However, the disadvantage of avaudioplayer is that it is quite slow (not very slow, but a little slow compared to other methods ). If you click a button and try to use avaudioplayer to play the sound, there will be a completely perceptible latency. But if that doesn't affect you (like playing background music), avaudioplayer is a good choice.

Here are two suggestions:

    1. If you want to play background music, you should check whether other audio (such as Ipod) is playing, so do not let the two music play simultaneously.
    2. If a phone call is called, the user selects "reject". By default, your avaudioplayer will stop. You can register avaudioplayerdelegate and use audioplayendinterruption to Resume playback.


InstanceCode

I put system sound services and avaudioplayer together for a demo. You can check it. This demo not only demonstrates these APIs, but also provides some interesting results.

 

Openal

If you are writing a game or application and want to use the underlying technology to manipulate the audio, you may not want to use the above method. You may need to use openal, a cross-platform Audio Library Supported by the iPhone.

It is difficult to learn about openal. Fortunately, Alex Restrepo has extended a beautiful sound engine that can use openal. You can easily use it in your project or as a reference.

In addition, you can choose an easy-to-use sound engine included in the cocos2d game engine to play audio. You can learn how to use it, through how to make a simple iPhone game with cocos2d ., if it is a little difficult to read a foreign language, it doesn't matter. I have also translated this article for you, the son of the Dragon Mountain who I learned and admired.

 

Conclusion

I only talked about the audio programming on the iPhone. If you are very interested, I suggest reading Apple's documents, especially the core audio overview and audio session programming guide. You can also specialize in openal knowledge.

 

I hope this series of tutorials will be helpful for developers who are new to audio concepts.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.