Saturday, April 20, 2024

Using an AVAudio Session Delegate to Playing Audio With AV Audio Player In Iphone

Share

The AVAudioSessionDelegate protocol allows the object that you have configured as a delegate to respond to specific events related to the AVAudioSession. Specifically, it enables you to be notified when your application audio has been interrupted, for example, during a phone call, or when certain audio parameters have been changed. For example, you can be notified if the sample rate has been changed or if the availability of an audio input device has changed. Thus, if you can imagine an application that records audio (which requires audio input), that application might want to be notified if the user unplugs her microphone.

How To Playing Audio With AV Audio Player In Iphone

So you’ve set your audio category, you’ve configured your sample rate, and now you’re ready to

actually play some audio. To do this, you use the class AVAudioPlayer. The AVAudioPlayer class is provided for the purposes of playing any audio that does not require stereo positioning, precise synchronization with UI elements, or audio captured from a network stream. For these purposes, you should probably use something else, such as OpenAL. In other words, the AVAudioPlayer class is good for lightweight audio such as that used in the most basic applications. You could probably use the AVAudioPlayer class even in some game applications, but OpenAL is more suited to things such as high-performance immersive games, where the audio positioning helps to enhance the game experience and must be changed dynamically and programmatically. The AVAudioPlayer class is suitable for most other applications.

To create an AVAudioPlayer instance, you simply call the constructors initWith ContentsOf URL:error: or initWithData:error:, which enable you to initialize the object using either a file or data already existing in memory. If you want to play multiple files simultaneously and have their audio mixed together, you simply instantiate multiple instances of the AVAudioPlayer class and use them individually. Once the AVAudioPlayer instance is created, to play the audio you simply call the play method. Similarly, to pause or stop the audio, you call the pause or stop methods. You can also configure whether you want the audio to loop a certain number of times by setting the number OfLoops property. To cause the audio to loop indefinitely, set the numberOfLoops property to any negative number. The default value of this property, which is zero, causes the audio to play once and then stop.

In some cases, you may want to minimize the amount of delay between calling the play method

and when the audio actually begins to play. To do this, you can call the prepareToPlay method on your AVAudioPlayer object. This tells the AVAudioPlayer object to preload its caches and buffers so that it is ready to play when the play method is called. When the sound stops playing, either by using the stop method or by allowing the audio to complete, the buffers are once again empty. So if you want the audio to play again with a minimal amount of delay, you should call prepareToPlay again before calling the play method.

Listing 25.2 shows how you load and play an audio file from within your application bundle. In this case, you’re going to play the audio file continually until you tell it to stop. Therefore, you’re going to set the numberOfLoops property to a negative value. In this code, you initially get the actual path to the filename out of your main bundle. You then convert that path into a URL. Finally, you initialize your AVAudioPlayer with that URL, set the number of loops to −1, set the delegate to self, and call the play method on the player. At this point, the audio begins playing.

During playback, you can find out certain information about the audio file, such as your current

location in the file by time using the currentTime property, as well as the total duration of the file using the duration property.

BIO:

Sussan Deyhim is from Bookmarking Demon and a University Lecturer. She loves to write on technical topics like Bookmarking Demon coupon. She is writing from last three years

Read more

More News