Wpf-multimedia

提供:Dev Guides
移動先:案内検索

WPF-マルチメディア

WPFアプリケーションは、 MediaElement を使用してビデオとオーディオをサポートします。 オーディオとビデオをアプリケーションに統合できます。 MediaElementクラスは、Imageクラスと同様に機能します。 メディアに向けるだけでレンダリングされます。 主な違いは、動画であることですが、MP3などの音声のみで動画を含まないファイルをポイントすると、画面に何も表示されずに再生されます。

WPFは、マシンの構成に応じて、すべての種類のビデオ/オーディオ形式をサポートしています。 メディアファイルがMedia Playerを再生する場合、同じマシン上のWPFでも機能します。

アプリケーションにマルチメディアを統合する方法を理解するための例を見てみましょう。

  • WPFMultimedia という名前の新しいWPFプロジェクトを作成します。 *次のXAMLコードは、メディア要素と3つのボタンを作成し、それらをいくつかのプロパティで初期化します。
<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-compatibility/2006"
   xmlns:local = "clr-namespace:WPFMultimedia"
   mc:Ignorable = "d" Title = "MainWindow" Height = "350" Width = "604">

   <Grid>
      <StackPanel HorizontalAlignment = "Center" VerticalAlignment = "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" Click = "mediaPlay"/>
            <Button Content = "Pause" Margin = "0,0,10,0" Padding = "5" Click = "mediaPause"/>
            <Button x:Name = "muteButt" Content = "Mute" Padding = "5" Click = "mediaMute"/>
         </StackPanel>
      </StackPanel>
   </Grid>

</Window>

以下は、さまざまなボタンのC#でのClickイベントの実装です。

using System;
using System.Windows;

namespace WPFMultimedia {

   public partial class MainWindow : Window {

      public MainWindow() {
         InitializeComponent();
         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";
         }
      }
   }
}

上記のコードをコンパイルして実行すると、次のウィンドウが生成されます。 3つのボタンでビデオを再生し、再生を制御できます。

マルチメディア

ボタンを使用すると、ビデオを一時停止、ミュート、および再生できます。

音声シンセサイザー

WPFには、テキストを音声に変換する機能があります。 このAPIはSystem.Speech名前空間に含まれています。* SpeechSynthesizer *クラスは、テキストを話し言葉に変換します。

簡単な例を見てみましょう。

  • WPFTextToSpeech という名前の新しいWPFプロジェクトを作成します。
  • System.Speechアセンブリが SpeechSynthesizer クラスが動作するための参照として追加する必要があります。
  • [参照]を右クリックし、[参照の追加]を選択します。

WPF Text To Speech

  • Reference Managerダイアログが開きます。 System.Speechチェックボックスをチェックしてください

リファレンスマネージャダイアログ

  • [OK]ボタンをクリックします。 リファレンスでSystem.Speechアセンブリを確認できます。

システム音声

  • 次に、ボタンとテキストボックスをツールボックスからデザインウィンドウにドラッグします。
  • 次のXAMLコードは、ボタンとテキストボックスを作成し、それらをいくつかのプロパティで初期化します。
<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-compatibility/2006"
   xmlns:local = "clr-namespace:WPFTextToSpeech"
   mc:Ignorable = "d" Title = "MainWindow" Height = "350" Width = "604">

   <Grid>
      <Button x:Name = "button" Content = "Speak"
         HorizontalAlignment = "Left" Margin = "218,176,0,0"
         VerticalAlignment = "Top" Width = "75"/>

      <TextBox x:Name = "textBox" HorizontalAlignment = "Left"
         Height = "23" Margin = "60,104,0,0" TextWrapping = "Wrap"
         VerticalAlignment = "Top" Width = "418"/>
   </Grid>

</Window>
  • 次に、テキストボックス内のテキストを話し言葉に変換するC#の簡単な実装を示します。
using System.Speech.Synthesis;
using System.Windows;

namespace WPFTextToSpeech {
  ///<summary>
     ///Interaction logic for MainWindow.xaml
  ///</summary>

   public partial class MainWindow : Window {

      public MainWindow() {
         InitializeComponent();
      }

      private void button_Click(object sender, RoutedEventArgs e) {

         if (textBox.Text != "") {
            SpeechSynthesizer speechSynthesizer = new SpeechSynthesizer();
            speechSynthesizer.Speak(textBox.Text);
         }
         else {
            MessageBox.Show("Write some thing in the textbox!");
         }
      }
   }
}

上記のコードをコンパイルして実行すると、次のウィンドウが生成されます。 次に、テキストボックス内に「Hello World」と入力し、「話す」ボタンをクリックします。

マルチメディア出力1

「Hello World」というサウンドが生成されます。 テキストボックスに何も入力しないと、次のメッセージが点滅します。

マルチメディア出力2

上記の例を実行することをお勧めします。