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
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:
Realy nice,
will use this. Thanks!
LikeLike
you are welcome =)
LikeLike
Excellent post!!!
LikeLike
Thx =D
LikeLike
Please share the example project for download. Thanks
LikeLike
i have lost the project source code. i suggest you could follow below tutorial to build a simple AVAudioPlayer project and then try to implement the code in this post.
XCode iPhone SDK Tutorial – Playing a Sound with AVAudioPlayer
LikeLike
Checking whether the slider is currently being dragged prevents it from jumping in updateSlider during interaction:
LikeLike
Thanks for your suggestion =)
LikeLike
I encounter problem once i touch the slider, it cause the program terminated show main in receive SIGABRT, please help
LikeLike
any more information about the error? Add some breakpoints to ur programme and check which line caused the problem.
LikeLike
This lags a lot! Is there a reason to this besides the AVAudioPlayer high latency?
LikeLike
Maybe you can add some debug logs and check which part of code caused the latency~
LikeLike
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.
LikeLike
Sorry, i have already lost the project source code. What error did u get? have u tried adding some debug messages and see what’s wrong?
If you want to find a example project, you can try this
XCode iPhone SDK Tutorial – Playing a Sound with AVAudioPlayer
Hope this help =)
LikeLike
Thank you very much for the tutorial
LikeLike
Good to know that it could help and thanks for your comment. =)
LikeLike
can i play a audio file from the external url in avaaudioplayer ………..can u pls help me…..
LikeLike
The following post should help.
StackOverflow – play audio from internet using AVAudioPlayer
LikeLike
sorry it is not working………….
LikeLike
How about this?
iPhone Dev SDK – Play audio file from external URL in AVAudioPlayer
LikeLike
thanks bro exacatly what i want.
LikeLike