2017-06-20 23 views
0

我能夠錄製視頻,並與保存:Xamarin的iOS,如何保存的視頻和從文件播放

var doc = Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments); string filename = Path.GetRandomFileName() + ".mov"; var videopath = Path.Combine (doc, filename);

^這就是我現在的儲蓄它,但我的視頻路徑無法找到如何從文件播放視頻。

mp = new MPMoviePlayerController(NSUrl.FromFilename("Movies/Showhouzreel2music.m4v")); 

^我已經嘗試這樣做,可以看到該視頻文件並播放,但這段視頻被放在由我,不會因爲我拍攝的視頻工作。

我會喜歡任何方向或幫助你們可以給。

謝謝,如果您需要更多信息,請隨時詢問。

回答

0

你說你可以讓MPMoviePlayerController處理你放置的文件嗎?

如果是這樣,那就試試這個。

string newVideoPath = Path.GetFileName(videopath); 
mp = new MPMoviePlayerController(NSUrl.FromFilename(newVideoPath)); 

EDIT(更復雜的答案):

改變這一行從這樣的:

var videopath = Path.Combine (doc, filename); 

要這樣:

string videopath = Path.Combine (doc, filename); 

,然後實現這樣的事情。

public void PlayVideo(string videopath) 
    { 
     string name = Path.GetFileName(videopath); 
     Device.BeginInvokeOnMainThread(() => 
     { 
      QLPreviewItemFileSystem prevItem = new QLPreviewItemFileSystem(name, videopath); 
      QLPreviewController previewController = new QLPreviewController(); 
      previewController.DataSource = new PreviewControllerDS(prevItem); 
      UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(previewController, true, null); 
     }); 
    } 

    public class PreviewControllerDS : QLPreviewControllerDataSource 
    { 
     private QLPreviewItem _item; 

     public PreviewControllerDS(QLPreviewItem item) 
     { 
      _item = item; 
     } 

     public override IQLPreviewItem GetPreviewItem(QLPreviewController controller, nint index) 
     { 
      return _item; 
     } 

     public override nint PreviewItemCount(QLPreviewController controller) 
     { 
      return 1; 
     } 
    } 

    public class QLPreviewItemFileSystem : QLPreviewItem 
    { 
     string _fileName, _filePath; 

     public QLPreviewItemFileSystem(string fileName, string filePath) 
     { 
      _fileName = fileName; 
      _filePath = filePath; 
     } 

     public override string ItemTitle 
     { 
      get 
      { 
       return _fileName; 
      } 
     } 
     public override NSUrl ItemUrl 
     { 
      get 
      { 
       return NSUrl.FromFilename(_filePath); 
      } 
     } 

    } 

,每當你需要調用它使用:

PlayVideo(videopath); 

這也將讓你在未來的其他文件,你應該選擇開拓。 (像pdf,圖像等)注意:您可能需要修改一些代碼才能使其正常工作,因爲我還沒有對其進行測試。

+0

編輯:你真的應該使用AVPlayer,因爲MPMoviePlayerController在iOS9及以上版本已被棄用。請閱讀文檔中的示例: https://developer.xamarin.com/recipes/ios/media/video_and_photos/play_a_video_using_avplayer/ – Gerrit

+0

剛剛得到了一個問題,對於videopath你是你只是把你想要的視頻名稱或整個視頻路徑,我無法讓它工作。我正試圖讓我準備好的視頻。 – oisin1min

+0

https://developer.xamarin.com/samples/monotouch/MoviePlayback/ 這就是我播放視頻的方式。也許這種播放視頻的不良方式是問題 – oisin1min