Csharp: play media file using Microsoft.DirectX

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 Microsoft.DirectX.AudioVideoPlayback;//需到微軟官網下載SDK
//MP4播放下載了DivX Plus 解碼器

namespace WindowsChineseCalender
{
    /// <summary>
    /// 塗聚文 20121222
    /// Geovin Du
    /// </summary>
    public partial class MediaPlayForm : Form
    {
        Video vdo;

        public string mode = "play";
        public string PlayingPosition, Duration;


        /// <summary>
        /// 
        /// </summary>
        public MediaPlayForm()
        {
            InitializeComponent();
            VolumeTrackBar.Value = 4;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MediaPlayForm_Load(object sender, EventArgs e)
        {
            MaximizeBox = false;
        }
        /// <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);

            vdo.Owner = panel1;
            panel1.Width = 700;
            panel1.Height = 390;
            Duration = CalculateTime(vdo.Duration);
            PlayingPosition = "0:00:00";
            txtStatus.Text = PlayingPosition + "/" + Duration;

            vdoTrackBar.Minimum = 0;
            vdoTrackBar.Maximum = Convert.ToInt32(vdo.Duration);
        }
        /// <summary>
        /// 播放
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPlay_Click(object sender, EventArgs e)
        {
            if (vdo != null)
            {
                if (vdo.Playing)
                {
                    vdo.Pause();
                    timer1.Stop();
                    btnPlay.BackgroundImage = WindowsChineseCalender.Properties.Resources.btnplay;
                }
                else
                {
                    vdo.Play();
                    timer1.Start();

                    btnPlay.BackgroundImage = WindowsChineseCalender.Properties.Resources.pause;
                }
            }
        }
        /// <summary>
        /// 停止
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStop_Click(object sender, EventArgs e)
        {
            vdo.Stop();
            btnPlay.BackgroundImage = WindowsChineseCalender.Properties.Resources.btnplay;
            vdoTrackBar.Value = 0;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer1_Tick(object sender, EventArgs e)
        {
            PlayingPosition = CalculateTime(vdo.CurrentPosition);
            txtStatus.Text = PlayingPosition + "/" + Duration;

            if (vdo.CurrentPosition == vdo.Duration)
            {
                timer1.Stop();
                Duration = CalculateTime(vdo.Duration);
                PlayingPosition = "0:00:00";
                txtStatus.Text = PlayingPosition + "/" + Duration;
                vdo.Stop();
                btnPlay.BackgroundImage = WindowsChineseCalender.Properties.Resources.btnplay;
                vdoTrackBar.Value = 0;
            }
            else
            {
                if (vdoTrackBar.Value >=2500)
                {
                    vdoTrackBar.Value--;
                }
                else
                {
                    vdoTrackBar.Value += 1;
                }
                
     
            }
        }
        /// <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;
        }
        /// <summary>
        /// 播放時段
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void vdoTrackBar_Scroll(object sender, EventArgs e)
        {
            if (vdo != null)
            {
                vdo.CurrentPosition = vdoTrackBar.Value;
            }

        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public int CalculateVolume()
        {
            switch (VolumeTrackBar.Value)
            {
                case 1:
                    return -1500;
                case 2:
                    return -1000;
                case 3:
                    return -700;
                case 4:
                    return -600;
                case 5:
                    return -500;
                case 6:
                    return -400;
                case 7:
                    return -300;
                case 8:
                    return -200;
                case 9:
                    return -100;
                case 10:
                    return 0;
                default:
                    return -10000;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            Duration = CalculateTime(vdo.Duration);
            PlayingPosition = "0:00:00";
            txtStatus.Text = PlayingPosition + "/" + Duration;
        }
        /// <summary>
        /// 音量調節
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void VolumeTrackBar_Scroll(object sender, EventArgs e)
        {
            //if (vdo != null)
            //{
            //    vdo.Audio.Volume = CalculateVolume();
            //}

            if (vdo != null)
            {
                vdo.Audio.Volume = CalculateVolume();
            }
        }
        /// <summary>
        /// 全屏
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonFull_Click(object sender, EventArgs e)
        {
            vdo.Fullscreen = true;           

              
        }
    }
}

 

posted @ 2012-12-22 14:57  ®Geovin Du Dream Park™  阅读(607)  评论(0编辑  收藏  举报