|
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;
namespace XAML_MMedia
{
public partial class Scene1
{
String mediaItem ="";
String mediaItemShortName = "";
String imgLogo="";
String mediaType="";
mediaChecker mc = new mediaChecker();
double oldHeight = 0;
double oldWidth = 0;
bool fetchHeights = false;
MediaPlayer player;
public Scene1()
{
this.InitializeComponent();
}
private void getNewMediaItem(object sender, System.Windows.RoutedEventArgs e)
{
try {
System.Windows.Forms.OpenFileDialog ofd = new System.Windows.Forms.OpenFileDialog();
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
if (!ofd.FileName.Equals(String.Empty)) {
FileInfo fi = new FileInfo(ofd.FileName);
mediaChecker.selFileType fileType = mc.getFileType(fi.Extension);
if (!fileType.Equals(mediaChecker.selFileType.Neither))
{
double fSize = (double)fi.Length / 1024;
if (fSize < 10000)
{
mediaType = fileType.ToString();
if (fileType.Equals(mediaChecker.selFileType.Audio))
{
imgLogo = "musicLogo.png";
}
else
{
imgLogo = "videoLogo.png";
}
mediaItem = fi.FullName;
mediaItemShortName = fi.Name;
}
else
{
System.Windows.MessageBox.Show("The file you picked is too large\r\nThe limit is 10 MBytes", "Error",
System.Windows.MessageBoxButton.OK,
System.Windows.MessageBoxImage.Error);
}
}
else
{
System.Windows.MessageBox.Show("You pick an unsupported file", "Error", System.Windows.MessageBoxButton.OK,
System.Windows.MessageBoxImage.Error);
}
}
else {
System.Windows.MessageBox.Show("You need to select a file", "Error", System.Windows.MessageBoxButton.OK,
System.Windows.MessageBoxImage.Error);
}
}
else
{
System.Windows.MessageBox.Show("You need to 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);
}
}
private void playMedia(object sender, System.Windows.RoutedEventArgs e)
{
try {
if (mediaItem.Equals(String.Empty)) {
return;
}
else {
player = new MediaPlayer();
player.Open(new Uri(@mediaItem, UriKind.Relative));
VideoDrawing aVideoDrawing = new VideoDrawing();
aVideoDrawing.Rect = new Rect(60,60,imgMM.Height,imgMM.Width);
aVideoDrawing.Player = player;
player.Play();
// create a drawing image to host the VideoDrawing
DrawingImage di = new DrawingImage(aVideoDrawing);
// freeze DrawingImage for performance
di.Freeze();
// assign DrawingImage as source of the image
imgMM.Source = di;
if (mediaType.ToLower().Equals("audio"))
{
if (!fetchHeights)
{
oldWidth = imgMM.Width;
oldHeight = imgMM.Height;
fetchHeights = true;
imgMM.Width = 0;
imgMM.Height = 0;
}
}
else if (mediaType.ToLower().Equals("video"))
{
if (fetchHeights)
{
imgMM.Width = oldWidth;
imgMM.Height = oldHeight;
}
}
Uri uri = new Uri(@imgLogo, UriKind.Relative);
BitmapImage bitmap = new BitmapImage(uri);
imgMediaType.Source = bitmap;
txtMediaType1.Text = mediaItemShortName;
txtMediaType2.Text = "Thats an " + mediaType + " media item";
}
}
catch (Exception ex) {
System.Windows.MessageBox.Show(ex.Message, "An error occurred", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error);
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)
- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence
Both of these at Sussex University UK.
Award(s)
I am lucky enough to have won a few awards for Zany Crazy code articles over the years
- Microsoft C# MVP 2016
- Codeproject MVP 2016
- Microsoft C# MVP 2015
- Codeproject MVP 2015
- Microsoft C# MVP 2014
- Codeproject MVP 2014
- Microsoft C# MVP 2013
- Codeproject MVP 2013
- Microsoft C# MVP 2012
- Codeproject MVP 2012
- Microsoft C# MVP 2011
- Codeproject MVP 2011
- Microsoft C# MVP 2010
- Codeproject MVP 2010
- Microsoft C# MVP 2009
- Codeproject MVP 2009
- Microsoft C# MVP 2008
- Codeproject MVP 2008
- And numerous codeproject awards which you can see over at my blog