Faster Song Transfer Using AVAudioPlayer

I am creating an application for an audio player, I need the user to be able to speed up the song back and forth by holding the button (separately for each). I made to play, stop, skip the next and previous song. But I am stuck in this section. I do not know how to implement this. Pls help.

+6
source share
2 answers

Use the currentTimeand properties duration.

i.e. skip ahead

NSTimeInterval time = avPlayer.currentTime;
time += 5.0; // forward 5 secs
if (time > avPLayer.duration)
{
    // stop, track skip or whatever you want
}
else
    avPLayer.currentTime = time;

Similarly, to go back, compare currentTimewith 0insteadduration

+19
source

for forward

var timeForward = audioPlayer.currentTime
        timeForward += 10.0 // forward 5 secs
        if (timeForward > audioPlayer.duration)
        {
            // stop, track skip or whatever you want

            audioPlayer.currentTime = timeForward
        }
        else{
            audioPlayer.currentTime = timeForward
        }

back

var timeBack = audioPlayer.currentTime
        timeBack -= 10.0 // forward 5 secs
        if (timeBack > 0)
        {
            // stop, track skip or whatever you want
            audioPlayer.currentTime = timeBack
        }
        else{
            audioPlayer.currentTime = timeBack
        }
0
source

All Articles