Audio playback
1. Introduction
-Function Introduction
Used to play long audio, notes, and music, using the Avfoundation
-Framework Introduction
* Avaudioplayer
* Initialize:
Attention:
(3) You must declare a global variable for a music playback object, or a music playback object that is a property to play
(4) When exiting the playback page, be sure to empty the playback object while delegate
Import Frame: #import <AVFoundation/AVFoundation.h>
Declaring global variables
@interface Viewcontroller () <AVAudioPlayerDelegate>{ *audioplayer;} @end
Basic properties of Audio
Pre-play [Audioplayer Preparetoplay];
Get the current music channel Audioplayer.numberofchannels
Audioplayer.currenttime//Current playback time
audioplayer.playing//determine if it is playing
Audioplayer.numberofloops;//Set loop playback of this
Audioplayer.duration get time to play audio
Audioplayer.pan setting left and right channel effects
Audioplayer.volume Set Volume 0.0-1.0 is a percentage
Set rate must be set enablerate to YES; Audioplayer.enablerate =yes; Audioplayer.rate = 3.0;
Volume Audioplayer.volume = 0.1;
Set Play count negative number is an infinite loop of 0 is one time 1 is two times in turn audioplayer.numberofloops = 0;
Get current peak Audioplayer Peakpowerforchannel:2
Average Peak
Audioplayer Averagepowerforchannel:2
Several proxy methods for audio playback
//call when playback is complete- (void) Audioplayerdidfinishplaying: (Avaudioplayer *) player successfully: (BOOL) flag{NSLog (@"Play Complete");}//decoding error occurs when the call- (void) Audioplayerdecodeerrordidoccur: (Avaudioplayer *) Player ERROR: (Nserror *__nullable) error{}//call to start interrupted interrupt- (void) Audioplayerbegininterruption: (Avaudioplayer *) player{}//call after end of interrupt- (void) Audioplayerendinterruption: (Avaudioplayer *) player withoptions: (Nsuinteger) flags{}
Instance parsing audio playback
Initialize a button
In the viewdidload
UIButton *button =[UIButton Buttonwithtype:uibuttontypecustom]; Button.frame= CGRectMake ( -, -, -, -); Button.backgroundcolor=[Uicolor Browncolor]; [Button Settitle:@"Play"Forstate:uicontrolstatenormal]; [Button Settitle:@"Pause"forstate:uicontrolstateselected]; [Button addtarget:self action: @selector (play:) forcontrolevents:uicontroleventtouchupinside]; [Self.view Addsubview:button]; [Self playmusicwithname:@"tfboys-Handbook of Youth cultivation. mp3"];
How to trigger the button:
-(void) Play: (UIButton *) sender{
sender.selected =!sender.selected;
Sender.selected! = YES? [Audioplayer Pause]:[audioplayer Play];
}
- (void) Playmusicwithname: (NSString *) name{Nserror*error; //Create a music playback objectAudioplayer = [[Avaudioplayer alloc]initwithcontentsofurl:[[nsbundle mainbundle]urlforresource:name withExtension: Nil] error:&ERROR]; if(Error) {NSLog (@"%@", error); } Pre-play [Audioplayer Preparetoplay]; Play here does not write in this but it is necessary to step [audioplayer play]; Gets the channel NSLog of the current music (@"%ld", Audioplayer.numberofchannels); Durtion: Get audio playback time set channel-1.0 Left0.0 Middle1. 0 Right Audioplayer.pan=0.0; Volume Audioplayer.volume=0.1; Set rate must be set Enablerate to Yes Audioplayer.enablerate=YES; Set Rate0.5 is half the speed1.0 General2. 0 Double Rate Audioplayer.rate=1.0; CurrentTime get time gain peak must be set meteringenabled to Yes audioplayer.meteringenabled=YES; Update peak [Audioplayer updatemeters]; Get current peak NSLog (@"Current Peak:%f", [Audioplayer Peakpowerforchannel:2]); NSLog (@"Average peak%f", [Audioplayer Averagepowerforchannel:2]); Set the number of plays negative is an infinite loop01 is two times , and so on. Audioplayer.numberofloops=0; Hang Up Agent Audioplayer.Delegate=Self ; }
Audio playback for iOS development