Skip to main content
Home Forums Silverlight Programming Programming with .NET - General Playing a video from a user's local computer
2 replies. Latest Post by Jonathan Shen – MSFT on November 10, 2008.
(0)
compcaddy
Member
0 points
2 Posts
11-06-2008 1:51 AM |
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
Contributor
3570 points
655 Posts
11-06-2008 2:42 AM |
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
Jonathan...
All-Star
24939 points
2,425 Posts
11-10-2008 10:21 PM |
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