Csharp: play media file using Windows Media Player

2 篇文章 0 订阅
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;
        }
    }
}

1、基本属性  URL 指定媒体位置,本机或网络地址 例如:axWindowsMediaPlayer1.URL = @"f:\aa.mp3"; uiMode 播放器界面模式,可为Full, Mini, None, Invisible ;Full:有下面的控制条; None:只有播放部份没有控制条 例如:axWindowsMediaPlayer1.uiMode = "Invisible"; playState 播放状态,1=停止,2=暂停,3=播放,6=正在缓冲,9=正在连接,10=准备就绪 enableContextMenu 启用/禁用右键菜单 fullScreen 是否全屏显示 stretchToFit 非全屏状态时是否伸展到最佳大小 2、播放器基本控制 Ctlcontrols.play(); 播放 Ctlcontrols.pause(); 暂停 Ctlcontrols.stop(); 停止 Ctlcontrols.currentPosition 当前进度 Ctlcontrols.currentPositionString 当前进度,字符串格式。如“00:23” Ctlcontrols.fastForward(); 快进 Ctlcontrols.fastReverse(); 快退 Ctlcontrols.next(); 下一曲 Ctlcontrols.previous(); 上一曲 3、播放器基本设置 settings.volume 音量,0-100 settings.balance 声道,通过它应该可以进行立体声、左声道、右声道的控制。 settings.autoStart 是否自动播放 settings.mute 是否静音 settings.playCount 播放次数 settings.rate 播放速度 4、当前媒体属性 currentMedia.duration 媒体总长度 currentMedia.durationString 媒体总长度,字符串格式。如“03:24” currentMedia.getItemInfo(const string) 获取当前媒体信息"Title"=媒体标题,"Author"=艺术家,"Copyright"=版权信息,"Description"=媒体内容描述,"Duration"=持续时间(秒),"FileSize"=文件大小,"FileType"=文件类型,"sourceURL"=原始地址 currentMedia.setItemInfo(const string) 通过属性名设置媒体信息 currentMedia.name 同 currentMedia.getItemInfo("Title")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值