• Csharp: play media file using Windows Media Player


    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;
            }
        }
    }
    
    哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)成功.---Geovin Du(涂聚文)
  • 相关阅读:
    Git for windows(Msysgit)中文乱码
    DB2嵌入式编程,语句“EXEC SQL INCLUDE”说明
    《DB2 最佳实践: 性能调优和问题诊断最佳实践,第 1 部分》阅读笔记
    android sdk setup时出现:Failed to fetch URL...
    Git GUI启动报错问题记录
    安全快门
    在VFP6中模拟CursorAdapter的功能
    调试asp.net网页时不显示treeview的原因
    XP机器上WCF采用X509证书加密时IIS读取证书的授权
    在SQLSERVER2008中建立数据库复制碰到的问题
  • 原文地址:https://www.cnblogs.com/geovindu/p/2829114.html
Copyright © 2020-2023  润新知