当前位置: 移动技术网 > IT编程>开发语言>c# > C#定义的MP3播放类实例

C#定义的MP3播放类实例

2019年07月18日  | 移动技术网IT编程  | 我要评论
本文实例讲述了c#定义的mp3播放类。分享给大家供大家参考。具体分析如下: 这里使用c#定义一个mp3播放类,将mp3文件作为资源文件包含到项目中,就可以播放mp3了

本文实例讲述了c#定义的mp3播放类。分享给大家供大家参考。具体分析如下:

这里使用c#定义一个mp3播放类,将mp3文件作为资源文件包含到项目中,就可以播放mp3了

using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.windows.forms;
using system.io;
using system.runtime.interopservices;
namespace mp3tool
{
  public class mp3player
  {
    [dllimport ("winmm.dll")]
    static extern int32 mcisendstring (string command,stringbuilder buffer, int32 buffersize, intptr hwndcallback);
    /// <summary>
    /// temporary repository of music files
    /// </ summary>
    private string m_musicpath = "";
    /// <summary>
    /// parent window handle
    /// </ summary>
    private intptr m_handle;
    /// <summary>
    /// create mp3 player class
    /// </ summary>
    /// <param name="music">embedded music file</ param>
    /// <param name="path">temporary music file path</ param>
    /// <param name="handle">parent window handle</ param>
    public mp3player (byte [] music, string path, intptr handle)
    {
      try
      {
        m_handle = handle;
        m_musicpath = path.combine (path, "temp.mp3");
        filestream fs = new filestream (m_musicpath, filemode.create);
        fs.write (music, 0, music.length);
        fs.close ();
      }
      catch (exception)
      {
      }
    }
    /// <summary>
    /// create mp3 player class
    /// </ summary>
    /// <param name="musicpath">to play the mp3 file path</ param>
    /// <param name="handle">parent window handle</ param>
    public mp3player(string musicpath, intptr handle)
    {
      m_musicpath = musicpath;
      m_handle = handle;
    }
    public mp3player(byte [] music, intptr handle) : this(music, @"c:\windows\",handle)
    {
    }
    public void open (string path)
    {
      if (path ! = "")
      {
        try
        {
          mcisendstring ("open " + path + " alias media", null, 0, m_handle);
          mcisendstring ("play media", null, 0, m_handle);
        }
        catch (exception)
        {
        }
      }
    }
    public void open()
    {
      open (m_musicpath);
    }
    void closemedia()
    {
      try
      {
        mcisendstring ("close all", null, 0, m_handle);
      }
      catch (exception)
      {
      }
    }
  }
}

调用方法:

private void main()
{
   //load music
   mp3player mp3 = new mp3player (properties.resources.music, handle);
  //music start playing
   mp3.open ();
}

希望本文所述对大家的c#程序设计有所帮助。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网