2013-07-04 2 views
7

Je veux lire une vidéo comme celle-là [link].Lire la vidéo sans utiliser de lecteur multimédia [Winform]

Je travaille sur l'application C# Windows Form (pas NXA). Mais je ne sais pas comment. J'ai essayé d'utiliser Microsoft.DirectX.AudioVideoPlayback mais pas de chance.

C'est ce que j'ai essayé jusqu'à présent:

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

Que puis-je faire? J'ai essayé d'utiliser la classe vidéo mais comme je l'ai dit, ça n'a pas marché. Je suis capable de compiler mais quand je cours le programme, je ne vois pas la fenêtre de formulaire.

Répondre

0

Okey Namespace est clair:

using Microsoft.DirectX.AudioVideoPlayback; 

Certaines variables globales en forme:

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

Et maintenant votre bouton ou quoi d'autre pour ouvrir:

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

Et dans certains Autre Bouton Code à démarrer/Pause:

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

BTW: Ne pas le nom variables/membres ou quelque chose autre dans votre code après les filles ...

Si vous n'êtes pas sûr de savoir comment le nommer, il y a quelques Guidelines ici.

L'objectif est de fournir un ensemble cohérent de nommer conventions qui se traduit par des noms qui ont du sens immédiat à développeurs.

+3

Elle n'a pas utilisé de noms de variables après les filles, elle jouait une vidéo et avait probablement un fichier constant avec le chemin de la vidéo, la constante a été nommée "rihanna.FileName" donc je suppose que cela dit que c'est la vidéo de Rihana (si vous ne savez pas, Rihana est une chanteuse donc c'est probablement un clip vidéo ou quelque chose.) –

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

Où trouver 'Microsoft.DirectX'? –

+0

Microsoft.DirectX est un abondend avec XNA. Le dernier studio Visual qui le supporte est 2010 si je ne me trompe pas – YoniXw

0

Pour AudioVideoPlayback fonctionne, vous devrez ajouter la référence AudioVideoPlayback, avec référence> Ajouter Référence> Catalogue> C:> Fenêtres> Microsoft.Net> DirectX pour le code managé> 1.0.2902.0> Microsoft.DirectX.AudioVideoPlayback.dll

Questions connexes