Csharp: play media file using Microsoft.DirectX

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
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 @   ®Geovin Du Dream Park™  阅读(607)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
< 2012年12月 >
25 26 27 28 29 30 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示