資源簡介
C#播放mp3的實例程,在實際工程中用的一個cs文件,播放mp3沒問題,供參考
代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.ComponentModel;
using?System.Data;
using?System.Data.SqlClient;
using?System.Drawing;
using?System.Text;
using?System.Windows.Forms;
using?Microsoft.Win32;
using?System.Threading;
using?System.Configuration;
using?System.Runtime.InteropServices;
using?System.IO;
using?Mine;
namespace?MineMonitoringClient
{
????public?partial?class?FormMain?:?Form
????{
????????//引用windowsAPI?
????????[DllImport(“kernel32.dll“?CharSet?=?CharSet.Auto)]
????????private?static?extern?int?GetShortPathName(string?lpszLongPath?StringBuilder?shortFile?int?cchBuffer);
????????[DllImport(“winmm.dll“?EntryPoint?=?“mciSendString“?CharSet?=?CharSet.Auto)]
????????private?static?extern?int?mciSendString(string?lpstrCommand?string?lpstrReturnString?int?uReturnLength?int?hwndCallback);???????
????????[DllImport(“kernel32“)]
????????private?static?extern?int?GetPrivateProfileString(string?section?string?key?string?def?byte[]?retVal?int?size?string?filePath);
????????private?const?int?MM_MCINOTIFY?=?0x3B9;
????????private?int?playCount?=?0;
????????private?bool?PlayFlag?=?true;
????????????
????????//讀取INI文件指定
????????public?string?ReadString(string?Section?string?Ident?string?Default)
????????{
????????????Byte[]?Buffer?=?new?Byte[65535];
????????????int?bufLen?=?GetPrivateProfileString(Section?Ident?Default?Buffer?Buffer.GetUpperBound(0)?Directory.GetCurrentDirectory().ToString()?+?@“\SoundSet.ini“);
????????????//必須設(shè)定0(系統(tǒng)默認的代碼頁)的編碼方式,否則無法支持中文
????????????string?s?=?Encoding.GetEncoding(0).GetString(Buffer);
????????????s?=?s.Substring(0?bufLen);
????????????return?s.Trim();
????????}
????????//播放聲音信息??
????????public?void?PlaySound(object?sender)
????????{
????????????PlayFlag?=?true;
????????????if?(ReadString(“SoundSet“?“SoundEnabled“?““)?==?“True“?&&?ReadString(“SoundSet“?“SoundPlayTotal“?““).ToString().Trim()?!=?“0“)
????????????{
????????????????string?name?=?ReadString(“SoundSet“?“SoundPath“?““);
????????????????StringBuilder?shortpath?=?new?StringBuilder(80);
????????????????int?result?=?GetShortPathName(name?shortpath?shortpath.Capacity);
????????????????mciSendString(@“close?all“?null?0?0);
????????????????mciSendString(@“open?“?+?name?+?“?alias?song“?null?0?0);
????????????????mciSendString(“play?song?notify“?null?0?this.Handle.ToInt32());
????????????}
?
????????}
????????//播放聲音信息??
????????public?void?PlaySound()
????????{
????????????PlayFlag?=?true;
????????????if?(ReadString(“SoundSet“?“SoundEnabled“?““)?==?“True“?&&?ReadString(“SoundSet“?“SoundPlayTotal“?““).ToString().Trim()?!=?“0“)
????????????{
????????????????string?name?=?ReadString(“SoundSet“?“SoundPath“?““);
????????????????StringBuilder?shortpath?=?new?StringBuilder(80);
????????????????int?result?=?GetShortPathName(name?shortpath?shortpath.Capacity);
????????????????mciSendString(@“close?all“?null?0?0);
???????????
評論
共有 條評論