gpt4 book ai didi

ios - MPMoviePlayerViewController 不在 Swift 中播放 .mp4 视频

转载 作者:行者123 更新时间:2023-11-28 08:35:01 24 4
gpt4 key购买 nike

我有

import MediaPlayer

声明

var moviePlayer: MPMoviePlayerViewController = MPMoviePlayerViewController()

并且在播放按钮的 Action 上调用这段代码

let url:NSURL = NSURL(string: String(format: "%@",self.videoListArray[indexPath!.row].url!))!
self.moviePlayer = MPMoviePlayerViewController(contentURL: url)
self.moviePlayer.moviePlayer.fullscreen = true
self.moviePlayer.moviePlayer.repeatMode = .None
self.moviePlayer.moviePlayer.movieSourceType = MPMovieSourceType.Streaming
self.presentMoviePlayerViewControllerAnimated(self.moviePlayer)

但电影播放器​​显示黑屏并在几秒钟后再次加载到 ViewController。

最佳答案

MPMoviePlayerViewController 在 iOS 9.0 中已弃用,因此最好使用 AVPlayerViewController。要播放本地视频,您只需创建一个新的 viewController 作为 AVPlayerViewController 并使用 VCplayer .

import AVKit
import AVFoundation

class MyViewController: AVPlayerViewController {

override func viewDidLoad() {
super.viewDidLoad()

if let filePath = NSBundle.mainBundle().pathForResource("Sample", ofType: "mp4") {
let avPlayerLayer = AVPlayerLayer(player: player)
avPlayerLayer.frame = self.view.frame
view.layer .addSublayer(avPlayerLayer)
let steamingURL = NSURL(fileURLWithPath: filePath)//note here, you need to use fileURLWithPath instead of string: for local files
//let steamingURL = NSURL(string: "http://www.")//this is for web urls
player = AVPlayer(URL: steamingURL)
player!.play()
}
}
}

关于ios - MPMoviePlayerViewController 不在 Swift 中播放 .mp4 视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38013848/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com