2017-09-15 111 views
0

目前我流動的教程,可以顯示和播放視頻使用窗口通用應用程序,我幾乎理解的過程,但我被卡在視頻顯示在另一個窗口(窗口2),上傳從第一個窗口(窗口1)如下圖所示:如何從窗口1在窗口2中顯示視頻?

enter image description here

public async void GetMedia() 
{ 
    var openPicker = new FileOpenPicker(); 
    openPicker.SuggestedStartLocation = PickerLocationId.VideosLibrary; 
    openPicker.FileTypeFilter.Add(".wmv"); 
    openPicker.FileTypeFilter.Add(".mp4"); 
    var file = await openPicker.PickSingleFileAsync(); 
    var stream = await file.OpenAsync(FileAccessMode.Read); 
    media.SetSource(stream, file.ContentType); 
    media.Play(); 
} 

窗口1

//Click boutton for upload video 
private void b_Click_1(object sender, RoutedEventArgs e) 
{ 
    Screen s=new Screen(); // window 2 
    //display and play video to window 2 
    s.GetMedia(); 
} 

回答

0

根據您的快照,你看起來就像找到這樣一個解決方案:

A music player app that lets users see what's playing while browsing through a list of other available music.

如果我的理解是正確的,你可以利用CoreApplication.CreateNewView()

這裏是一個示例代碼:

在MainPage.xaml中

<Button Content="Select media file" Click="Button_Click_1"/> 
    <Button Content="PlayInNewWindow" Click="Button_Click" /> 

在MainPage.xaml.cs中

public static IRandomAccessStream stream = null; 
    public StorageFile file = null; 

    public async void GetMedia() 
    { 
     var openPicker = new FileOpenPicker(); 
     openPicker.SuggestedStartLocation = PickerLocationId.VideosLibrary; 
     openPicker.FileTypeFilter.Add(".wmv"); 
     openPicker.FileTypeFilter.Add(".mp4"); 
     file = await openPicker.PickSingleFileAsync(); 
     stream = await file.OpenAsync(FileAccessMode.Read); 
    } 

    private async void Button_Click(object sender, RoutedEventArgs e) 
    { 
     CoreApplicationView newView = CoreApplication.CreateNewView(); 
     int newViewId = 0; 
     await newView.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,() => 
     { 
      Frame frame = new Frame(); 

      MediaData mediaData = new MediaData(); 
      mediaData.stream = stream; 
      mediaData.file = file; 
      frame.Navigate(typeof(NewWindowPage), mediaData); 
      Window.Current.Content = frame; 
      Window.Current.Activate(); 

      newViewId = ApplicationView.GetForCurrentView().Id; 
     }); 
     bool viewShown = await ApplicationViewSwitcher.TryShowAsStandaloneAsync(newViewId); 
    } 

    private void Button_Click_1(object sender, RoutedEventArgs e) 
    { 
     GetMedia(); 
    } 

在NewWindowPage.xaml

<MediaElement Name="newMedia"/> 

在NewWindowPage.xa中

public class MediaData 
{ 
    public IRandomAccessStream stream { get; set; } 
    public StorageFile file { get; set; } 
} 
+0

謝謝,它的工作原理。我使用Windows 10 IOT作爲操作系統的Raspberry Pi 3進行了測試,視頻選擇上存在錯誤,是否有其他協議可以使用樹莓上傳視頻。 –

0

在Windows通用應用程序(Windows 8/8.1的環境,我相信),你可以使用一個彈出來實現這一目標功能。使用「媒體元素」實現視頻播放。

怎麼辦? -

<Popup x:Name="popupMediaPlay" IsOpen="false"> 
<MediaElement x:Name="mediaPlayer" 
       Width="400" Height="300" 
       AutoPlay="True"/> 
</Popup> 

public async void GetMedia() 
{ 
var openPicker = new FileOpenPicker(); 
openPicker.SuggestedStartLocation = PickerLocationId.VideosLibrary; 
openPicker.FileTypeFilter.Add(".wmv"); 
openPicker.FileTypeFilter.Add(".mp`enter code here`4"); 
var file = await openPicker.PickSingleFileAsync(); 
var stream = await file.OpenAsync(FileAccessMode.Read); 
//While assigning the source you may need to provide the path/url of the 
video. 
mediaPlayer.Source = stream; 
popupMediaPlay.IsOpen = true; 
} 

我想這將是最簡單和最好的解決方案根據您的要求。

+0

是的,但我想在主窗口外面顯示視頻:ml.cs

protected override void OnNavigatedTo(NavigationEventArgs e) { var mediaData = (MediaData)e.Parameter; newMedia.SetSource(mediaData.stream, mediaData.file.ContentType); newMedia.Play(); } 

MediaData類。在您的代碼中,即使在主窗口中也會顯示彈出窗口 –

+0

如果您的要求是要感覺它在另一個窗口上運行,那麼您可以使Popup處於全屏狀態,並始終將其IsOpen屬性設置爲true。 – Dash

+0

感謝破折號,我最後的問題是什麼可以刪除第二個窗口的標題,我想第二個窗口沒有調整大小或關閉圖標。 –