IOS development: avaudioplayer usage

Source: Internet
Author: User

Avaudioplayer is the most basic audio player class in avfoundation. framework. It is similar to mpmediaplayercontroller in some basic operation functions, such as play, pause, and volume.

The difference is that avaudioplayer does not have a queue. That is to say, it can only play audio in a specified path. To play multiple audios, you can create multiple avaudioplayers. In avfoundation, there is another powerful avqueueplayer class. Basically, it depends on the music player. What QQ music, cool dog should all use avqueueplayer. The use of avqueueplayer will be described in the following blog.

So what is avaudioplayer used?

Avaudioplayer can implement functions similar to system audio services to play a single audio. However, avaudioplayer can control the playback times and playback time. For example, you can use it to easily achieve repeated playback. In addition, it also has a very powerful function, that is, it can easily adjust the volume of the left and right audio channels, so as to achieve a cool stereo effect. From this perspective, avaudioplayer is suitable for playing audio in the game. For example, when shuffling a card game, you can repeat the audio during shuffling until the shuffling ends.

The following describes how to use avaudioplayer.


Step 1: Set audio session

Remember, you must first set audio session unless you use the default settings to play audio using avfoundation. For more information about audio session settings, see the previous blog.

The following is an example:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];


Step 2: Initialize avaudioplayer

Sample Code:

Nsurl * musiconeurl = [[nsbundle mainbundle] urlforresource: @ "" withextension: @ "MP3"]; self. audioplayerone = [[avaudioplayer alloc] handler: musiconeurl error: Nil];

Step 3: Set avaudioplayer

Avaudioplayer has an important property: Pan, which can be used to adjust the size of the left and right audio channels during audio playback. If the value is-1.0, the left audio channel is used completely, if it is 1.0, It is the full right-channel pronunciation.

In addition, a powerful function is to measure the power of the real-time audio channels during audio playback. This function can be used to aid in the display of sound waves, which are available in many players on computers. Of course, if you use it for acoustic analysis, I think it is also possible.

Sample Code:

// Audioplayerone is an avaudioplayer object self. audioplayerone. meteringenabled = yes; // allows measurement [self. audioplayerone updatemeters]; // update data [self. averagepowerchannelone setvalue: [self. audioplayerone averagepowerforchannel: 0] animated: Yes]; // assign the obtained data to the related control [self. averagepowerchanneltwo setvalue: [self. audioplayerone averagepowerforchannel: 1] animated: Yes];


Step 4: Control

Sample Code:

[Self. audioplayerone preparetoplay]; // prepare data. You must set [self. audioplayerone setnumberofloops:-1]; // you can set it to-1 to enable infinite loop playback [self. audioplayerone setrate: 1.5f]; // sets the playback speed [self. audioplayerone setpan: 1.0f]; // you can specify the left-right listener [self. audioplayerone setcurrenttime: 20366f]; // sets the playback time [self. audioplayerone play]; // playback [self. audioplayerone pause]; // pause [self. audioplayerone stop]; // stop


Step 5: Delegate

You can set the avaudioplayer delegate as needed, so that you can perform related operations at the end of the playback.

Sample Code:

// Complete playback-(void) audioplayerdidfinishplaying :( avaudioplayer *) player successfully :( bool) Flag {If (FLAG) {nslog (@ "play succeed ");}} // After the playback interruption ends, for example, the disconnection caused by a sudden call-(void) audioplayerendinterruption :( avaudioplayer *) player witexceptions :( nsuinteger) flags {}


The above is about the use of avaudioplayer.


[This article is an original article with all rights reserved. If you need to reprint it, please indicate the source. Thank you for your cooperation.]




Related Article

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.