Click here to Skip to main content
15,889,877 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please find code of finding the duration of particular audio file (.wav file)which we have
Posted
Comments
Ayush Nautiyal 24-Mar-14 3:50am    
I have used below mentioned coding for playing audio file..but it is unable to play...While playing i want time duration
....
SoundPlayer simpleSound = new SoundPlayer(@"E:\Ayush\audio.wma");
simpleSound.Play();

C#
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;
        }
    }
}
 
Share this answer
 
 
Share this answer
 
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)

C#
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();
}
 
Share this answer
 
v4
Comments
Ayush Nautiyal 24-Mar-14 6:44am    
This much i have done.
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string fileName = @"E:\Ayush\adios.wav";
FileInfo f = new FileInfo(fileName);
long s1 = f.Length;
Label2.Text = s1.ToString();
SoundPlayer player = new SoundPlayer(fileName);
{
player.LoadAsync();
player.PlaySync();
player.Stop();
}


}
}


but audio is not playing.
milindaSnow 24-Mar-14 8:20am    
remove player.Stop() and place it in a separate button click event.
Ayush Nautiyal 24-Mar-14 8:31am    
But again it is not working.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900