C#的winform程序如何实现mp3文件的播放啊?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#的winform程序如何实现mp3文件的播放啊?相关的知识,希望对你有一定的参考价值。
在C#的winform程序中,当点击一个按钮时,播放一首Mp3歌曲。
但不是把Mp3播放器弹出来的那种。
我是个菜鸟,刚学C#,请各位大侠指教啊!
最好加上注释,谢谢!
调用方法
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 durLength = "";
[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
durLength = "";
durLength = durLength.PadLeft(128, Convert.ToChar(" "));
APIClass.mciSendString("status media length", durLength, durLength.Length, 0);
durLength = durLength.Trim();
if (durLength == "") return 0;
return (int)(Convert.ToDouble(durLength) / 1000f);
//当前时间
public int CurrentPosition
get
durLength = "";
durLength = durLength.PadLeft(128, Convert.ToChar(" "));
APIClass.mciSendString("status media position", durLength, durLength.Length, 0);
durLength = durLength.Trim();
mc.iPos = (int)(Convert.ToDouble(durLength) / 1000f);
return mc.iPos;
public bool IsEnd()
durLength = "";
durLength = durLength.PadLeft(128, Convert.ToChar(" "));
APIClass.mciSendString("status media length", durLength, durLength.Length, 0);
durLength = durLength.Trim();
string curLength;
curLength = "";
curLength = curLength.PadLeft(128, Convert.ToChar(" "));
APIClass.mciSendString("status media position", curLength, curLength.Length, 0);
curLength = curLength.Trim();
if (curLength == durLength)
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
);
参考技术A 在工具中选"添加引用",然后算com,找到mediaPlayer控件,然后在工具箱中找到它,就OK了,余下很简单,你应该会的 参考技术B 可以利用 mediaPlayer 控件
如何在C# Winform中嵌套一个播放器,就好像PPS那种我想做一个播放视频的软件,请高手指点啊
还有个问题就是如何把Windows界面放入Winform界面中,程序在服务器上面查找文件,我只知道用System.Diagnostics.Process.Start,但是我想让界面直接在winform中打开,而不是弹出一个网页,该如何做。在线等
1. 你没法把类似pps的东西嵌入到winform上。2. 能嵌入到winform上的视频播放的东东可以是自己或在别人封装的一个专门做视频播放的组件或者控件。
3. 实际上拿个panel放在winform上就可以当做一个视频界面,只是需要你自己把视频图像弄到那个panel上面去。至于弄上去的方法,ms有个avicap32.dll,具体使用方法可以网上找。 参考技术A 你可以在winform里面嵌入一个ActiveX组件,如Windows Media Player,这个.NET自带就有,可以在添加引用->Com组件里找到追问
也许我播放的视频是Windows Media Player不支持的怎么办呢。 有什么办法解决吗。
追答那就用别的播放器组件啊,原理相似的吧,或者用第三方的开发工具,这个网上开源和付费的dll都很多的
本回答被提问者采纳以上是关于C#的winform程序如何实现mp3文件的播放啊?的主要内容,如果未能解决你的问题,请参考以下文章
C#中axWindowsMediaPlayer1怎么播放mp3文件,能不能给个代码啊
用C#或C++MFC之类的,实现一个简单音乐播放功能:输入字符串,播放 字符串.mp3