WPFの動画再生は使い物にならない
せっかくVisualStudio2008買ってきたし、ずっと止まってるUGOの開発が再開できるのかな?*1とか思ってきたのでまずは動画再生を試してみた。
試しにDivX読ませてみるとすっごいコマ落ちして動画と音が全然あわない。WMV読み込ませるとコマ落ちはしないものの、CPU使用率100%。これでは使い物にならない。
もしかして、マシンスペック足りてないのかな?XPじゃだめ?シングルコアじゃ話にならない?でもそれだともっとスペック低いであろうノートマシンでの動作なんて望めない気がするんだけど・・・。
ちなみに、参考にしたのは 川西 裕幸のブログ : MediaElement and MediaPlayer というページと、前々から持ってたWindows Presentation Foundationプログラミングというかなり古い本。
Window1.xaml
<Window x:Class="MediaTest.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="362" Width="436"> <Grid> <StackPanel> <WrapPanel> <Button Name="PlayButton" Click="PlayButton_Click">Play</Button> <Button Name="StopButton" Click="StopButton_Click">Stop</Button> </WrapPanel> <Viewbox> <Rectangle Width="300" Height="200" Stroke="Black"> <Rectangle.Fill> <DrawingBrush> <DrawingBrush.Drawing> <VideoDrawing x:Name="MyVideoDrawing" Rect="0,0,300,200" /> </DrawingBrush.Drawing> </DrawingBrush> </Rectangle.Fill> </Rectangle> </Viewbox> </StackPanel> </Grid> </Window> __CODE__
Window1.xaml.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace MediaTest { /// <summary> /// Window1.xaml の相互作用ロジック /// </summary> public partial class Window1 : Window { public Window1() { InitializeComponent(); } MediaPlayer MyPlayer = new MediaPlayer(); private void PlayButton_Click(object sender, RoutedEventArgs e) { MyPlayer.Open(new Uri(@"<適当な動画のパス>")); MyVideoDrawing.Player = MyPlayer; MyPlayer.Play(); } private void StopButton_Click(object sender, RoutedEventArgs e) { MyPlayer.Stop(); } } }