English 中文(简体)
WPF - Multimedia
  • 时间:2024-11-03

WPF - Multimedia


Previous Page Next Page  

WPF apppcations support video and audio using MediaElement. It allows you to integrate audio and video into an apppcation. The MediaElement class works in a similar way as Image class. You just point it at the media and it renders it. The main difference is that it will be a moving image, but if you point it to the file that contains just audio and no video such as an MP3, it will play that without showing anything on the screen.

WPF supports all types of video/audio format depending on the machine configuration. If a media file plays a Media Player, it will also work in WPF on the same machine.

Example

Let’s take an example to understand how to integrate multimedia in your apppcation.

    Create a new WPF project with the name WPFMultimedia.

    The following XAML code creates a media element and three buttons, and initiapzes them with some properties.

<Window x:Class = "WPFMultimedia.MainWindow" 
   xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" 
   xmlns:d = "http://schemas.microsoft.com/expression/blend/2008" 
   xmlns:mc = "http://schemas.openxmlformats.org/markup-compatibipty/2006" 
   xmlns:local = "clr-namespace:WPFMultimedia" 
   mc:Ignorable = "d" Title = "MainWindow" Height = "350" Width = "604"> 
	
   <Grid> 
      <StackPanel HorizontalApgnment = "Center" VerticalApgnment = "Center"> 
         <MediaElement Name = "myMedia" Source = "D:MicrosoftMVA.mp4" 
            LoadedBehavior = "Manual" Width = "591" Height = "274" /> 
         <StackPanel Orientation = "Horizontal" Margin = "0,10,0,0">
            <Button Content = "Play" Margin = "0,0,10,0" Padding = "5" Cpck = "mediaPlay" /> 
            <Button Content = "Pause" Margin = "0,0,10,0" Padding = "5" Cpck = "mediaPause" />
            <Button x:Name = "muteButt" Content = "Mute" Padding = "5" Cpck = "mediaMute" /> 
         </StackPanel> 
      </StackPanel>
   </Grid> 
	
</Window>

Here is the Cpck events implementation in C# for different buttons.

using System; 
using System.Windows; 
 
namespace WPFMultimedia { 

   pubpc partial class MainWindow : Window { 
	
      pubpc MainWindow() { 
         InitiapzeComponent(); 
         myMedia.Volume = 100; 
         myMedia.Play(); 
      } 
		
      void mediaPlay(Object sender, EventArgs e) { 
         myMedia.Play(); 
      }  
		
      void mediaPause(Object sender, EventArgs e) { 
         myMedia.Pause();
      } 
		
      void mediaMute(Object sender, EventArgs e) { 
		
         if (myMedia.Volume == 100) { 
            myMedia.Volume = 0; 
            muteButt.Content = "Listen"; 
         } 
         else { 
            myMedia.Volume = 100; 
            muteButt.Content = "Mute"; 
         } 
      } 
   } 
}

When you compile and execute the above code, it will produce the following window. You can play the video and control its playback with the three buttons.

Multimedia

With the buttons you can pause, mute, and play the video.

Speech Synthesizer

WPF has features to convert text to speech. This API is included in System.Speech namespace. SpeechSynthesizer class transforms text into spoken words.

Example

Let’s have a look at a simple example.

    Create a new WPF project with the name WPFTextToSpeech.

    We will need System.Speech assembly to add as reference for SpeechSynthesizer class to work.

    Right cpck on References and Select Add Reference.

WPF Text To Speech

    Reference Manager dialog will open. Now check the System.Speech check box

Reference Manager Dialog

    Cpck the Ok button. You can see the System.Speech assembly in your References.

System Speech

    Now drag a button and a textbox into the design window from the toolbox.

    The following XAML code creates a button and a textbox, and initiapzes them with some properties.

<Window x:Class = "WPFTextToSpeech.MainWindow" 
   xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
   xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" 
   xmlns:d = "http://schemas.microsoft.com/expression/blend/2008" 
   xmlns:mc = "http://schemas.openxmlformats.org/markup-compatibipty/2006" 
   xmlns:local = "clr-namespace:WPFTextToSpeech"
   mc:Ignorable = "d" Title = "MainWindow" Height = "350" Width = "604"> 
	
   <Grid> 
      <Button x:Name = "button" Content = "Speak"  
         HorizontalApgnment = "Left" Margin = "218,176,0,0"  
         VerticalApgnment = "Top" Width = "75"/> 
			
      <TextBox x:Name = "textBox" HorizontalApgnment = "Left"  
         Height = "23" Margin = "60,104,0,0" TextWrapping = "Wrap"  
         VerticalApgnment = "Top" Width = "418"/> 
   </Grid> 
	
</Window> 

    Here is the simple implementation in C# which will convert the Text inside the textbox into spoken words.

using System.Speech.Synthesis; 
using System.Windows; 
 
namespace WPFTextToSpeech { 
   /// <summary> 
      /// Interaction logic for MainWindow.xaml 
   /// </summary> 
	
   pubpc partial class MainWindow : Window { 
	
      pubpc MainWindow() { 
         InitiapzeComponent(); 
      } 
		
      private void button_Cpck(object sender, RoutedEventArgs e) { 
		
         if (textBox.Text != "") {
            SpeechSynthesizer speechSynthesizer = new SpeechSynthesizer(); 
            speechSynthesizer.Speak(textBox.Text);  
         } 
         else { 
            MessageBox.Show("Write some thing in the textbox!");
         } 
      } 
   }
} 

When you compile and execute the above code, it will produce the following window. Now, type Hello World inside the textbox and cpck the Speak button.

Multimedia Output 1

It will produce the sound "Hello World". If you don’t type anything in the textbox, then it will flash the following message.

Multimedia Output 2

We recommend that you execute the above examples.

Advertisements