如何在窗口1中显示视频?

时间:2017-09-15 07:49:56

标签: c# video raspberry-pi3 windows-iot-core-10

目前我发布了一个可以使用窗口通用应用程序显示和播放视频的教程,我几乎理解了这个过程,但我被困在另一个窗口(window2)的视频显示中,从第一个窗口(窗口1)上传为如下图所示:

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();
}

2 个答案:

答案 0 :(得分:0)

在Windows Universal Application(我相信的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;
} 

我想根据您的要求,这将是最简单和最佳的解决方案。

答案 1 :(得分: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.xaml.cs

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

MediaData类:

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