无法阻止AVPlayer

时间:2015-10-07 13:44:22

标签: ios swift avplayer

我目前正在使用 Swift 测试AVPlayer与音频流网址的使用情况。有play()pause()方法,但问题是,仅暂停,流仍然缓存在设备中。

这是我的测试代码:

import UIKit
import AVFoundation

class ViewController: UIViewController {

    let player = AVPlayer(URL: NSURL(string: "http://streaming.radio.rtl.fr/rtl-1-48-192")!)

    @IBOutlet weak var btnPlay: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()            
    }

    @IBAction func btnPress(sender: AnyObject) {
        if (btnPlay.titleLabel?.text == "Play") {
            initPlayer()
            btnPlay.setTitle("Stop", forState: UIControlState.Normal)
        } else {
            stopPlayer()
            btnPlay.setTitle("Play", forState: UIControlState.Normal)
        }
    }

    func initPlayer()  {
        player.play()
    }

    func stopPlayer() {
        // player.currentItem = nil // Last thing I tried, but generate an error
        player.pause()
    }
}

以下是尝试某事时的问题:

player = nil:“无法将类型'NilLiteralCOnvertible'的值分配给'AVPlayer'类型的值”

player.currentItem = nil:“无法分配给属性:'currentItem'是一个只用属性”

我尝试了所有内容,即使通过AVQueuePlayer也没有任何有效结果。 (显然,因为我的情况只有一个项目。)

如何停止AVPlayer 销毁他的实例

4 个答案:

答案 0 :(得分:30)

post开始,我找到了在离开或开始新玩家之前完全停止AVPlayer的最佳解决方案:

videoPlayer.replaceCurrentItemWithPlayerItem(nil)

答案 1 :(得分:17)

如果您将player声明为可选变量,则可以将播放器设置为nil以取消分配。

愚蠢的例子,但它显示了会发生什么:

import UIKit
import AVFoundation

class ViewController: UIViewController {

    @IBOutlet weak var btnPlay: UIButton!

    var player:AVPlayer?

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @IBAction func btnPress(sender: AnyObject) {
        if (btnPlay.titleLabel?.text == "Play") {
            initPlayer()
            btnPlay.setTitle("Stop", forState: UIControlState.Normal)
        } else {
            stopPlayer()
            btnPlay.setTitle("Play", forState: UIControlState.Normal)
        }
    }

    func initPlayer()  {
        if let play = player {
            print("playing")
            play.play()
        } else {
            print("player allocated")
            player = AVPlayer(URL: NSURL(string: "http://streaming.radio.rtl.fr/rtl-1-48-192")!)
            print("playing")
            player!.play()
        }
    }

    func stopPlayer() {
        if let play = player {
            print("stopped")
            play.pause()
            player = nil
            print("player deallocated")
        } else {
            print("player was already deallocated")
        }
    }
}

答案 2 :(得分:3)

SWIFT 3版本:

player.replaceCurrentItem(with: nil)

答案 3 :(得分:0)

在初始化播放器时,声明avplayer变量nil并将URL分配给avplayer。 我认为这段代码将为您提供帮助。 您也可以使用@ayaio代码。

这里是修改后的代码:

导入UIKit

导入AVFoundation

ViewController类:UIViewController {

let player = AVPlayer?]

@IBOutlet weak var btnPlay: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()            
}

@IBAction func btnPress(sender: AnyObject) {
    if (btnPlay.titleLabel?.text == "Play") {
        initPlayer()
        btnPlay.setTitle("Stop", forState: UIControlState.Normal)
    } else {
        stopPlayer()
        btnPlay.setTitle("Play", forState: UIControlState.Normal)
    }
}

func initPlayer(strUrl : String)  {
    player = AVPlayer(URL : URL(string : strUrl!)
    player.play()
}

func stopPlayer() { //For Pause
    player.pause()
    player = nil //Assign nil to player destroy player object
}

}