iPhone – Add an 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.

  • playButtonClickedRun when the play button is clicked
  • updateSliderRun in 1 second interval to update the UISlider
  • sliderChangeFast skip the music when user scroll the UISlider

Here comes the code

- (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 {
	// 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];
	}
}

 

Please note that u have to implement the AVAudioPlayerDelegate to use the audioPlayerDidFinishPlaying method.

Done =)

Reference:

About these ads

20 thoughts on “iPhone – Add an UISlider For AVAudioPlayer

  1. I just want to leave a snippet!

    Checking whether the slider is currently being dragged prevents it from jumping in updateSlider during interaction:

      if (aSlider.touchInside) {
        return;
      }
    
    Reply
  2. Randy Tian

    I encounter problem once i touch the slider, it cause the program terminated show main in receive SIGABRT, please help

    Reply
    1. ykyuen Post author

      any more information about the error? Add some breakpoints to ur programme and check which line caused the problem.

      Reply
  3. Rami

    Sorry I’m a beginner, I could not run this code on Xcode.
    Would you please upload the project files or tell me how to run the code.
    I have multiple audio files and I want to create an app that have a play button and a progress bar.

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s