资源简介
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“);
//必须设定0(系统默认的代码页)的编码方式,否则无法支持中文
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);
评论
共有 条评论