首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >Csharp: play media file using Microsoft.DirectX

Csharp: play media file using Microsoft.DirectX

作者头像
geovindu
发布2026-06-18 15:54:45
发布2026-06-18 15:54:45
1140
举报
代码语言:javascript
复制
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


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;           

              
        }
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2026-06-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档