使用 Alamofire 下载视频,然后在 AV 播放器上播放
Download Video using Alamofire and then Play on AV player
我使用以下代码从 URL
下载视频。下载视频效果很好。
func downloadVideo(){
Alamofire.request("https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4").downloadProgress(closure:{ (progress) in
print(progress.fractionCompleted)
self.progressView.progress = Float(progress.fractionCompleted)
}).responseData{ (response) in
print(response.result)
print(response.result.value!)
print(response.result.description)
if let data = response.result.value {
let obj = String(data: data, encoding: .utf8)
let player = AVPlayer(url: URL(string: obj!)!)
self.playerController.player = player
self.addChildViewController(self.playerController)
self.view.addSubview(self.playerController.view)
self.playerController.view.frame = self.view.frame
player.play()
}
}
但我想在 AVPlayer
中播放此视频
我发现视频可能被下载为 NSData
。我在对。?
任何人都可以帮助我如何在完成下载后播放此视频?
It Would be good if anyone Can suggest some another Code For
downloading video with Progress Bar and then Playing it in Swift 3.0.
TIA。
搜索此内容的几个小时后
我想到了一种简单可靠的方法来转换 URL 中的 NSData,以便 AVPlayer
可以播放它
func downloadVideo(){
Alamofire.request("https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4").downloadProgress(closure : { (progress) in
print(progress.fractionCompleted)
self.progressView.progress = Float(progress.fractionCompleted)
}).responseData{ (response) in
print(response)
print(response.result.value!)
print(response.result.description)
if let data = response.result.value {
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let videoURL = documentsURL.appendingPathComponent("video.mp4")
do {
try data.write(to: videoURL)
} catch {
print("Something went wrong!")
}
print(videoURL)
let player = AVPlayer(url: videoURL as URL)
self.playerController.player = player
self.addChildViewController(self.playerController)
self.view.addSubview(self.playerController.view)
self.playerController.view.frame = self.view.frame
player.play()
}
}
}
您可以使用以下这段代码来转换
if let data = response.result.value {
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let videoURL = documentsURL.appendingPathComponent("video.mp4")
do {
try data.write(to: videoURL) // Pass this videoURL to AVPlayer after Downloading Video it will be played.
} catch {
print("Something went wrong!")
}
print(videoURL)
根据请求,
这是完整实现的源代码。
P.S:- This was made thinking of a demo. Many of you will argue that this code can be optimised and can be done better, but yes this
is an demo and you can pick some of the part or whole depending upon
your Requirement
我使用以下代码从 URL
下载视频。下载视频效果很好。
func downloadVideo(){
Alamofire.request("https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4").downloadProgress(closure:{ (progress) in
print(progress.fractionCompleted)
self.progressView.progress = Float(progress.fractionCompleted)
}).responseData{ (response) in
print(response.result)
print(response.result.value!)
print(response.result.description)
if let data = response.result.value {
let obj = String(data: data, encoding: .utf8)
let player = AVPlayer(url: URL(string: obj!)!)
self.playerController.player = player
self.addChildViewController(self.playerController)
self.view.addSubview(self.playerController.view)
self.playerController.view.frame = self.view.frame
player.play()
}
}
但我想在 AVPlayer
我发现视频可能被下载为 NSData
。我在对。?
任何人都可以帮助我如何在完成下载后播放此视频?
It Would be good if anyone Can suggest some another Code For downloading video with Progress Bar and then Playing it in Swift 3.0.
TIA。
搜索此内容的几个小时后
我想到了一种简单可靠的方法来转换 URL 中的 NSData,以便 AVPlayer
可以播放它func downloadVideo(){
Alamofire.request("https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4").downloadProgress(closure : { (progress) in
print(progress.fractionCompleted)
self.progressView.progress = Float(progress.fractionCompleted)
}).responseData{ (response) in
print(response)
print(response.result.value!)
print(response.result.description)
if let data = response.result.value {
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let videoURL = documentsURL.appendingPathComponent("video.mp4")
do {
try data.write(to: videoURL)
} catch {
print("Something went wrong!")
}
print(videoURL)
let player = AVPlayer(url: videoURL as URL)
self.playerController.player = player
self.addChildViewController(self.playerController)
self.view.addSubview(self.playerController.view)
self.playerController.view.frame = self.view.frame
player.play()
}
}
}
您可以使用以下这段代码来转换
if let data = response.result.value {
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let videoURL = documentsURL.appendingPathComponent("video.mp4")
do {
try data.write(to: videoURL) // Pass this videoURL to AVPlayer after Downloading Video it will be played.
} catch {
print("Something went wrong!")
}
print(videoURL)
根据请求, 这是完整实现的源代码。
P.S:- This was made thinking of a demo. Many of you will argue that this code can be optimised and can be done better, but yes this is an demo and you can pick some of the part or whole depending upon your Requirement