I want to make a sound play starting from the 15th second

137 views Asked by At

I want to make a sound play starting from the 15th second. How can I do it programmatically? Thanks!

1

There are 1 answers

0
Leo Dabus On BEST ANSWER

As I can understand you are trying to start your audio not from the beginning. In this case:

You need to use seekToTime

import UIKit
import AVFoundation

class ViewController: UIViewController {
    var player: AVPlayer!
    override func viewDidLoad() {
        super.viewDidLoad()
        let audioUrl = URL(string: "https://www.ringtonemobi.com/storage/upload/user_id_1/iphone-5-alarm-2016-08-21-01-49-25.mp3")!
        player = AVPlayer(url: audioUrl)
        player.seek(to: .init(seconds: 15, preferredTimescale: 1))
        player.play()
    }
}