AVAudioPlayer in ios

Source: Internet
Author: User


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

  1. [Player prepareToPlay]; // allocate resources required for playing and add them to the internal playing queue
  2. [Player play]; // play
  3. [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:

    1. Player. delegate = self;
      1. -(Void) audioPlayerDidFinishPlaying :( AVAudioPlayer *) player successfully :( BOOL) flag {
      2. // Action executed at the end of playback
      3. }
      4. -(Void) audioPlayerDecodeErrorDidOccur :( AVAudioPlayer *) player error :( NSError *) error {
      5. // Decode the action that is incorrectly executed
      6. }
      7. -(Void) audioPlayerBeginInteruption :( AVAudioPlayer *) player {
      8. // Process the interrupted code
      9. }
      10. -(Void) audioPlayerEndInteruption :( AVAudioPlayer *) player {
      11. // Process the code that ends the interrupt
      12. }

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.