需要找到音频文件的持续时间。
问题描述:
请找到找到特定音频文件(.wav文件)的持续时间的代码
Please find code of finding the duration of particular audio file (.wav file)which we have
答
using System;
using System.Text;
using System.Runtime.InteropServices;
namespace Sound
{
public static class SoundInfo
{
[DllImport("winmm.dll")]
private static extern uint mciSendString(
string command,
StringBuilder returnValue,
int returnLength,
IntPtr winHandle);
public static int GetSoundLength(string fileName)
{
StringBuilder lengthBuf = new StringBuilder(32);
mciSendString(string.Format("open \"{0}\" type waveaudio alias wave", fileName), null, 0, IntPtr.Zero);
mciSendString("status wave length", lengthBuf, lengthBuf.Capacity, IntPtr.Zero);
mciSendString("close wave", null, 0, IntPtr.Zero);
int length = 0;
int.TryParse(lengthBuf.ToString(), out length);
return length;
}
}
}
您可以查看一下:
http:// sourcecodecenter .blogspot.com.au / 2011/04 / c-get-time-duration-of-audio-file.html [ ^ ]
You may like to check this out:
http://sourcecodecenter.blogspot.com.au/2011/04/c-get-time-duration-of-audio-file.html[^]
Hello Ayush,
检查这个帖子,这可能对你有所帮助。
问候..
Edited => => => (另一种方式)
以下作品,请查看
(您需要添加对PresentationCore和WindowsBase的引用,您可以在.NET选项卡中找到它们)
Hello Ayush,
Check this thread out, this might help you.
Regards..
Edited => => => (Another Way)
Following works, check it out
(you'll need to add references to PresentationCore and WindowsBase, you can find them in .NET tab)
MediaPlayer mplayer = new MediaPlayer();
public MainWindow()
{
InitializeComponent();
}
private void btnPlay_Click(object sender, RoutedEventArgs e)
{
Uri path = new Uri(@"BigBounce.wav");
mplayer.Open(path);
mplayer.Play();
Thread.Sleep(1000);//if we ommit this, TimeSpan will be NULL,
//because for TimeSpan to have a value, file should be playing.
if (mplayer.NaturalDuration.HasTimeSpan)
{
lblLength.Text = mplayer.NaturalDuration.TimeSpan.ToString();
}
}
private void btnStop_Click(object sender, RoutedEventArgs e)
{
mplayer.Stop();
mplayer.Close();
}