关于得到歌曲总时间
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.Win32;
using System.Runtime.InteropServices;
namespace _0
{
public partial class Form1 : Form
{// API 宣告
[DllImport("winmm.dll", EntryPoint = "mciSendString", CharSet = CharSet.Auto)]
public static extern int mciSendString(
string lpstrCommand, string lpstrReturnString,
int uReturnLength, int hwndCallback);
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object s, EventArgs e)
{
MessageBox.Show(GetMediaLen(@"E:\MP3\bjbl.mp3").ToString() + " Sec");
}
// 取得多媒體檔案長度
private long GetMediaLen(string File)
{
long RetVal = 0;
string key = "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\MCI Extensions";
RegistryKey RegKey = Registry.LocalMachine.OpenSubKey(key);
if (RegKey == null)
RegKey = Registry.LocalMachine.CreateSubKey(key);
string FileExt = Path.GetExtension(File).Replace(".", "");
string tp = RegKey.GetValue(FileExt, "MPEGVideo").ToString();
RegKey.Close();
string tm = new string((char)0, 128);
if (mciSendString("open \"" + File + "\" type " + tp + " alias Media", null, 0, 0) == 0)
{
if (mciSendString("status Media length", tm, tm.Length, 0) == 0)
{
tm = tm.Trim((char)0);
if (!string.IsNullOrEmpty(tm)) RetVal = Convert.ToInt64(tm) / 1000;
}
mciSendString("close Media", null, 0, 0);
}
return RetVal;
}
}
}
//大家好,上面的代码运行时没有提示错误,为什么得到的时间是0秒;麻烦大家一下,怎样修改才能得到歌曲的总时间;