zoukankan      html  css  js  c++  java
  • 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;           
    
                  
            }
        }
    }
    
    哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)成功.---Geovin Du(涂聚文)
  • 相关阅读:
    可怕的沉默
    我排着队 拿着号码牌
    移动改名正在运行的程序
    C# 注册热键
    c#通过编码检查是否有PendingChanges没有签入
    c#读取带命名空间的xml
    自动查找并关闭窗口
    C# 应用微软的Visual Studio International Pack 类库提取汉字拼音首字母
    C#执行DOS命令(CMD命令) (转)
    asp.net 文件下载,有进度条
  • 原文地址:https://www.cnblogs.com/geovindu/p/2829106.html
Copyright © 2011-2022 走看看