IOS multimedia playing music AVAudioPlayer and AVPlayer
1. iOS audio playback Mode
AVAudioPlayer AVPlayer system audio and audio Arrangement
AVAudioPlayer
It is easy to use, but can only play local audio, does not support streaming media playback
AVPlayer
After iOS4.0, you can use AVPlayer to play local audio and support streaming media playback. However, there are few interfaces and the processing of audio is not flexible enough.
Audio queue
The audio queue mainly processes streaming media playback and provides powerful and flexible API interfaces (the C-row interface). However, it is complicated to process.
2 AVAudioPlayer usage
To use AVAudioPlayer and AVPlayer, you must reference the AVFoundation class library.
ViewController. h
# Import
# Import
@ Interface ViewController: UIViewController
{
AVAudioPlayer * audioplayer;
}
@ End
ViewController. m
Add the following method to the loadview method in the. m file:
# Import "ViewController. h"
@ InterfaceViewController ()
@ End
@ Implementation ViewController
@ Synthesize label;
@ Synthesize textField;
-(Void) viewDidLoad
{
[Superview DidLoad];
// Method 1:
// NSURL * url = [NSURL fileURLWithPath: [NSString
// StringWithFormat: @ "% @/think about your", [[NSBundle mainBundle] resourcePath];
// Method 2
NSBundle * bundle = [NSBundlemainBundle];
NSString * urlString = [bundlepathForResource: @ "miss your night" ofType: @ "mp3"];
NSURL * url = [NSURLfileURLWithPath: urlString];
NSError * error;
Audioplayer = [[AVAudioPlayeralloc] initWithContentsOfURL: urlerror: & error];
// Volume control
Audioplayer. volume = 0.8; // 0.0 ~ Between 1.0
// Number of cycles. As long as the numberOfLoops attribute of AVAudioPlayer is set to a negative number, the audio file will be played cyclically until the stop method is called.
Audioplayer. numberOfLoops = 3; // by default, only playback is performed once.
// Playback position
Audioplayer. currentTime = 15.0; // you can specify any position to start playing.
// Number of audio channels
NSInteger channals = audioplayer. numberOfChannels; // read-only attribute
// Duration
NSTimeInterval duration = audioplayer. duration; // obtain the duration of the event.
If (audioplayer = nil ){
NSLog ([error description]);
}
Else {
[Audioplayer play];
}
}
@ End
Playing sound
- [Player prepareToPlay]; // allocate resources required for playing and add them to the internal playing queue
- [Player play]; // play
- [Player stop];/
Proxy Method
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) audioPlayerDecodeErrorDidOccur :( AVAudioPlayer *) player error :( NSError *) error {
- // Decode the action that is incorrectly executed
- }
- -(Void) audioPlayerBeginInteruption :( AVAudioPlayer *) player {
- // Process the interrupted code
- }
- -(Void) audioPlayerEndInteruption :( AVAudioPlayer *) player {
- // Process the code that ends the interrupt
- }