Is it possible to play just a part of an mp3 file with AVAudioPlayer? If yes, how? - iphone

Hi I have a locally stored 5:00 minutes mp3 files in my iPhone app.
I would like to know if it is possible to play just an interval from that file using AVAudioPlayer after it is readyToPlay, lets say from 0:25 to 1:34.
If it is possible how can I achieve that?
I know I can move the playhead cursor using setCurrentTime: but how do I let the AVAudioPlayer know how many seconds it should play from that currentTime.
Any help is highly appreciated.

You will need to set a timer which will stop that playing after particular time interval.

#Horatiu you can use NSTimer to schedule it for a particular time interval after you have use setCurrentTime: method, and in the selector method of the NSTimer you can make the audioPlayer stop.

Fire a timer when you start playing the file. When the required time is reached pause or stop the player

Related

Is there an alternative to using an NSTimer to display remaining time of audio file with AVAudioPlayer?

I play music with AVAudioPlayer and want to display the remaining time. It seems many suggest an NSTimer that fires every second but I am afraid this is inefficient and prone to error. Since NSTimer is not very precise timing (I have heard) it can be that the timer fires twice within the same second of audio, and the countdown then jumps over one second. Also this is a pull model.
Is there a more savvy way where AVAudioPlayer would call my countdown every time a second ticks away?
While NSTimer is semi-inaccurate, you can still just have it call every second or so and get information from the currentTime property of the song, and compare it to the duration property. From that you can calculate remaining time etc.
AVAudioPlayer Documentation
There are no callbacks available to help you handle updating the time display when playing an audio file. The NSTimer method is how it should be done. If you're concerned with inaccuracies in the display due to NSTimer firing precision, just reduce your timer's update interval. Keep the workload light in your implementation and set the interval to 500ms, or perhaps 250ms...

How to get notified when AVAudioPlayer loops back to the beginning?

I'm playing a sound file in a loop and need to restart an animation with it every time the loop starts from the beginning.
I couldn't find a delegate method for it in the documentation. Is there a way to get notified when the player loops around to the beginning?
My approach was to use a timer which checks current play time every few milliseconds but this sounds like a horrible solution.
No, AFAIK your solution is the only one which works. The finishedPlaying method is not called whilst looping so sampling the position is the only technique.
I'm updating some inherited code to add looping and came to this conclusion under iOS8.
You can set up a delegate for the avaudioplayer instance. Then the delegate will be notified whenever the sound has successfully finished playing. Then you can probably restart the player to play again and restart your animation. Just off the top of my head. Look here for more information.

AVAudioRecorder setting recording start time

I am using AVAudioRecorder for recording sound in my ios app. I want that if user moves the UISlider object to 2 seconds and press Record button then after saving the recording file and playing it, user should hear the recording after a pause of 2 seconds. How can I achieve this functionality? Can anyone please help?
Best Regards
You can add 2 second silence (please consider slider value is 2) to your Audio file starting section
To do this please go through this link
The below links also may help you to achieve the goal.
avaudioplayer-append-recording-to-file
trim-audio-with-ios

NSTimer Pause / Playfile1, Pause / Playfile2

is there way to use NSTimer to play a number of sound files to completion in order, or how would I play 3 sound files, one after another, each starting after the previous completed.
Each file is only 2 seconds and I figured I could do something with NSTimer, but the logic is escaping me.
Anyone have any suggestions
I would recommend you look into the AV Framework rather than trying to roll your own.

AVAudioPlayer seek performance is not good

We are a developing an application which is supposed to play long audio files. duration of audio file can be upto 23 hours...
we are trying to use this AVAuidoPlayer for this purpose. To perform fast forward we are setting CurrentTime property of AVAudioPlayer. if the forward duration is just few minutes or even upto 20 minutes everything works fine. But if we try to jump by say 2 hours time then setCurrentTime call on the player take too long time ( almost 30-40 seconds). This is not acceptable behavior.
please let me know if anybody managed to successfully use AVAudioPlayer setCurrentTime property to jump long durations (say 2 hours)
What is the file format of your file?
I had this problem with MP3, it worked better with AAC.