How to display a video in window 2 from window 1?

125 views Asked by At

Currently I flow a tutorial that can display and play a video using window universal application and I almost understand the process but I am stuck on the video display in another window (window2),Upload from the first window (window 1) as in the figure below:

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

Window1

//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

There are 2 answers

1
Rita Han On BEST ANSWER

Based on your snapshot, you look like finding a solution like this:

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

If my understanding is correct, you can utilize CoreApplication.CreateNewView().

And here is a sample code:

In MainPage.xaml

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

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

In NewWindowPage.xaml

    <MediaElement Name="newMedia"/>

In NewWindowPage.xaml.cs

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

MediaData class:

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

In Windows Universal Application(Windows 8/8.1 environment I believe), you can use a popup to achieve this functionality. Use the "Media Element" to achieve the video playback.

How to do it? -

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

I guess this would be easiest and best solution as per your requirement.