locked
playing avi files on C# form RRS feed

  • Question

  • Hi, i need help on how to play an .avi file on my C# form. I have a panel on it that should be the owner (panel1) and a button that should play the movie. Please help me with the code and if it requires any References, please copy their name here. I am a C# newbie and i need some help. Thanks.
    Wednesday, May 10, 2006 5:42 AM

Answers

  • You can use DirectX to do it.

    First you'll need to download the DirectX SDK, which you can find here http://msdn.microsoft.com/directx/sdk/

    In your c# project, add a reference to Microsoft.DirectX.AudioVideoPlayback

    You can then use the following code to play a movie

    //create the video

    Microsoft.DirectX.AudioVideoPlayback.Video video = new Microsoft.DirectX.AudioVideoPlayback.Video(fileName);

    //set the System.Windows.Forms.Control to play it in (e.g a panel)

    video.Owner = panel1;

    //Play the video (put this in a buttons click event)

    video.Play();

    //Pause the video (put this in a buttons click event)

    video.Pause();

    //Stop the video (put this in a buttons click event)

    video.Stop();

    When you are done, don't forget to call Dispose() on the video object.

    Also take a look at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_m/directx/ref/ns/microsoft.directx.audiovideoplayback/c/video/video.asp for full reference on the AudioVideoPlayback namespace

    Hope this helps

     

    Wednesday, May 10, 2006 6:12 AM
  • Alternatively, you can install a Windows Media Player component and go:

    myplayer.URL = Application.StartupPath + @"myavifile.avi";
    myplayer.Ctlcontrols.play();

    etc.

    Regards,

      Guido

    Wednesday, May 10, 2006 9:36 PM

All replies

  • You can use DirectX to do it.

    First you'll need to download the DirectX SDK, which you can find here http://msdn.microsoft.com/directx/sdk/

    In your c# project, add a reference to Microsoft.DirectX.AudioVideoPlayback

    You can then use the following code to play a movie

    //create the video

    Microsoft.DirectX.AudioVideoPlayback.Video video = new Microsoft.DirectX.AudioVideoPlayback.Video(fileName);

    //set the System.Windows.Forms.Control to play it in (e.g a panel)

    video.Owner = panel1;

    //Play the video (put this in a buttons click event)

    video.Play();

    //Pause the video (put this in a buttons click event)

    video.Pause();

    //Stop the video (put this in a buttons click event)

    video.Stop();

    When you are done, don't forget to call Dispose() on the video object.

    Also take a look at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_m/directx/ref/ns/microsoft.directx.audiovideoplayback/c/video/video.asp for full reference on the AudioVideoPlayback namespace

    Hope this helps

     

    Wednesday, May 10, 2006 6:12 AM
  • Alternatively, you can install a Windows Media Player component and go:

    myplayer.URL = Application.StartupPath + @"myavifile.avi";
    myplayer.Ctlcontrols.play();

    etc.

    Regards,

      Guido

    Wednesday, May 10, 2006 9:36 PM
  • The easiest way to ro this would be to add a toolbox item ( com component) Microsoft animation control ( mscomct.ocx)

    Then  return to your form and drag the tool onto your form from the tool listing ( this should appear at the bottom).

     

    Eg.  I renamed the tool am1

     

    double click on the tool and enter code to play, stop etc.

     

    am1.Open( "avifilename.avi");

    am1.Play();

     

     

     

    Thursday, October 4, 2007 7:16 PM
  •  Sven De Bont wrote:

    You can use DirectX to do it.

    First you'll need to download the DirectX SDK, which you can find here http://msdn.microsoft.com/directx/sdk/

    In your c# project, add a reference to Microsoft.DirectX.AudioVideoPlayback

    You can then use the following code to play a movie

    //create the video

    Microsoft.DirectX.AudioVideoPlayback.Video video = new Microsoft.DirectX.AudioVideoPlayback.Video(fileName);

    //set the System.Windows.Forms.Control to play it in (e.g a panel)

    video.Owner = panel1;

    //Play the video (put this in a buttons click event)

    video.Play();

    //Pause the video (put this in a buttons click event)

    video.Pause();

    //Stop the video (put this in a buttons click event)

    video.Stop();

    When you are done, don't forget to call Dispose() on the video object.

    Also take a look at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_m/directx/ref/ns/microsoft.directx.audiovideoplayback/c/video/video.asp for full reference on the AudioVideoPlayback namespace

    Hope this helps

     



    Willl this work for MP3 files or any other audio file by itself??
    Friday, October 5, 2007 8:27 AM
  •  

    ok, but the FILEMOVE.AVI in C:\Arquivos de programas\Microsoft Visual Studio .NET 2003\Common7\Graphics\videos

    hav a pink color, what code to set tranparent for this.......

     

     

     

    Friday, February 8, 2008 7:01 PM
  • Hello,

    How do I handle an event, for example:

    video.Owner. Click += new Event.....?

    Friday, June 11, 2010 7:41 PM
  •  Sven De Bont wrote:

    You can use DirectX to do it.

    First you'll need to download the DirectX SDK, which you can find here http://msdn.microsoft.com/directx/sdk/

    In your c# project, add a reference to Microsoft.DirectX.AudioVideoPlayback

    You can then use the following code to play a movie

    //create the video

    Microsoft.DirectX.AudioVideoPlayback.Video video = new Microsoft.DirectX.AudioVideoPlayback.Video(fileName);

    //set the System.Windows.Forms.Control to play it in (e.g a panel)

    video.Owner = panel1;

    //Play the video (put this in a buttons click event)

    video.Play();

    //Pause the video (put this in a buttons click event)

    video.Pause();

    //Stop the video (put this in a buttons click event)

    video.Stop();

    When you are done, don't forget to call Dispose() on the video object.

    Also take a look at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_m/directx/ref/ns/microsoft.directx.audiovideoplayback/c/video/video.asp  for full reference on the AudioVideoPlayback namespace

    Hope this helps

     



    Willl this work for MP3 files or any other audio file by itself??
    I am getting an exception
    Here is my code
     private void Form1_Load(object sender, EventArgs e)
            {
                try
                {

                    Microsoft.DirectX.AudioVideoPlayback.Video video = new Microsoft.DirectX.AudioVideoPlayback.Video(fileName);

                    //set the System.Windows.Forms.Control to play it in (e.g a panel)
                    video.Owner = panel1;
                    video.Play();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
               
            }
    please suggest me what am I missing
    Thursday, July 15, 2010 6:16 AM
  •  Sven De Bont wrote:

    You can use DirectX to do it.

    First you'll need to download the DirectX SDK, which you can find here http://msdn.microsoft.com/directx/sdk/

    In your c# project, add a reference to Microsoft.DirectX.AudioVideoPlayback

    You can then use the following code to play a movie

    //create the video

    Microsoft.DirectX.AudioVideoPlayback.Video video = new Microsoft.DirectX.AudioVideoPlayback.Video(fileName);

    //set the System.Windows.Forms.Control to play it in (e.g a panel)

    video.Owner = panel1;

    //Play the video (put this in a buttons click event)

    video.Play();

    //Pause the video (put this in a buttons click event)

    video.Pause();

    //Stop the video (put this in a buttons click event)

    video.Stop();

    When you are done, don't forget to call Dispose() on the video object.

    Also take a look at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_m/directx/ref/ns/microsoft.directx.audiovideoplayback/c/video/video.asp  for full reference on the AudioVideoPlayback namespace

    Hope this helps

     



    Willl this work for MP3 files or any other audio file by itself??


    I am getting am exception

    Here is my code

     private void Form1_Load(object sender, EventArgs e)
        {
          try
          {
    
            Microsoft.DirectX.AudioVideoPlayback.Video video = new Microsoft.DirectX.AudioVideoPlayback.Video(fileName); 
    
            //set the System.Windows.Forms.Control to play it in (e.g a panel)
            video.Owner = panel1;
            video.Play();
          }
          catch (Exception ex)
          {
            MessageBox.Show(ex.ToString());
          }
          
        }


    Please suggest me.

    Thursday, July 15, 2010 6:17 AM
  • Hi,

     I can download one file and can play that after downloading. But if I have different video links in different button events, I can download and play the file for the first click button. but after that if I click on any of the other button a different video should get download and play but its not happening rather the same first downloaded file is playing. Here is my code.

     private void pictureBox1_Click_1(object sender, EventArgs e)
        {
          
    
          try
          {
            
            string url = xmldatastruct[1].url;
            // Create an instance of WebClient
            WebClient client = new WebClient();
            // Hookup DownloadFileCompleted Event
            client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
            // Start the download and copy the file to c:\temp
            client.DownloadFileAsync(new Uri(url), @"D:\test\source.mp4");
    
    
          }
          catch (Exception ex)
          {
            MessageBox.Show(ex.ToString());
          }
    }
     private void pictureBox2_Click(object sender, EventArgs e)
        {
          
          try
          {
            
            string url = xmldatastruct[2].url;
            // Create an instance of WebClient
            WebClient client = new WebClient();
            // Hookup DownloadFileCompleted Event
            client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
            // Start the download and copy the file to c:\temp
            client.DownloadFileAsync(new Uri(url), @"D:\test\source.mp4");
    
    
          }
          catch (Exception ex)
          {
            MessageBox.Show(ex.ToString());
          }
    }
     void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
    
        {
         
    
        MessageBox.Show("File downloaded");
          try
          {
            int width = panel1.Width;
            int height = panel1.Height;
            //System.Uri u = new Uri(@"http://202.87.41.148/hungama/iphone/video_whatsnew_view.php?client_id=1040");
            System.Uri u = new Uri(@"D:\test\source.mp4");
            Video video;
            video = Video.FromUrl(u);
            video.Owner = panel1;
            video.Stop();
    //        video.Dispose();
            video.Play();
    
            // resize the video to the size original size of the panel
            panel1.Size = new Size(width, height);
          }
          catch (Exception ex)
          {
            MessageBox.Show(ex.ToString());
          }
          
    
    
        }
    
    Please suggest me. I want that when I press one button the link of that button should get download and play stopping the running one.

    Thursday, July 15, 2010 10:14 AM
  • Hello,

    Thanks for above example. It helped me a lot !

    I was really looking for it. 

    Can you please tell me where can I find the complete multimedia tutorial of MSDN?

    Does multimedia programming with C# contains audio/video playing or is there anything more?

    I want to learn multimedia programming with C# at basic level.

    Kindly let me know.

    Its urgent.

    Thank you. 
    Aditya N B
    Friday, May 20, 2011 6:21 PM
  • Hello, Thanks for above example. It helped me a lot ! I was really looking for it. Can you please tell me where can I find the complete multimedia tutorial of MSDN? Does multimedia programming with C# contains audio/video playing or is there anything more? I want to learn multimedia programming with C# at basic level. Kindly let me know. Its urgent. Thank you.
    Aditya N B
    Friday, May 20, 2011 6:22 PM