Avaudioplayer audio player-iOS development

Source: Internet
Author: User
Tags file url

There are three methods for playing audio in IOS: avaudioplayer, audio service, and audio queue.

This article focuses on avaudioplayer. For the other two, see related articles.

Avaudioplayer is in the avfoundation framework, so we need to import avfoundation. framework.

The avaudioplayer class encapsulates the ability to play a single sound. The player can be initialized using nsurl or nsdata. Note that nsurl is not a network URL but a local file URL, because avaudioplayer does not have the ability to play network audio, however, we can use a small method to make it have this capability, which will be explained later.

One avaudioplayer can only play one audio. If you want to mix audio, you can create multiple avaudioplayer instances, each of which is equivalent to one track on the sound mixing board.

1. Create a player

# Import <avfoundation/avfoundation. h> nserror * err; avaudioplayer * player = [[avaudioplayer alloc] handler: [nsurl fileurlwithpath: [[nsbundle mainbundle] pathforresource: @ "Music" oftype: @ "m4a" indirectory: @ "/"] error: & err]; // create with local URL

Avaudioplayer * player = [[avaudioplayer alloc] initwithdata: mydata error: & err]; // create with nsdata

I have mentioned earlier that avaudioplayer cannot play a network URL, but nsdata can be played. We seem to be inspired that we can create nsdata through a network URL and then play nsdata through avaudioplayer, in this way, can I play online music? However, this method is not desirable, because avaudioplayer can only play a complete file and does not support stream playback. Therefore, it must be played After buffering. So if the network file is too large or the network speed is not enough, will it take a long time? Therefore, we generally use an audio queue to play network audio.

Ii. Player attributes

After creating an avaudioplayer, you can access or set its attributes.

1. Volume

Player. volume = 0.8; // 0.0 ~ Between 1.0

2. Number of cycles

Player. numberofloops = 3; // by default, only playback is performed once.

3. Playback position

Player. currenttime = 15.0; // you can specify any position to start playing.

4. Number of Audio Channels

Nsuinteger channels = player. numberofchannels; // read-only attribute

5. Duration

Nstimeinterval duration = player. dueration; // gets the sampling duration.

6. Meter count

Player. meteringenabled = yes; // enable the meter count function [Player updatemeters]; // update the meter readings // read the average power level and peak level of each sound channel, representing the number of bits per sound channel, the range is-100 ~ Between 0. For (INT I = 0; I <player. numberofchannels; I ++) {float power = [Player averagepowerforchannel: I]; float peak = [Player peakpowerforchannel: I];}

Iii. Playing Sound

After preparing for such a long time, I was so excited that I could play the video.

[PLAYER preparetoplay]; // allocate resources required for playing and add them to the internal playback queue [player play]; // play [Player stop]; // stop

Do you think that the preparation has been over for so long? It's too fast, don't worry, there are several key points.

Iv. Proxy Methods

When an exception occurs when we join the player or are interrupted by a higher level system task, our program will not be able to end. What should we do? Don't worry, we can use several delegate methods to handle all the situations well.

First, it is required to set Delegation for player:

player.delegate = self;
-(Void) audioplayerdidfinishplaying :( avaudioplayer *) player successfully :( bool) Flag {// action executed at the end of playback}-(void) warn :( avaudioplayer *) player error :( nserror *) error {// The Action for decoding the error execution}-(void) audioplayerbegininteruption :( avaudioplayer *) player {// code for handling the interrupt}-(void) audioplayerendinteruption :( avaudioplayer *) player {// code for handling the end of the interrupt}

After learning about this, you can try to make a local player.

Finally, paste a demo: musicplayer.


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.