I need to play mp3 file. I want to use winmm.dll (Windows 7)
class Program
{
[DllImport("winmm.dll")]
private static extern long mciSendString(string Cmd, StringBuilder StrReturn, int ReturnLength, IntPtr HwndCallback);
开发者_如何转开发 static void Main(string[] args)
{
string FileName = @"F:\MUSIC\ROCK.mp3";
string CommandString = "open " + "\"" + FileName + "\"" + " type mpegvideo alias Mp3File";
mciSendString(CommandString, null, 0, IntPtr.Zero);
CommandString = "play Mp3File";
mciSendString(CommandString, null, 0, IntPtr.Zero);
Console.ReadKey();
}
}
But when I run my program, nothing happened. Where is a mistake?
The accepted answer will not work for file paths that contains spaces. The correct method is to use the alias you set up in the open command:
string FileName = @"F:\MUSIC\ROCK.mp3";
mciSendString("open \"" + FileName + "\" type mpegvideo alias thisIsMyTag", null, 0, IntPtr.Zero);
mciSendString("play thisIsMyTag from 0", null, 0, IntPtr.Zero);
string FileName = @"F:\MUSIC\ROCK.mp3";
mciSendString("open \"" + FileName + "\" type mpegvideo alias MediaFile", null, 0, IntPtr.Zero);
mciSendString("play " + FileName + " from 0", null, 0, IntPtr.Zero);
It works correctly.
Your command string appears to have an incorrect type.
You're passing type mpegvideo
, but the file is not a video file.
The correct type for audio is type waveaudio
for *.wav files, type sequencer
for *.mid files, type cdaudio
for RedBook CD. I don't see any way to play MP3 with MCI. You could try leaving the type
clause out entirely, then MCI will try to detect it.
Also, you should capture the error code returned by mciSendString
, it may give you more information.
MSDN Reference
Here,
class Program
{
public string _command;
public bool isOpen;
[DllImport("winmm.dll")]
public static extern long mciSendString(string strCommand,StringBuilder strReturn,int iReturnLength, IntPtr hwndCallback);
static void Main(string[] args)
{
string FileName = @"F:\MUSIC\ROCK.mp3";
string _command = "open \"" + sFileName + "\" type mpegvideo alias MediaFile";
mciSendString(_command, null, 0, IntPtr.Zero);
isOpen = true;
if(isOpen)
{
_command = "play MediaFile";
if (loop)
_command += " REPEAT";
mciSendString(_command, null, 0, IntPtr.Zero);
}
/*For Close the audio
_command = "close MediaFile";
mciSendString(_command, null, 0, IntPtr.Zero);
isOpen = false; */
}
}
CommandString should be "play Mediafile" instead of "play Mp3file" Hope It will help .. =]
精彩评论