问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

C#的winform程序如何实现mp3文件的播放啊?

发布网友 发布时间:2022-04-26 00:27

我来回答

3个回答

热心网友 时间:2023-10-24 15:51

用API函数就行,新建个类文件把下面的代码粘进去在调用play方法就行了(先给FileName属性赋值)
调用方法
Test.Mp3 mp3 = new Test.Mp3();
mp3.FileName = xxx;
mp3.play();

类代码
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace Test
{
class Mp3
{
public Mp3()
{
}
//定义API函数使用的字符串变量
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
private string Name = "";
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
private string rLength = "";
[MarshalAs(UnmanagedType.LPTStr, SizeConst = 128)]
private string TemStr = "";
int ilong;
//定义播放状态枚举变量
public enum State
{
mPlaying = 1,
mPuase = 2,
mStop = 3
};
//结构变量
public struct structMCI
{
//public bool bMut;
//public int iDur;
//public int iVol;
//public int iBal;
public string iName;
public State state;
public int iPos;
};

public structMCI mc = new structMCI();

//取得播放文件属性
public string FileName
{
get
{
return mc.iName;
}
set
{
//ASCIIEncoding asc = new ASCIIEncoding();
try
{
TemStr = "";
TemStr = TemStr.PadLeft(127, Convert.ToChar(" "));
Name = Name.PadLeft(260, Convert.ToChar(" "));
mc.iName = value;
ilong = APIClass.GetShortPathName(mc.iName, Name, Name.Length);
Name = GetCurrPath(Name);
//Name = "open " + Convert.ToChar(34) + Name + Convert.ToChar(34) + " alias media";
Name = "open " + Convert.ToChar(34) + Name + Convert.ToChar(34) + " alias media";
ilong = APIClass.mciSendString("close all", TemStr, TemStr.Length, 0);
ilong = APIClass.mciSendString(Name, TemStr, TemStr.Length, 0);
ilong = APIClass.mciSendString("set media time format milliseconds", TemStr, TemStr.Length, 0);
mc.state = State.mStop;
}
catch
{
MessageBox.Show("出错错误!");
}
}
}
//播放
public void play()
{
TemStr = "";
TemStr = TemStr.PadLeft(127, Convert.ToChar(" "));
APIClass.mciSendString("play media", TemStr, TemStr.Length, 0);
mc.state = State.mPlaying;
}
//停止
public void StopT()
{
TemStr = "";
TemStr = TemStr.PadLeft(128, Convert.ToChar(" "));
ilong = APIClass.mciSendString("close media", TemStr, 128, 0);
ilong = APIClass.mciSendString("close all", TemStr, 128, 0);
mc.state = State.mStop;
}

public void Puase()
{
TemStr = "";
TemStr = TemStr.PadLeft(128, Convert.ToChar(" "));
ilong = APIClass.mciSendString("pause media", TemStr, TemStr.Length, 0);
mc.state = State.mPuase;
}
private string GetCurrPath(string name)
{
if (name.Length < 1) return "";
name = name.Trim();
name = name.Substring(0, name.Length - 1);
return name;
}
//总时间
public int Duration
{
get
{
rLength = "";
rLength = rLength.PadLeft(128, Convert.ToChar(" "));
APIClass.mciSendString("status media length", rLength, rLength.Length, 0);
rLength = rLength.Trim();
if (rLength == "") return 0;
return (int)(Convert.ToDouble(rLength) / 1000f);
}
}

//当前时间
public int CurrentPosition
{
get
{
rLength = "";
rLength = rLength.PadLeft(128, Convert.ToChar(" "));
APIClass.mciSendString("status media position", rLength, rLength.Length, 0);
rLength = rLength.Trim();
mc.iPos = (int)(Convert.ToDouble(rLength) / 1000f);
return mc.iPos;
}
}
public bool IsEnd()
{
rLength = "";
rLength = rLength.PadLeft(128, Convert.ToChar(" "));
APIClass.mciSendString("status media length", rLength, rLength.Length, 0);
rLength = rLength.Trim();
string curLength;
curLength = "";
curLength = curLength.PadLeft(128, Convert.ToChar(" "));
APIClass.mciSendString("status media position", curLength, curLength.Length, 0);
curLength = curLength.Trim();
if (curLength == rLength)
{
return true;
}
return false;
}
}
public class APIClass
{
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern int GetShortPathName(
string lpszLongPath,
string shortFile,
int cchBuffer
);

[DllImport("winmm.dll", EntryPoint = "mciSendString", CharSet = CharSet.Auto)]
public static extern int mciSendString(
string lpstrCommand,
string lpstrReturnString,
int uReturnLength,
int hwndCallback
);
}
}

热心网友 时间:2023-10-24 15:52

在工具中选"添加引用",然后算com,找到mediaPlayer控件,然后在工具箱中找到它,就OK了,余下很简单,你应该会的

热心网友 时间:2023-10-24 15:52

可以利用 mediaPlayer 控件

热心网友 时间:2023-10-24 15:51

用API函数就行,新建个类文件把下面的代码粘进去在调用play方法就行了(先给FileName属性赋值)
调用方法
Test.Mp3 mp3 = new Test.Mp3();
mp3.FileName = xxx;
mp3.play();

类代码
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace Test
{
class Mp3
{
public Mp3()
{
}
//定义API函数使用的字符串变量
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
private string Name = "";
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
private string rLength = "";
[MarshalAs(UnmanagedType.LPTStr, SizeConst = 128)]
private string TemStr = "";
int ilong;
//定义播放状态枚举变量
public enum State
{
mPlaying = 1,
mPuase = 2,
mStop = 3
};
//结构变量
public struct structMCI
{
//public bool bMut;
//public int iDur;
//public int iVol;
//public int iBal;
public string iName;
public State state;
public int iPos;
};

public structMCI mc = new structMCI();

//取得播放文件属性
public string FileName
{
get
{
return mc.iName;
}
set
{
//ASCIIEncoding asc = new ASCIIEncoding();
try
{
TemStr = "";
TemStr = TemStr.PadLeft(127, Convert.ToChar(" "));
Name = Name.PadLeft(260, Convert.ToChar(" "));
mc.iName = value;
ilong = APIClass.GetShortPathName(mc.iName, Name, Name.Length);
Name = GetCurrPath(Name);
//Name = "open " + Convert.ToChar(34) + Name + Convert.ToChar(34) + " alias media";
Name = "open " + Convert.ToChar(34) + Name + Convert.ToChar(34) + " alias media";
ilong = APIClass.mciSendString("close all", TemStr, TemStr.Length, 0);
ilong = APIClass.mciSendString(Name, TemStr, TemStr.Length, 0);
ilong = APIClass.mciSendString("set media time format milliseconds", TemStr, TemStr.Length, 0);
mc.state = State.mStop;
}
catch
{
MessageBox.Show("出错错误!");
}
}
}
//播放
public void play()
{
TemStr = "";
TemStr = TemStr.PadLeft(127, Convert.ToChar(" "));
APIClass.mciSendString("play media", TemStr, TemStr.Length, 0);
mc.state = State.mPlaying;
}
//停止
public void StopT()
{
TemStr = "";
TemStr = TemStr.PadLeft(128, Convert.ToChar(" "));
ilong = APIClass.mciSendString("close media", TemStr, 128, 0);
ilong = APIClass.mciSendString("close all", TemStr, 128, 0);
mc.state = State.mStop;
}

public void Puase()
{
TemStr = "";
TemStr = TemStr.PadLeft(128, Convert.ToChar(" "));
ilong = APIClass.mciSendString("pause media", TemStr, TemStr.Length, 0);
mc.state = State.mPuase;
}
private string GetCurrPath(string name)
{
if (name.Length < 1) return "";
name = name.Trim();
name = name.Substring(0, name.Length - 1);
return name;
}
//总时间
public int Duration
{
get
{
rLength = "";
rLength = rLength.PadLeft(128, Convert.ToChar(" "));
APIClass.mciSendString("status media length", rLength, rLength.Length, 0);
rLength = rLength.Trim();
if (rLength == "") return 0;
return (int)(Convert.ToDouble(rLength) / 1000f);
}
}

//当前时间
public int CurrentPosition
{
get
{
rLength = "";
rLength = rLength.PadLeft(128, Convert.ToChar(" "));
APIClass.mciSendString("status media position", rLength, rLength.Length, 0);
rLength = rLength.Trim();
mc.iPos = (int)(Convert.ToDouble(rLength) / 1000f);
return mc.iPos;
}
}
public bool IsEnd()
{
rLength = "";
rLength = rLength.PadLeft(128, Convert.ToChar(" "));
APIClass.mciSendString("status media length", rLength, rLength.Length, 0);
rLength = rLength.Trim();
string curLength;
curLength = "";
curLength = curLength.PadLeft(128, Convert.ToChar(" "));
APIClass.mciSendString("status media position", curLength, curLength.Length, 0);
curLength = curLength.Trim();
if (curLength == rLength)
{
return true;
}
return false;
}
}
public class APIClass
{
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern int GetShortPathName(
string lpszLongPath,
string shortFile,
int cchBuffer
);

[DllImport("winmm.dll", EntryPoint = "mciSendString", CharSet = CharSet.Auto)]
public static extern int mciSendString(
string lpstrCommand,
string lpstrReturnString,
int uReturnLength,
int hwndCallback
);
}
}

热心网友 时间:2023-10-24 15:52

在工具中选"添加引用",然后算com,找到mediaPlayer控件,然后在工具箱中找到它,就OK了,余下很简单,你应该会的

热心网友 时间:2023-10-24 15:52

可以利用 mediaPlayer 控件
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
iPhone XR电量百分比怎么设置 Django如何使用网页嵌套网页(2023年最新解答) 社保之前工作缴了 后面工作不缴。自己要怎么续上?自己缴要缴多少?每... 壁挂炉数字不亮了怎么回事了解壁挂炉数字不亮的原因及解决方法_百度知 ... 单反长镜头和短镜头什么区别 ai的剪切蒙版快捷键是什么 自学当程序员需要哪些基础 程序员,需要学什么课程 想当一名出色的程序员数学必须要很好吗 华为路由器192.168.1.1怎样设置? 如何使用C#winForm做音乐播放器 winform程序中如何播放MP4文件 C#winform 怎么播放amr音频文件 C# winform中如果实现播放视频功能 心血管神经官能症如何治疗 心脏神经官能症怎么治疗 心脏官能症怎么治疗? 心脏神经官能症要做些什么锻炼? 心脏神经官能症需要吃药进行调理吗? 心脏官能症要如何进行治疗? 小学生暑假学习作息表? 小学生暑假作息思维导图怎么画? 经常跑步会伤膝关节吗?该怎么预防膝关节损伤? 有人说跑步时间太长会对膝盖造成伤害,事实是怎样的呢? 跑步真的会伤膝盖吗? 天天跑步真的会对膝盖造成损伤吗 跑步会损伤膝盖吗? 有人说跑步时间太长也会对膝盖造成伤害,究竟是不是这样呢? 每天跑步5公里对膝盖有影响吗?平时要怎么保护膝盖呢? 经常跑步为什么损伤膝盖?会有什么后果? 求高手,C# WinForm怎么根据条件播放声音? 怎么在winform中播放mp4文件 C#winform程序怎么播放flash 如何使用C#winForm做音乐播放器拜托了各位 谢谢 winform能否实现在线播放视频的功能? C#winForm怎么实现,像暴风影音或者酷狗那样,从外面拖入文件就能播放? winform 怎么播放MP4? winform窗体怎么播放声音 如何在C# Winform中嵌套一个播放器,就好像PPS那种我想做一个播放视频的软件,请高手指点啊 c# winform 中自带播放系统的几种声音,如蜂鸣声,但是它只播放一次,怎... winform客户端播放服务器端wav文件 c#winform中如何使用Windows Media Player 控件自动播放下一首歌曲,事件写在哪里,麻烦具体一点的代码 c#Winform程序 播放两个音乐 C#winform如何判断Media Player是否播放完毕? winform中怎么让axwindowsmediaplayer重新播放 少女时代黑海事件起因是什么? 少女时代的黑海事件到底是怎么回事 少女时代黑海事件(具体。) 少女时代的三次黑海事件的详细过程? 请问2008年的少女时代黑海事件是怎么回事。