Avaudioplayer class is writing iOS player

Source: Internet
Author: User

Reprinted from Http://www.cnblogs.com/liukunpeng/p/3737412.html

First of all, avaudioplayer it is Apple packaged a class, easy to use and very powerful, but it only supports playing the sandbox saved music, not support online play, Apple's own ipod player dedicated. You know that you do not have to delve into it;

1, create Avaudioplayer:
    • In the project import (avfoundation.framework) framework, it is primary, otherwise everything is not discussed, then import (#import) this header file.

    • Start creating Avaudioplayer

// This code is used to read the songs in the sandbox. Nsurl *soundflieurl = [[Nsbundlemainbundle] Urlforresource: _songsdatas[_index] withextension:@ "mp3 "]; // creates an Error object, which is defined here as a pointer object, because the parameters required by the API interface are outgoing parameters and pointers. Nserror *error = nil; // Create Avaudioplayer Avaudioplayer  *avaudioplayer = [[avaudioplayeralloc]initwithcontentsofurl:                                                _soundflieurl Error: &ERROR]; [Avaudioplayer play];


This is where our players can sing. However, if you want to be more powerful, you need to set many properties of Avaudioplayer.

Avaudioplayer Properties at a glance:

@property (readonly, getter=isplaying) BOOL playing; Used to indicate whether the playback state

-(BOOL) Preparetoplay; ready to play

-(BOOL) play;

-(BOOL) Playattime: (nstimeinterval) time; Indicates that the song starts playing at this time

(readonly) Nsuinteger number of audio channels for numberofchannels;** audio player

(readonly) Nstimeinterval duration;** indicates the total length of the track

(readonly) Nsurl *url;** gets the local URL that created the data, if it is not created by a local URL, the value is nil

(readonly) NSData *data;** Gets the data source created, and if it is not data, it is nil

Float pan;** Stereo Channel, 1 is the left channel, 0 is stereo, 1 is the right channel

Float volume;** Audio Volume 0-1

BOOL enablerate;** Specifies whether an audio player adjustment is available, to adjust a player speed, set this property to Yes, call timing after initializing audio and before calling the Preparetoplay function

Float rate;** audio playback speed, the default value of this property is 1.0, provides normal playback speed, 0.5 is half the speed, 2.0 is twice times the speed, the value area is between 0.5~2.0. This value does not work and is affected by enablerate.

Nstimeinterval currenttime;**
The current playback point on the timeline where audio plays the corresponding audio. If the audio is playing, CurrentTime is set to the number of seconds that the current playback position is at the beginning of the audio. If the audio is not playing, CurrentTime is set and the play function is called the number of seconds from where the audio plays to the beginning of the audio. By setting this property, you can specify the specific points of the audio, and after implementing the fast forward of the audio back.

(readonly) Nstimeinterval devicecurrenttime;**
The time value of the audio output device, in seconds. The value of this property will be monotonically incremented when the audio is played or stopped. If multiple audio players are connected to the audio playback settings, the device time will continue to increase as long as there is an audio player in the play or pause state. Otherwise the device time is 0. Use this property to symbolize now, when calling Playattime: function. By configuring multiple audio players to play at a specified point in the specified devicecurrenttime, you can achieve precise synchronization.

Nsinteger numberofloops;**
Sets the number of audio loops. 0 is the default value and the audio is played once. Sets a positive integer that specifies the number of plays positive integer +1, sets a negative, and then plays the loop indefinitely until the stop function is called to stop playing.

(readonly) Nsdictionary *settings;**
The settings field of the audio player, including the audio and the player information that is contacted.

(getter=ismeteringenabled) BOOL meteringenabled; **
Sets whether the meter is available, which is turned off by default.

Initialize method

**-(ID) Initwithcontentsofurl: (nsurl *) URL error: (Nserror ) outerror;

Initialize returns a player that plays the data source as a URL.

**-(ID) initwithdata: (NSData *) Data error: (Nserror ) outerror;

The initialization returns a player that plays the data source.

2.-(BOOL) Preparetoplay;

Determine if the audio file is ready for playback with an audio player.

3.-(BOOL) play;

Open the audio player.

4.-(BOOL) Playattime: (nstimeinterval) time;

How much time will open the audio player.

5.-(void) pause;

Pauses the audio player.

6.-(void) stop;

To stop the audio player, the currenttime must be set to 0 and the audio player will return to the starting state.

7.-(void) updatemeters;

Update meter Readings

8.-(float) Peakpowerforchannel: (Nsuinteger) channelnumber;

-(float) Averagepowerforchannel: (Nsuinteger) channelnumber;

Reads the average and peak level of each channel, representing the number of decibels per channel, ranging between -100~0.

Agent:

1.**-(void) audioplayerdidfinishplaying: (Avaudioplayer *) player successfully: (BOOL) flag;**

When the audio finishes playing back the callback.

2.-(void) Audioplayerdecodeerrordidoccur: (Avaudioplayer ) Player error: (Nserror) error;

Decodes the error callback.

3.**-(void) Audioplayerbegininterruption: (Avaudioplayer *) player;**

Handles interrupt callbacks.

Example: When a user receives a phone call during audio playback, the audio disappears, and when this occurs, the Avaudioplayer delegate accepts the Audioplayerbegininterruption: callback, the audio session is temporarily invalid, and the player is paused. If the user receives the call, the application aborts, and the application delegates to accept a applicationwillresignactive: callback. When the call is over, the application restarts (using Applicationdidbecomeactive: Callback). If the user refuses to answer the call, then a audioplayerbegininterruption: callback is sent to the delegate. You can reply to playback from this method.

4.**-(void) Audioplayerendinterruption: (Avaudioplayer *) player withoptions: (Nsuinteger) flags; * *

**-(void) Audioplayerendinterruption: (Avaudioplayer *) player withflags: (Nsuinteger) flags;**

**-(void) Audioplayerendinterruption: (Avaudioplayer *) player;**

Handles the interrupt end callback. Here, the calling method is different as the iOS system updates.

GitHub Escrow Address

Https://github.com/liukunpengiOS/iOS-MusicPlayer

git clone address

Https://github.com/liukunpengiOS/iOS-MusicPlayer.git

Avaudioplayer class is writing iOS player

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.