using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WMPLib;//引用Windows MediaPlay COM組件
using AxWMPLib; //在選項卡裏添加 Windows MediaPlay COM組件
namespace WindowsChineseCalender
{
/// <summary>
/// 塗聚文 20121222
/// Geovin Du
/// </summary>
public partial class WindowMediaPlayForm : Form
{
// AxWMPLib.AxWindowsMediaPlayer wmp = new AxWMPLib.AxWindowsMediaPlayer();
//this.Controls.Add(wmp);
WMPLib.WindowsMediaPlayerClass player=new WMPLib.WindowsMediaPlayerClass();
WMPLib.WindowsMediaPlayer Player;
double d1;
double d2;
/// <summary>
///
/// </summary>
public WindowMediaPlayForm()
{
InitializeComponent();
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void WindowMediaPlayForm_Load(object sender, EventArgs e)
{
player.URL = "";
this.axWindowsMediaPlayer1.settings.autoStart = false; //設定不自動撥放
this.tbarVolume.Minimum = 0; //設定音量調整Bar最小值為最小音量值(0)
this.tbarVolume.Maximum = 100; //設定音量調整Bar最大值為最大音量值(100)
this.tbarVolume.Value = this.axWindowsMediaPlayer1.settings.volume; //設定音量調整Bar目前值為目前音量值
this.timer1.Enabled = true;
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnupload_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.ShowDialog();
openFileDialog1.Title = "Select video file..";
openFileDialog1.InitialDirectory = Application.StartupPath;
openFileDialog1.DefaultExt = ".avi";
openFileDialog.Filter = "Media Files|*.mpg;*.avi;*.wma;*.mov;*.wav;*.mp2;*.mp3|All Files|*.*";
//vdo = new Video(openFileDialog.FileName);
//player.URL = openFileDialog.FileName;
//this.Controls.Add(player):
axWindowsMediaPlayer1.URL = openFileDialog.FileName;
this.Controls.Add(axWindowsMediaPlayer1);
axWindowsMediaPlayer1.Ctlcontrols.play();
d2 = axWindowsMediaPlayer1.currentMedia.duration;
d1 = axWindowsMediaPlayer1.Ctlcontrols.currentPosition;
this.textBox1.Text = d2.ToString() + "/" + d1.ToString();
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnPlay_Click(object sender, EventArgs e)
{
// player.controls.play();
//player.enableContextMenu = true;//启用/禁用右键菜单
axWindowsMediaPlayer1.Ctlcontrols.play();
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnStop_Click(object sender, EventArgs e)
{
//player.controls.stop();
axWindowsMediaPlayer1.Ctlcontrols.stop();
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonFull_Click(object sender, EventArgs e)
{
//player.fullScreen = true;
axWindowsMediaPlayer1.fullScreen=true;
}
/// <summary>
///
/// </summary>
/// <param name="url"></param>
private void PlayFile(String url)
{
Player = new WMPLib.WindowsMediaPlayer();
Player.PlayStateChange +=
new WMPLib._WMPOCXEvents_PlayStateChangeEventHandler(Player_PlayStateChange);
Player.MediaError +=
new WMPLib._WMPOCXEvents_MediaErrorEventHandler(Player_MediaError);
Player.URL = url;
Player.controls.play();
}
/// <summary>
///
/// </summary>
/// <param name="NewState"></param>
private void Player_PlayStateChange(int NewState)
{
if ((WMPLib.WMPPlayState)NewState == WMPLib.WMPPlayState.wmppsStopped)
{
this.Close();
}
}
/// <summary>
///
/// </summary>
/// <param name="pMediaObject"></param>
private void Player_MediaError(object pMediaObject)
{
MessageBox.Show("Cannot play media file.");
this.Close();
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tbarVolume_RightToLeftLayoutChanged(object sender, EventArgs e)
{
this.axWindowsMediaPlayer1.settings.volume = this.tbarVolume.Value; //改變音量大小
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tbarPlayLoaction_Scroll(object sender, EventArgs e)
{
this.axWindowsMediaPlayer1.Ctlcontrols.currentPosition = tbarPlayLoaction.Value; //改變撥放位置
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnIncreaseVolume_Click(object sender, EventArgs e)
{
this.axWindowsMediaPlayer1.settings.volume += 1; //音量大小+1
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnDecreaseVolume_Click(object sender, EventArgs e)
{
this.axWindowsMediaPlayer1.settings.volume -= 1; //音量大小-1
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timer1_Tick(object sender, EventArgs e)
{
if (this.axWindowsMediaPlayer1.currentMedia == null)
return;
this.tbarPlayLoaction.Maximum = (int)this.axWindowsMediaPlayer1.currentMedia.duration; //設定撥放位置調整Bar最大值
d2 = axWindowsMediaPlayer1.currentMedia.duration;
d1 = axWindowsMediaPlayer1.Ctlcontrols.currentPosition;
this.textBox1.Text = CalculateTime(d2) + "/" + CalculateTime(d1);
}
/// <summary>
///
/// </summary>
/// <param name="Time"></param>
/// <returns></returns>
public string CalculateTime(double Time)
{
string mm, ss, CalculatedTime;
int h, m, s, T;
Time = Math.Round(Time);
T = Convert.ToInt32(Time);
h = (T / 3600);
T = T % 3600;
m = (T / 60);
s = T % 60;
if (m < 10)
mm = string.Format("0{0}", m);
else
mm = m.ToString();
if (s < 10)
ss = string.Format("0{0}", s);
else
ss = s.ToString();
CalculatedTime = string.Format("{0}:{1}:{2}", h, mm, ss);
return CalculatedTime;
}
}
}
Csharp: play media file using Windows Media Player
最新推荐文章于 2024-05-15 08:44:23 发布