IOS AVAudio Player Audio Player

Keywords: network iOS Attribute

There are three ways to play audio in iOS: AVAudio Player, audio service, and audio queue.

This article mainly talks about AVAudio Player, the other two please see the relevant articles.
AVAudio Player is in the AVFoundation framework, so we need to import the AVFoundation.framework.
The AVAudioPlayer class encapsulates the ability to play a single sound. Players can be initialized with NSURLs or NSData. It should be noted that NSURLs are not network URLs but local file URLs, because AVAudio Player does not have the ability to play network audio, but we can use a little means to make it have this ability, which will be explained later.

An AVAudio Player can only play one audio. If you want to mix, you can create multiple AVAudio Player instances, each equivalent to a track on the mixer board.

1. Create a Player
#import <AVFoundation/AVFoundation.h>     
NSError* err;  
AVAudioPlayer *player = [[AVAudioPlayer alloc]  
                        initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"music" ofType:@"m4a" inDirectory:@"/"]] error:&err ];//Create using local URL s  

AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithData:myData error:&err ];//Create with NSData  

I said before that AVAudio Player can't play network URLs, but it can play NSData. We seem to be inspired by the fact that we can create NSData through network URLs, and then play NSData through AVAudio Player. So can we play network music? But this method is not desirable, because AVAudio Player can only play a complete file, does not support streaming playback, so it must be buffered before playback, so if the network file is too large or the network speed is not enough, will not it take a long time? So we usually use audio queue to play network audio.

II. Player Properties
After creating an AVAudio Player, you can access or set its various properties.

1. volume
player.volume=0.8;//Between 0.0 and 1.0  

2. Number of cycles
player.numberOfLoops = 3;//Play only once by default  

3. Play location
player.currentTime = 15.0;//You can specify to start playing from any location  

4. channel number
NSUInteger channels = player.numberOfChannels;//Read-only attribute

5. Duration
NSTimeInterval duration = player.dueration;//Get the duration of the sampling 

6. Instrument Counting
player.meteringEnabled = YES;//Open Instrument Counting Function  
[player updateMeters];//Update Instrument Reading  
//Read the average level and peak level of each channel, representing the decibel number of each channel, ranging from - 100 to 0.  
for(int i = 0; i<player.numberOfChannels;i++)
{  
	float power = [player averagePowerForChannel:i];  
	float peak = [player peakPowerForChannel:i];  
}  

3. Broadcasting sound
After so long preparation, it's finally ready to play. I'm very excited.
[player prepareToPlay];//Allocate the resources needed for playback and add them to the internal playback queue  
[player play];//play  
[player stop];//Stop it 
Whether you feel ready for such a long time, it's all over at once. It's too fast. Don't worry, there are still several key points.

IV. Agency Method
What if there is an exception to the playback, or if it is interrupted by a higher-level system task, our program will hang up before it has time to finish? No hurry, we can handle all situations well through several delegation methods.
Firstly, it is necessary to set delegation for player:
player.delegate = self;  
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer*)player successfully:(BOOL)flag
{  
    //Actions performed at the end of playback  
}  
- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer*)player error:(NSError *)error
{  
    //Decoding Error Execution Actions  
}  
- (void)audioPlayerBeginInteruption:(AVAudioPlayer*)player
{  
    //Code to handle interrupts  
}  
- (void)audioPlayerEndInteruption:(AVAudioPlayer*)player
{  
    //Code handling interrupt termination  
}  

With that in mind, you can try to make a local player.

Finally, paste a Demo: MusicPlayer



Posted by Thauwa on Sat, 23 Mar 2019 01:36:53 -0700