简体   繁体   中英

fast forwarding a song using AVAudioPlayer

I am creating an audio player application, i need to enable the user to fast forward and backward a song by holding a button(seperately for each). I've done for playing, stopping, skip to next and previous song. But i got stuck with this section. I dont know the method to implement this. pls help.

Use the currentTime and duration properties.

ie to skip forward

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;

Similar for going backwards, only compare currentTime against 0 , instead of duration

In swift 4

for forward

var timeForward = audioPlayer.currentTime
        timeForward += 10.0 // forward 10 secs
        if (timeForward > audioPlayer.duration) {
            audioPlayer.currentTime = timeForward
        } else {
            audioPlayer.currentTime = audioPlayer.duration
        }

for backward

var timeBack = audioPlayer.currentTime
        timeBack -= 10.0 //backward 10 secs
        if (timeBack > 0) {
            audioPlayer.currentTime = timeBack
        } else {
            audioPlayer.currentTime = 0
        }

Hope this will help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM