我想从第15秒开始发声

时间:2015-01-14 23:59:22

标签: ios swift avfoundation avplayer seektotime

我想从第15秒开始发声。 我该如何以编程方式完成? 谢谢!

1 个答案:

答案 0 :(得分:0)

据我所知,您试图从一开始就尝试启动音频。在这种情况下:

您需要使用seekToTime

import UIKit
import AVFoundation

class ViewController: UIViewController {
    var myPlayer:AVPlayer? = nil

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        if let myAudioUrl = NSURL(string: "http://freetone.org/ring/stan/iPhone_5-Alarm.mp3") {
            myPlayer = AVPlayer(URL: myAudioUrl)
            if let myPlayer = myPlayer {
                myPlayer.seekToTime(CMTimeMakeWithSeconds(15, 1))
                myPlayer.play()
            }

        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}