Search code examples
iosswiftuiwebview

Cannot play a mp4 video inside a collection view


I am trying to play a mp4 video inside a collection view, but couldn't succeed. The methods i tried are:

Method 1:

let html = "<video playsinline controls width=\"100%\" src=\"\(url)\"> </video>"
cell.webview.loadHTMLString(html, baseURL: nil)
cell.webview.allowsInlineMediaPlayback = true

Method 2:

cell.webview.loadRequest(URLRequest(url: URL(string: source)!))

Result: Video doesn't load and shows this


Solution

  • Can you please try below code,

    let html = "<video width=\"100%\" controls> <source src=\"http://test.creatrixcampus.com/uploads/doc/binaryOctalHex_1526459737.mp4\" type=\"video/mp4\"></video>"
    

    UPDATE

    let html = "<iframe src=\"http://test.creatrixcampus.com/uploads/doc/binaryOctalHex_1526459737.mp4\" width=\"100%\"></iframe>"
    webView.loadHTMLString(html, baseURL: nil)
    webView.allowsInlineMediaPlayback = true
    

    Below is the output for the same,

    enter image description here

    Let me know in case of any queries.