2017-08-24 130 views
0

使用this鏈接我試圖在AVPlayerViewController模擬器上播放視頻。完美的模擬器上的工作。但是當我試圖在真實的設備上播放它時。它不起作用。真實設備本地視頻在實際設備上播放Iphone

import UIKit 
import AVKit 
import AVFoundation 

class SafaribroserViewController: UIViewController { 

var playerViewController = AVPlayerViewController() 
var playerView = AVPlayer() 

override func viewDidLoad() { 
    super.viewDidLoad() 

} 

@IBAction func hsdgfsf(_ sender: Any) { 
    let fileURL = NSURL.init(fileURLWithPath: "/Users/macmini/Downloads/QRCodeReader-master 3/QRCodeReader/small.mp4") 
    playerView = AVPlayer(url: fileURL as URL) 
    playerViewController.player = playerView 
    present(playerViewController,animated:true) { 
     self.playerViewController.player?.play() 
    } 
} 

輸出

enter image description here

注: - 我真正的設備連接到MacMini熱點。

如何在我的真實設備上播放視頻?由於

+0

檢查了這一點,可能會有所幫助:https://stackoverflow.com/questions/34265411/iphone- simulator-plays-video-real-device-wont – Leo

+0

@Leo我的視頻路徑在項目資源中不存在 –

+0

看起來'fileURL'不存在 – WeiJay

回答

0

我認爲你有問題,你的路徑

let fileURL = NSURL.init(fileURLWithPath: "/Users/macmini/Downloads/QRCodeReader-master 3/QRCodeReader/small.mp4") 

只需添加small.mp4到你的應用程式專案

的包,並使用該

let urlpath  = Bundle.main.path(forResource: "small", ofType: "mp4") 
    let fileURL   = NSURL.fileURL(withPath: urlpath!) 
+0

我可以從視頻包資源中播放視頻嗎?我的視頻位於本地系統文件夾 –

+0

設備將如何看到您的本地系統文件夾,設備是否可以看到您的下載文件夾:),模擬器可以看到它,因爲它的xcode只是在您的Mac上安裝程序,但是我連接的設備是 –

+0

我的設備與本地系統Hotspot(wifi)。 –

0

試試這個代碼

let path = NSBundle.mainBundle().pathForResource("small", ofType: "mp4") 
let fileURL = NSURL.init(fileURLWithPath: path) 
if (fileURL) 
{ 
    print("Video url is not empty") 
} 

並檢查視頻複製包資源

--->項目

---->Target 
    ---->BuildPhase 
    ---->Copy Bundle Resources-->Add small.mp4 
+0

我不是在資源中添加視頻,我的視頻是外部資源 –

+0

我怎樣才能訪問真正的設備上的本地系統文件任何想法? –

+0

你是否檢查過這個條件if(fileURL) –