Articles
FAQs
Login
Create a MediaPlayer in WPF
By [)ia6l0 iii
ODBC Drivers for QuickBooks, Salesforce, SAP, MSCRM, SharePoint … Free Trial!
The following code shows how to build a simple mediaplayer in WPF
Please note:
1. Create an Image called "
imgVideoPlayer" in your xaml code.
2. Call the "REadItem" method on a button click
3. Call the "PlayFile" method on another button click. preferably a button called "Play"
using
System;
using
System.IO;
using
System.Net;
using
System.Windows;
using
System.Windows.Controls;
using
System.Windows.Data;
using
System.Windows.Media;
using
System.Windows.Media.Animation;
using
System.Windows.Navigation;
using
System.Windows.Media.Imaging;
using
System.Windows.Forms;
using
System.Collections.Generic;
namespace
Utilities
{
/// <summary>
/// Player
/// </summary>
public
partial
class
Player
{
#region
Private Varaibles
String item =
string
.Empty;
String mediaItemShortName =
string
.Empty;
String imgLogo =
string
.Empty;
String mediaType =
string
.Empty;
double
oldHeight = 0;
double
oldWidth = 0;
bool
fetchHeights =
false
;
MediaPlayer player;
#endregion
Private Varaibles
#region
constructor
/// <summary>
/// Initializes a new instance of the <see cref="Player"/> class.
/// </summary>
public
Player()
{
this
.InitializeComponent();
}
#endregion
constructor
/// <summary>
/// Reads the item.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
private
void
readItem(
object
sender, System.Windows.RoutedEventArgs e)
{
try
{
System.Windows.Forms.OpenFileDialog openDialog =
new
System.Windows.Forms.OpenFileDialog();
if
(openDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
if
(!String.IsNullOrEmpty(openDialog.FileName))
{
FileInfo fi =
new
FileInfo(openDialog.FileName);
item = fi.FullName;
mediaItemShortName = fi.Name;
}
else
{
MessageBox.Show(
"Please select a file."
,
"Error"
,
System.Windows.MessageBoxButton.OK,
System.Windows.MessageBoxImage.Error);
}
}
}
catch
(Exception ex)
{
System.Windows.MessageBox.Show(ex.Message,
"An error occurred"
, System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error);
}
}
/// <summary>
/// Playfiles the specified sender.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
private
void
playfile(
object
sender, System.Windows.RoutedEventArgs e)
{
try
{
if
(
string
.IsNullOrEmpty(item))
{
return
;
}
else
{
player =
new
MediaPlayer();
player.Open(
new
Uri(@item, UriKind.Relative));
VideoDrawing drawing =
new
VideoDrawing();
drawing.Rect =
new
Rect(60,60,imgVideoPlayer.Height,imgVideoPlayer.Width);
drawing.Player = player;
player.Play();
DrawingImage di =
new
DrawingImage(drawing);
// freeze the image
di.Freeze();
imgVideoPlayer.Source = di;
if
(mediaType.ToLower().Equals(
"video"
))
{
if
(fetchHeights)
{
imgVideoPlayer.Width = oldWidth;
imgVideoPlayer.Height = oldHeight;
}
}
}
}
catch
(Exception ex) {
MessageBox.Show(ex.Message, ex.Message, System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error);
}
}
}
}
Popularity
(
5190 Views
)