Skip to main content

Microsoft Silverlight

Answered Question Playing a video from a user's local computerRSS Feed

(0)

compcaddy
compcaddy

Member

Member

0 points

2 Posts

Playing a video from a user's local computer

I am trying to come up with a solution where I have a Windows Forms application and I would like a user to be able to select a video on their local hardrive and have it play within a Silverlight control (not Windows Media Player). Any ideas? Can I have a silverlight control within a webbrowser control on a Windows Forms app and somehow have the user be able to select a file on their harddrive and play it within the Silverlight Media Player? Thanks! Phil

preishuber
preishuber

Contributor

Contributor

3570 points

655 Posts

Re: Playing a video from a user's local computer

there is for security reasons no direct access to local resources from silverlight like hdd. But user can open file dialog, open file and

a) upload it to server

b) store it in isolated storage

both sources are valid for playing video

 

-Hannes

http://www.preishuber.net http://weblogs.asp.net/hpreishuber

Jonathan Shen – MSFT
Jonathan...

All-Star

All-Star

24939 points

2,425 Posts

Microsoft
Answered Question

Re: Playing a video from a user's local computer

Hi Phil,

We can use OpenFileDialog to select the video file and then get the file stream by using openFileDialog1.File.OpenRead();  Below is the working sample.

 

<UserControl x:Class="LocalVideoPlayer.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">
        <StackPanel>
            <Button Content="Open" Width="50" Height="50" Click="Button_Click_1"></Button>
            <Button Content="Close" Width="50" Height="50" Click="Button_Click"></Button>
            <MediaElement x:Name="mediaPlayer" Width="300" Height="300" />
        </StackPanel>
    </Grid>
</UserControl>
  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;

namespace LocalVideoPlayer
{
    public partial class Page : UserControl
    {
        Stream stream = null;

        public Page()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(Page_Loaded);
        }

        void Page_Loaded(object sender, RoutedEventArgs e)
        {
            Open();
        }

        void Open()
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Filter = "Videos(*.wmv)|*.wmv";
            if (openFileDialog1.ShowDialog() == true)
            {
                stream = openFileDialog1.File.OpenRead();
                this.mediaPlayer.SetSource(stream);
            }
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (stream != null) { this.mediaPlayer.Source = null; stream.Close(); }
        }

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


    }
}

Best regards,

Jonathan 

Jonathan Shen
Microsoft Online Community Support
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities