ios 无法停止 AVPlayer
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32993896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Impossible to stop AVPlayer
提问by w3spi
I am currently testing the use of AVPlayer with audio streaming url, using Swift. There are play()and pause()methods, but the problem is that, pausing only, the stream remains cached in the device.
我目前正在使用Swift测试 AVPlayer 与音频流 url 的使用。有play()和pause()方法,但问题是,仅暂停,流仍会缓存在设备中。
Here is my test code :
这是我的测试代码:
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()
}
}
Here are the issues when trying somethings :
以下是尝试某些事情时的问题:
player = nil
: "Cannot assign a value of type 'NilLiteralCOnvertible' to a value of type 'AVPlayer'"
player = nil
:“无法将“NilLiteralCOnvertible”类型的值分配给“AVPlayer”类型的值”
player.currentItem = nil
: "Cannot assign to property: 'currentItem' is a get-only property"
player.currentItem = nil
:“无法分配给属性:'currentItem' 是一个只能获取的属性”
I tried everything, even through AVQueuePlayerwithout any effective result. (obviously, since I only have one item in my case).
我尝试了一切,即使通过AVQueuePlayer 也没有任何有效结果。(显然,因为我的案例中只有一件物品)。
How to stop AVPlayeror destroy his instance?
如何停止 AVPlayer或销毁他的实例?
采纳答案by Eric Aya
If you declare player
as an optional variable, you can then set the player to nil
to deallocate it.
如果您声明player
为可选变量,则可以将播放器设置nil
为解除分配。
Silly example but it shows what happens:
愚蠢的例子,但它显示了会发生什么:
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")
}
}
}
回答by taynguyen
回答by Krishna Kirana
SWIFT 3 Version:
SWIFT 3 版本:
player.replaceCurrentItem(with: nil)
回答by Nayan Bhut
Declare avplayer variable nil and assign the url to avplayer when you are initialize player. I think this code will help you. You can also use the code of @ayaio.
声明 avplayer 变量 nil 并在初始化播放器时将 url 分配给 avplayer。我认为这段代码会对你有所帮助。您也可以使用@ayaio 的代码。
Here is modified code :
这是修改后的代码:
import UIKit
导入 UIKit
import AVFoundation
导入 AVFoundation
class ViewController: UIViewController {
类视图控制器: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
}
}
}