2011-04-25 88 views
0

如何爲媒體播放器創建進度條和播放列表?我正在用C#製作自己的媒體播放器。媒體播放器中的進度條和播放列表

這裏是播放文件

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 QuartzTypeLib; 
using System.Runtime.InteropServices; 

namespace trying 
{ 
public partial class Form1 : Form 
{ 
    [DllImport("winmm.dll")] 
    public static extern int waveOutGetVolume(IntPtr hwo, out uint dwVolume); 

    [DllImport("winmm.dll")] 
    public static extern int waveOutSetVolume(IntPtr hwo, uint dwVolume); 

    public Form1() 
    { 
     InitializeComponent(); 
    } 
    // Define constants used for specifying the window style. 
    private const int WS_CHILD = 0x40000000; 
    private const int WS_CLIPCHILDREN = 0x2000000; 
    // Hold a form-level reference to the media control interface, 
    // so the code can control playback of the currently loaded 
    // movie. 
    public IMediaControl mc =null; 
    public IMediaPosition m=null; 
    // Hold a form-level reference to the video window in case it 
    // needs to be resized. 
    public IVideoWindow videoWindow = null; 
    public int a; 

    private void button1_Click(object sender, EventArgs e) 
    { 
     if (mc != null) 
     { 
      mc.Run(); 
     } 
    } 

    private void button2_Click(object sender, EventArgs e) 
    { 
     mc.Stop(); 
    } 

    private void button3_Click(object sender, EventArgs e) 
    { 
     mc.Pause(); 
    } 

    private void trackBar1_Scroll(object sender, EventArgs e) 
    { 
     //Calculate the volume that's being set 
    int NewVolume = ((ushort.MaxValue/10) * trackBar1.Value); 
    // Set the same volume for both the left and the right channels 
    uint NewVolumeAllChannels = (((uint)NewVolume & 0x0000ffff) | ((uint)NewVolume << 16)); 
    // Set the volume 
    waveOutSetVolume(IntPtr.Zero, NewVolumeAllChannels); 
    } 

    private void newFileToolStripMenuItem_Click(object sender, EventArgs e) 
    { 
     // Allow the user to choose a file. 
     OpenFileDialog openFileDialog = new OpenFileDialog(); 
     openFileDialog.Filter = 
     "Media Files|*.mpg;*.avi;*.wma;*.mov;*.wav;*.mp2;*.mp3|" + 
     "All Files|*.*"; 
     if (DialogResult.OK == openFileDialog.ShowDialog()) 
     { 
      // Stop the playback for the current movie, if it exists. 
      if (mc != null) mc.Stop(); 
      // Load the movie file. 
      FilgraphManager graphManager = new FilgraphManager(); 
      graphManager.RenderFile(openFileDialog.FileName); 
      // Attach the view to a picture box on the form. 
      try 
      { 
       videoWindow = (IVideoWindow)graphManager; 
       videoWindow.Owner = (int)pictureBox1.Handle; 
       videoWindow.WindowStyle = WS_CHILD | WS_CLIPCHILDREN; 
       videoWindow.SetWindowPosition(
       pictureBox1.ClientRectangle.Left, 
       pictureBox1.ClientRectangle.Top, 
       pictureBox1.ClientRectangle.Width, 
       pictureBox1.ClientRectangle.Height); 
      } 
      catch 
      { 
       // An error can occur if the file does not have a video 
       // source (for example, an MP3 file). 
       // You can ignore this error and still allow playback to 
       // continue (without any visualization). 
      } 

      // Start the playback (asynchronously). 
      mc = (IMediaControl)graphManager; 
      mc.Run(); 
     } 
    } 

代碼我想這個工作

this.progressBar2.Value =(int) m.CurrentPosition; 

,但它不工作。

+1

看來,你不知道如何處理基本的鑄造,這個項目可能會超出你的頭。我會推薦通過C#書籍或指南閱讀。 – 2011-04-25 22:12:05

回答

1

如果你想顯示當前位置,你需要this.progressBar1.Value = (int)m.CurrentPosition;

+0

它給出了一個錯誤..不能隱含轉換type'double'到'int'.An明確的轉換存在(你是否缺少演員表) – Ross 2011-04-25 22:00:28

+0

我frogot添加'(int)'。你需要在'm.CurrentPosition;'之前加'(int)'。 – 2011-04-28 17:57:25

2

m.CurrentPosition是double,progressBar1.Value是int。投它它

this.progressBar1.Value = (int)m.CurrentPosition

然而,這會截斷你的雙重使它成爲一個整數。

+0

儘管程序運行,但進度條不移動,如果我點擊它給nullreferenceexepction被處理error.Object引用未設置爲對象的實例。 – Ross 2011-04-25 22:18:31

+0

我不應該用mc來代替m? MC是IMEDIACONTROL 而m是IMEDIAPOSITION – Ross 2011-04-25 22:18:56

+0

是的。我們真的沒有足夠的代碼在這裏真正幫助。 – 2011-04-25 22:19:29