轉自:http://gadil.blog.51cto.com/1077220/316977/
我想寫的是關於VLC Activex Plugin V1的應用,利用它在.net平臺下做一個簡單的播放器。在這裏我們會用到VS2005+VLC0.9.9,下面我會說明實現的詳細步驟:
1.運行VS2005,新建一個項目,選擇Windows應用程序,名稱MyMediaPlayer,打開工具箱,選擇“選擇項”,打開“COM組件”選項卡,選擇VideoLAN VLC ActiveX Plugin V1,選擇確定後工具箱裏會出現相應的組件,如下圖示:
2.創建MyMediaPlayer的窗體界面部分,如下圖示:
3.控件的事件編輯部分:
1)AddFile(添加播放文件)事件:
private void BtnAddFile_Click(object sender, EventArgs e)
{
//可以多行選擇
openFileDialog1.Multiselect = true;
//對話框標題
openFileDialog1.Title = "請選擇你想要播放媒體文件";
//將選中的文件添加進節目列表,默認選中的是列表中的第一個文件
if (openFileDialog1.ShowDialog() == DialogResult.OK)
for (int i = 0; i < openFileDialog1.FileNames.Length; i++)
{
this.listBox1.Items.Add(openFileDialog1.FileNames[i]);
this.listBox1.SelectedIndex = 0;
}
}
2)Play(播放)事件:
private void BtnPlay_Click(object sender, EventArgs e)
{
if (this.listBox1.SelectedIndex != -1)
{
this.axVLCPlugin1.addTarget(this.listBox1.Items[this.listBox1.SelectedIndex].ToString(), null, AXVLC.VLCPlaylistMode.VLCPlayListAppendAndGo, 0);
this.axVLCPlugin1.play();
}
//這裏是根據Url來確定播放文件的路徑,文件可以來自網絡
else if (this.textBox1.Text != "")
{
this.axVLCPlugin1.addTarget(this.textBox1.Text.ToString(), null, AXVLC.VLCPlaylistMode.VLCPlayListAppendAndGo, 0);
this.axVLCPlugin1.play();
}
isPause = true;//全局變量isPause表示可暫停狀態
}
3)Stop(停止)事件
private void BtnStop_Click(object sender, EventArgs e)
{
if (this.axVLCPlugin1.Playing || isPause==true)
{
this.axVLCPlugin1.stop();
}
}
3)Pause(暫停)事件:
private void BtnPause_Click(object sender, EventArgs e)
{
if (isPause==true)
{
this.axVLCPlugin1.pause();
}
}
4)PlayFast(快放)事件:
private void BtnPlayFaster_Click(object sender, EventArgs e)
{
if (this.axVLCPlugin1.Playing)
{
this.axVLCPlugin1.playFaster();
}
}
5)PlaySlow(慢放)事件
private void BtnPlaySlower_Click(object sender, EventArgs e)
{
if (this.axVLCPlugin1.Playing)
{
this.axVLCPlugin1.playSlower();
}
}
6)實現播放器進度條的timer控件timer.Endable=true;
timer.Interval=10;
private void timer1_Tick(object sender, EventArgs e)
{
if (this.axVLCPlugin1.Playing)
{
this.progressBar1.Maximum = this.axVLCPlugin1.Length;
this.progressBar1.Value = this.axVLCPlugin1.Time;
}
}
7)注意了,上述代碼是基本完成了播放器的一些功能,但有一個比較棘手的問題是當你沒有停止播放器播放而直接關閉程序時會產生藍屏或重啓現象,本人的解決方案如下:
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
if (this.axVLCPlugin1.Playing)
{
this.axVLCPlugin1.stop();
System.Threading.Thread.Sleep(500);
}
}
補充:
音量的控制比較簡單如:增加(一級)音量:this.axVLCPlugin1.Volume++, 增加(一級)音量:this.axVLCPlugin1.Volume--
問題:
在實現進度條顯示的時候會出現問題,當在停止後再開始播放時就不在更新進度了,因爲播放器仍然處於暫停狀態,Plugin沒有標誌暫停狀態的變量,自定義暫停狀態又不能很好的和銜接。所以這個一直是我難以解決的問題!希望有能夠解決的次問題的方法,懇請告知!