2013-07-04 209 views
7

我想播放像那樣的人做的視頻 [link]在不使用媒體播放器的情況下播放視頻[Winform]

我正在處理C#Windows窗體應用程序(而不是NXA)。 但我不知道如何。 我試過使用Microsoft.DirectX.AudioVideoPlayback,但沒有運氣。

這是我試過到目前爲止:

OpenFileDialog rihanna = new OpenFileDialog(); 
if(rihanna.ShowDialog() == DialogResult.OK) 
{ 
    video = new Video(rihanna.FileName); 
    video.Owner = panel1;  
    video.Stop();  
} 

現在我能做些什麼?我嘗試過使用視頻課,但正如我所說,它只是沒有工作。 我可以編譯,但是當我運行程序時,我沒有看到窗體窗口。

回答

0

歐凱命名空間是顯而易見的:

using Microsoft.DirectX.AudioVideoPlayback; 

表格一些全局變量:

Video vdo; 
public string mode="play"; 
public string PlayingPosition, Duration; 

現在在你的按鈕或什麼別的打開:

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); 

而且在一些其他按鈕代碼開始/暫停:

if (vdo.Playing) 
{ 
    vdo.Pause(); 
    btnPlay.Text= "Play"; 
} 
else 
{ 
    vdo.Play(); 
    btnPlay.Text= "Pause"; 
} 

BTW: 不要將其命名變量/成員或一些女孩後別人在你的代碼......

如果不知道如何將它命名,這裏有一些Guidelines

的目標是提供一套統一的命名約定 導致那名讓立即感覺到 開發商。

+3

她沒有使用變量名稱後,女孩,她正在播放視頻,並可能有一個恆定的文件與視頻的路徑,常數被命名爲「rihanna.FileName」,所以我想這個是說這是Rihana的視頻(如果你不知道,Rihana是一位歌手,所以這可能是一個視頻剪輯或什麼)。 –

5
using Microsoft.DirectX.AudioVideoPlayback; 

namespace Play_Video 
{ 

public partial class Form1 : Form 
{ 
    Video vdo; 

    public string mode="play"; 
    public string PlayingPosition, Duration; 
    public Form1() 
    { 
     InitializeComponent(); 
     VolumeTrackBar.Value = 4; 
    } 



    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 = Play_Video.Properties.Resources.btnplay; 
      vdoTrackBar.Value = 0; 
     } 
     else 
      vdoTrackBar.Value += 1; 

    } 

    private void openToolStripMenuItem_Click(object sender, EventArgs e) 
    { 
     if (vdo != null) 
     { 
      vdo.Stop(); 
      timer1.Stop(); 
      btnPlay.BackgroundImage = Play_Video.Properties.Resources.btnplay; 
      vdoTrackBar.Value = 0; 

     } 
     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|*.*"; 
     if (openFileDialog1.FileName != "") 
     { 
      Form1.ActiveForm.Text = openFileDialog.FileName + " - Anand Media Player"; 
      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); 
     } 
    } 

    private void btnPlay_Click(object sender, EventArgs e) 
    { 

     if (vdo != null) 
     { 
      if (vdo.Playing) 
      { 
       vdo.Pause(); 
       timer1.Stop(); 
       btnPlay.BackgroundImage = Play_Video.Properties.Resources.btnplay; 
      } 
      else 
      { 
       vdo.Play(); 
       timer1.Start(); 

       btnPlay.BackgroundImage = Play_Video.Properties.Resources.pause; 
      } 
     } 

    } 

    private void btnStop_Click(object sender, EventArgs e) 
    { 
     vdo.Stop(); 
     timer1.Stop(); 
     btnPlay.BackgroundImage = Play_Video.Properties.Resources.btnplay; 
     vdoTrackBar.Value = 0; 
    } 

    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; 
    } 

    private void VolumeTrackBar_Scroll(object sender, EventArgs e) 
    { 
     if (vdo != null) 
     { 
      vdo.Audio.Volume = CalculateVolume(); 
     } 
    } 
    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; 
     } 
    } 

    private void openFileDialog1_FileOk(object sender, CancelEventArgs e) 
    { 
     Duration = CalculateTime(vdo.Duration); 
     PlayingPosition = "0:00:00"; 
     txtStatus.Text = PlayingPosition + "/" + Duration; 
    } 

    private void vdoTrackBar_Scroll(object sender, EventArgs e) 
    { 
     if (vdo != null) 
     { 
      vdo.CurrentPosition = vdoTrackBar.Value; 
     } 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     MaximizeBox = false; 
    } 

    private void exitToolItem_Click(object sender,EventArgs e) 
    { 
     Application.Exit(); 
    } 
} 
} 
+0

從哪裏獲得'Microsoft.DirectX'? –

+0

Microsoft.DirectX與XNA一起是一個不錯的選擇。如果我沒有弄錯,最後一個支持它的Visual Studio是2010 – YoniXw

0

對於AudioVideoPlayback工作,你需要添加AudioVideoPlayback參考,參考>添加引用>瀏覽> C:> Windows>系統Microsoft.Net>的DirectX託管代碼> 1.0.2902.0> Microsoft.DirectX.AudioVideoPlayback.dll

相關問題