Skip to content

IOS:AVAudioPlayer

An instance of the AVAudioPlayer class, called an audio player, provides playback of audio data from a file or memory.

Using an audio player you can:

  • Play sounds of any duration
  • Play sounds from files or memory buffers
  • Loop sounds
  • Play multiple sounds simultaneously, one sound per audio player, with precise synchronization
  • Control relative playback level, stereo positioning, and playback rate for each sound you are playing
  • Seek to a particular point in a sound file, which supports such application features as fast forward and rewind
  • Obtain data you can use for playback-level metering

Simple example

AVAudioPlayer를 사용한 간단한 MP3파일 재생방법은 아래와 같다.

// 우선, Linked Frameworks and Libraries에... AVFoundation.framework 를 추가해야 한다.
#import <AVFoundation/AVAudioPlayer.h>
// ...
NSError * error = nil;
NSURL * url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"name" ofType:@"mp3"]];
AVAudioPlayer * player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[player prepareToPlay];
[player play];

UISlider For AVAudioPlayer

We can use AVAudioPlayer to play music. Now i want to add an UISlider as the music progress bar and user can fast skip the music.
Now i have a play button in the view and when user click it, i will play the music file as well as the following method.

  • playButtonClicked – Run when the play button is clicked
  • updateSlider – Run in 1 second interval to update the UISlider
  • sliderChange – Fast skip the music when user scroll the UISlider
- (IBAction)playButtonClicked:(id)sender {
    // Read the file from resource folder and set it in the avAudioPlayer
    NSURL *fileUrl = [[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:@"musicFile" ofType:@"caf"]];
    avAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileUrl error:nil];
    [avAudioPlayer setDelegate:self];
    [avAudioPlayer setVolume:1.0];
 
    // Set a timer which keep getting the current music time and update the UISlider in 1 sec interval
    sliderTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateSlider:) userInfo:nil repeats:YES];
    // Set the maximum value of the UISlider
    aSlider.maximumValue = avAudioplayer.duration;
    // Set the valueChanged target
    [aSlider addTarget:self action:@selector(sliderChanged:) forControlEvents:UIControlEventValueChanged];
 
    // Play the audio
    [avAudioPlayer prepareToPlay];
    [avAudioPlayer play];
}

- (void) updateSlider:(NSTimer *)aTimer {
    // Update the slider about the music time
    aSlider.value = avAudioPlayer.currentTime;
}
 
- (IBAction)sliderChanged:(UISlider *)sender {
    // Fast skip the music when user scroll the UISlider
    [avAudioPlayer stop];
    [avAudioPlayer setCurrentTime:aSlider.value];
    [avAudioPlayer prepareToPlay];
    [avAudioPlayer play];
}
 
// Stop the timer when the music is finished (Need to implement the AVAudioPlayerDelegate in the Controller header)
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
    // Music completed
    if (flag) {
        [sliderTimer invalidate];
    }
}

See also

Favorite site

References


  1. Yagom's_blog_-ios_develop_30-_How_to_use_AVAudioPlayer.pdf