Вопрос

I have an application that plays a particular song for a specified time, as the result of a select.

I can already play the songs, but I can not set a time duration.

    public void PlaySound()
    {
        int i = 0;

        foreach (string musicFile in musicFiles)
        {
            Thread thrStopMusic = new Thread(ThreadTimer);
            player.SoundLocation = musicFile;
            musicExecuteTime = GetMusicDuration[i];
            player.Play();
            thrStopMusic.Start();
            thrStopMusic.Abort();
            i++;
        }
    }

 public void ThreadTimer()
    {
       Thread.Sleep(musicExecuteTime * 1000);
       StopSound();
    }

im using SoundPlayer class.

Это было полезно?

Решение 2

i think you can just do something like this. Play() uses a new thread to play the file so you should just need to 'pause' your thread for an amount of time before invoking Stop().

public void PlaySound()
{
    int i = 0;

    foreach (string musicFile in musicFiles)
    {
        player.SoundLocation = musicFile;
        player.Play();
        Thread.Sleep(1000 * GetMusicDuration[i])
        player.Stop();
        i++;
    }
}

Другие советы

Maybe i did not understand your intent correctly, but why do you even use a thread for your timing (also i guess StopSound()is not the appropriate method to call)? Why not just:

...
player.Play();
Thread.Sleep(musicExecuteTime * 1000);
player.Stop();
...
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top