你說你可以讓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,圖像等)注意:您可能需要修改一些代碼才能使其正常工作,因爲我還沒有對其進行測試。
編輯:你真的應該使用AVPlayer,因爲MPMoviePlayerController在iOS9及以上版本已被棄用。請閱讀文檔中的示例: https://developer.xamarin.com/recipes/ios/media/video_and_photos/play_a_video_using_avplayer/ – Gerrit
剛剛得到了一個問題,對於videopath你是你只是把你想要的視頻名稱或整個視頻路徑,我無法讓它工作。我正試圖讓我準備好的視頻。 – oisin1min
https://developer.xamarin.com/samples/monotouch/MoviePlayback/ 這就是我播放視頻的方式。也許這種播放視頻的不良方式是問題 – oisin1min