需要找到音频文件的持续时间。 [英] Need to find the duration of audio file.

查看:84
本文介绍了需要找到音频文件的持续时间。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请找到找到特定音频文件(.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();
}


这篇关于需要找到音频文件的持续时间。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆