• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

C#定义的一个MP3播放类

OC/C/C++ 水墨上仙 1894次浏览

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#定义的一个MP3播放类
喜欢 (0)
加载中……