開発環境 |
Microsoft Visual Studio Express 2013 for Windows Desktop |
実行環境 |
Microsoft Windows 8.1 (64bit) |
プロジェクトの種類 |
Visual C#/Windows フォーム アプリケーション |
プロジェクト名 |
WfMediaPlayer |
参考
コンテナー/Panel
(Name):panel1
プロパティ:Dock=Bottom/Size.Height=25
コンテナー/Panel
(Name):panel2
プロパティ:Dock=Fill
コモン コントロール/Button
(Name):button1
コモン コントロール/Label
(Name):label1
Form1.cs
/*
* 参照の追加
* アセンブリ/フレームワーク
* PresentationCore
* PresentationFramework
* System.Xaml
* WindowsBase
* WindowsFormsIntegration
*/
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Forms.Integration;
using System.Windows.Threading;
namespace WfMediaPlayer
{
public partial class Form1 : Form
{
ElementHost _elm;
MediaElement _media;
DispatcherTimer _timer;
bool _pause = false;
string _duration = "";
public Form1()
{
InitializeComponent();
_elm = new ElementHost();
_elm.Visible = true;
_elm.Dock = DockStyle.Fill;
panel2.Controls.Add(_elm);
_media = new MediaElement();
_media.Visibility = Visibility.Visible;
_media.Margin = new Thickness(0, 0, 0, 0);
_media.LoadedBehavior = MediaState.Manual;
_media.UnloadedBehavior = MediaState.Manual;
_media.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
_media.VerticalAlignment = VerticalAlignment.Center;
_elm.Child = _media;
_timer = new DispatcherTimer();
_timer.Interval = new TimeSpan(0, 0, 0, 1, 0);
_timer.Tick += timer_Tick;
_timer.Start();
_media.Source = new Uri(@"C:\tmp\sample.mp4");
_media.MediaOpened += media_MediaOpened;
_media.Play();
}
void media_MediaOpened(object sender, RoutedEventArgs e)
{
_duration = _media.NaturalDuration.ToString();
}
void timer_Tick(object sender, EventArgs e)
{
label1.Text = String.Format("{0} / {1}", _media.Position.ToString(@"hh\:mm\:ss"), _duration);
}
protected override bool ProcessDialogKey(Keys keyData)
{
switch (keyData & Keys.KeyCode)
{
case Keys.Space:
if (_pause)
{
_media.Play();
_pause = false;
}
else
{
_media.Pause();
_pause = true;
}
return true;
case Keys.Left:
_media.Position -= new TimeSpan(0, 0, 1, 0, 0);
return true;
case Keys.Right:
_media.Position += new TimeSpan(0, 0, 1, 0, 0);
return true;
}
return base.ProcessDialogKey(keyData);
}
}
}
最終更新:2014年07月24日 10:46