none
用C#呼叫VB中MY物件播放音樂延遲問題 RRS feed

  • 問題

  • HI.

    小弟最近在撰寫一個動態歌詞顯示器該程式可以解讀下列格式的檔案


    [00:01:00]HI

    [00:02:00]你好


    上面是TXT檔程式讀取存進變數內會在開始播放音樂之後的第一秒顯示"HI"第二秒顯示"你好" (文字的顯示是用TIMER觸發)

    由於查了幾本書之後似乎C#並沒有內建音樂播放的物件,後來在偶然的機會下,現可以呼叫VB中的MY物件來播放音樂檔,可是這個物件的功能弱,只能播放(.WAV)格式的檔案,並且事件觸發執行該物件之後,還要經過過數秒延遲才開始播放音樂,使得歌詞顯示的時間,跟音樂唱出來的時間完全無法對上.

    結論:

    1.道是不是有較好用的物件可以使用(1.至少可播放mp3 2.延遲較低)還是小弟必須自己寫一套音樂播放軟體才能達到此效果

    2.如果繼續使用MY物件是否有辦法可以讓TIMER啟動的時間跟音樂播放的時間是一樣的

    請各位大大指教!!


    小弟剛學習電腦程式語言請大大用較容易的方式指導!!

    2007年1月15日 上午 04:52

解答

  • // To play an mp3 file in C#, using MCI (from winmm.dll)

    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;

    using System.Text;
    using System.Runtime.InteropServices;

    namespace WindowsApplication1
    {
     public class Form1 : System.Windows.Forms.Form
     {
      private System.Windows.Forms.Button button1;
      private System.Windows.Forms.Button button2;
      private System.Windows.Forms.Button button3;
      private System.Windows.Forms.TextBox textBox1;
      private WindowsApplication1.Form1.Player testPlay;

      private System.ComponentModel.Container components = null;

      public Form1()
      {
       InitializeComponent();
      }

      protected override void Dispose( bool disposing )
      {
       if( disposing )
       {
        if (components != null)
        {
         components.Dispose();
        }
       }
       base.Dispose( disposing );
      }

      #region Windows Form Designer generated code
      /// <summary>
      /// Required method for Designer support - do not modify
      /// the contents of this method with the code editor.
      /// </summary>
      private void InitializeComponent()
      {
       this.button1 = new System.Windows.Forms.Button();
       this.button2 = new System.Windows.Forms.Button();
       this.button3 = new System.Windows.Forms.Button();
       this.textBox1 = new System.Windows.Forms.TextBox();
       this.SuspendLayout();
       //
       // button1
       //
       this.button1.Location = new System.Drawing.Point(8, 40);
       this.button1.Name = "button1";
       this.button1.TabIndex = 0;
       this.button1.Text = "Open";
       this.button1.Click += new System.EventHandler(this.button1_Click);
       //
       // button2
       //
       this.button2.Location = new System.Drawing.Point(96, 40);
       this.button2.Name = "button2";
       this.button2.TabIndex = 1;
       this.button2.Text = "Play";
       this.button2.Click += new System.EventHandler(this.button2_Click);
       //
       // button3
       //
       this.button3.Location = new System.Drawing.Point(208, 40);
       this.button3.Name = "button3";
       this.button3.TabIndex = 2;
       this.button3.Text = "Close";
       this.button3.Click += new System.EventHandler(this.button3_Click);
       //
       // textBox1
       //
       this.textBox1.Location = new System.Drawing.Point(8, 8);
       this.textBox1.Name = "textBox1";
       this.textBox1.Size = new System.Drawing.Size(272, 22);
       this.textBox1.TabIndex = 3;
       this.textBox1.Text = "test.mp3";
       //
       // Form1
       //
       this.AutoScaleBaseSize = new System.Drawing.Size(5, 15);
       this.ClientSize = new System.Drawing.Size(292, 69);
       this.Controls.Add(this.textBox1);
       this.Controls.Add(this.button3);
       this.Controls.Add(this.button2);
       this.Controls.Add(this.button1);
       this.Name = "Form1";
       this.Text = "Form1";
       this.Load += new System.EventHandler(this.Form1_Load);
       this.ResumeLayout(false);

      }
      #endregion

      [STAThread]
      static void Main()
      {
       Application.Run(new Form1());
      }

      private void Form1_Load(object sender, System.EventArgs e)
      {
       testPlay = new Player();
      }

      // Open
      private void button1_Click(object sender, System.EventArgs e)
      {
       // Error checking
       if(textBox1.Text.Length!=0)
       {
        testPlay.Open(textBox1.Text);
       }
      }

      // Play
      private void button2_Click(object sender, System.EventArgs e)
      {
       testPlay.Play(true);
      }

      // Close
      private void button3_Click(object sender, System.EventArgs e)
      {
       testPlay.Close();
      }


      public class Player
      {  
       private string strCommand;
       private bool bolOpen;

       [DllImport("winmm.dll")]
       private static extern long mciSendString(string strCommand,StringBuilder strReturn,int iReturnLength, IntPtr hwndCallback);
      
       public Player()
       {   
       }

       public void Close()
       {
        strCommand = "close MediaFile";
        mciSendString(strCommand, null, 0, IntPtr.Zero);
        bolOpen = false;
       }


       public void Open(string sFileName)
       {
        strCommand = "open \"" + sFileName + "\" type mpegvideo alias MediaFile";
        mciSendString(strCommand, null, 0, IntPtr.Zero);
        bolOpen = true;
       }


       public void Play(bool loop)
       {
        if(bolOpen)
        {
         strCommand = "play MediaFile";
         if (loop)
         {
          strCommand += " REPEAT";
         }
         mciSendString(strCommand, null, 0, IntPtr.Zero);
        }
       }
      }


     }
    }

    2007年1月15日 上午 09:03

所有回覆

  • 你其實可以直接呼叫System.Media.SoundPlayer這類別,
    不需要再多透過一層My物件來播放音樂。

    先試試看把Wav檔預先載入到MemoryStream,再傳給SoundPlayer播放。
    不過SoundPlayer一樣只支援Wav,
    要放Mp3的話,可以試試用Media player的ActiveX元件。

    2007年1月15日 上午 05:39
  • // To play an mp3 file in C#, using MCI (from winmm.dll)

    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;

    using System.Text;
    using System.Runtime.InteropServices;

    namespace WindowsApplication1
    {
     public class Form1 : System.Windows.Forms.Form
     {
      private System.Windows.Forms.Button button1;
      private System.Windows.Forms.Button button2;
      private System.Windows.Forms.Button button3;
      private System.Windows.Forms.TextBox textBox1;
      private WindowsApplication1.Form1.Player testPlay;

      private System.ComponentModel.Container components = null;

      public Form1()
      {
       InitializeComponent();
      }

      protected override void Dispose( bool disposing )
      {
       if( disposing )
       {
        if (components != null)
        {
         components.Dispose();
        }
       }
       base.Dispose( disposing );
      }

      #region Windows Form Designer generated code
      /// <summary>
      /// Required method for Designer support - do not modify
      /// the contents of this method with the code editor.
      /// </summary>
      private void InitializeComponent()
      {
       this.button1 = new System.Windows.Forms.Button();
       this.button2 = new System.Windows.Forms.Button();
       this.button3 = new System.Windows.Forms.Button();
       this.textBox1 = new System.Windows.Forms.TextBox();
       this.SuspendLayout();
       //
       // button1
       //
       this.button1.Location = new System.Drawing.Point(8, 40);
       this.button1.Name = "button1";
       this.button1.TabIndex = 0;
       this.button1.Text = "Open";
       this.button1.Click += new System.EventHandler(this.button1_Click);
       //
       // button2
       //
       this.button2.Location = new System.Drawing.Point(96, 40);
       this.button2.Name = "button2";
       this.button2.TabIndex = 1;
       this.button2.Text = "Play";
       this.button2.Click += new System.EventHandler(this.button2_Click);
       //
       // button3
       //
       this.button3.Location = new System.Drawing.Point(208, 40);
       this.button3.Name = "button3";
       this.button3.TabIndex = 2;
       this.button3.Text = "Close";
       this.button3.Click += new System.EventHandler(this.button3_Click);
       //
       // textBox1
       //
       this.textBox1.Location = new System.Drawing.Point(8, 8);
       this.textBox1.Name = "textBox1";
       this.textBox1.Size = new System.Drawing.Size(272, 22);
       this.textBox1.TabIndex = 3;
       this.textBox1.Text = "test.mp3";
       //
       // Form1
       //
       this.AutoScaleBaseSize = new System.Drawing.Size(5, 15);
       this.ClientSize = new System.Drawing.Size(292, 69);
       this.Controls.Add(this.textBox1);
       this.Controls.Add(this.button3);
       this.Controls.Add(this.button2);
       this.Controls.Add(this.button1);
       this.Name = "Form1";
       this.Text = "Form1";
       this.Load += new System.EventHandler(this.Form1_Load);
       this.ResumeLayout(false);

      }
      #endregion

      [STAThread]
      static void Main()
      {
       Application.Run(new Form1());
      }

      private void Form1_Load(object sender, System.EventArgs e)
      {
       testPlay = new Player();
      }

      // Open
      private void button1_Click(object sender, System.EventArgs e)
      {
       // Error checking
       if(textBox1.Text.Length!=0)
       {
        testPlay.Open(textBox1.Text);
       }
      }

      // Play
      private void button2_Click(object sender, System.EventArgs e)
      {
       testPlay.Play(true);
      }

      // Close
      private void button3_Click(object sender, System.EventArgs e)
      {
       testPlay.Close();
      }


      public class Player
      {  
       private string strCommand;
       private bool bolOpen;

       [DllImport("winmm.dll")]
       private static extern long mciSendString(string strCommand,StringBuilder strReturn,int iReturnLength, IntPtr hwndCallback);
      
       public Player()
       {   
       }

       public void Close()
       {
        strCommand = "close MediaFile";
        mciSendString(strCommand, null, 0, IntPtr.Zero);
        bolOpen = false;
       }


       public void Open(string sFileName)
       {
        strCommand = "open \"" + sFileName + "\" type mpegvideo alias MediaFile";
        mciSendString(strCommand, null, 0, IntPtr.Zero);
        bolOpen = true;
       }


       public void Play(bool loop)
       {
        if(bolOpen)
        {
         strCommand = "play MediaFile";
         if (loop)
         {
          strCommand += " REPEAT";
         }
         mciSendString(strCommand, null, 0, IntPtr.Zero);
        }
       }
      }


     }
    }

    2007年1月15日 上午 09:03