如何使用WkWebView内联播放视频

时间:2017-05-10 07:09:51

标签: iphone swift video wkwebview

当您使用iphone并在网页浏览中播放视频时,此视频将在原生播放器中以全屏模式打开。

我们尝试过使用" allowInlineMediaPlayback"的UIWebView和WKWebView。财产到真。但是,网络内容中的视频全屏启动,带有iphone iOS 10.2。你知道我能做什么吗?

let webConfiguration = WKWebViewConfiguration()
// Fix Fullscreen mode for video and autoplay
webConfiguration.preferences.javaScriptEnabled = true
webConfiguration.mediaPlaybackRequiresUserAction = false
webConfiguration.allowsInlineMediaPlayback = true

webView = WKWebView(frame: CGRect(x: 0, y: 0, width:self.backgroundView.frame.width, height:self.backgroundView.frame.height), configuration: webConfiguration)

环境:Xcode 8,swift 3

5 个答案:

答案 0 :(得分:8)

您的代码没有问题,但您还需要一个步骤,您使用的视频Set<String> resultSet = phraseSet.stream() .filter(s->wordSet.equals(Stream.of(s.split("\\s"))//wordSet.containsAll(...) .collect(Collectors.toSet()))) .collect(Collectors.toSet()); 应始终使用参数URL

playsinline=1

那么你可以做左边的事情。

答案 1 :(得分:6)

这是一个你想要的解决方案,我以编程方式制作播放器并更改一些代码,以内联播放视频。

var myPlayer: WKWebView!

    override func viewDidLoad() {
        super.viewDidLoad()

        let webConfiguration = WKWebViewConfiguration()
        webConfiguration.allowsInlineMediaPlayback = true
        webConfiguration.mediaTypesRequiringUserActionForPlayback = []

        myPlayer = WKWebView(frame: CGRect(x: 0, y: 0, width: 375, height: 300), configuration: webConfiguration)
        self.view.addSubview(myPlayer)

        if let videoURL:URL = URL(string: "https://www.youtube.com/embed/9n1e1N0Sa9k?playsinline=1") {
             let request:URLRequest = URLRequest(url: videoURL)
             myPlayer.load(request)
        }

        //OR to show player control also, use this
        /*if let videoURL:URL = URL(string: "https://www.youtube.com/embed/9n1e1N0Sa9k?playsinline=1&controls:1") {
             let request:URLRequest = URLRequest(url: videoURL)
             myPlayer.load(request)
        }*/
    }

答案 2 :(得分:2)

我最终这样做了:

func load(url: String) {
    let html = "<video playsinline controls width=\"100%\" src=\"\(url)\"> </video>"
    self.webView.loadHTMLString(html, baseURL: nil)
}

将网页视图加载为HTML并添加一些视频标记以自定义用户体验。

答案 3 :(得分:2)

这是在不需要HTML字符串的情况下完成此操作的最佳方法。还添加了一个在archive.org上托管的测试视频,这样你就可以避免大佬兔子测试视频恐怖节目:

斯威夫特4:

if let mediaURL:URL = URL(string: "https://ia800307.us.archive.org/14/items/electricsheep-flock-244-32500-9/00244%3D32649%3D22645%3D23652_512kb.mp4") {
    let request:URLRequest = URLRequest(url: mediaURL);
    self.webView.load(request)
}

的OBJ-C:

NSURL *mediaURL = [NSURL URLWithString:@"https://ia800307.us.archive.org/14/items/electricsheep-flock-244-32500-9/00244%3D32649%3D22645%3D23652_512kb.mp4"];

NSURLRequest *request = [NSURLRequest requestWithURL:mediaURL]
[self.webView loadRequest:request];

答案 4 :(得分:1)

这对我有用:

let configuration = WKWebViewConfiguration()
if #available(iOS 10.0, *) {
   configuration.mediaTypesRequiringUserActionForPlayback = []
}
configuration.allowsInlineMediaPlayback = true
let webView = WKWebView(frame: frame, configuration: configuration)