C# - I want my player to delay 2 seconds before playing any file and repeat it in the loop(For)

298 Views Asked by At

Hi I have written the following code but only once plays the audio file after it leaves the "dataGridView1_CellClick" event. I want to know:


1) Can I be able to play sound within the event?


2) Can I repeat the broadcast without using "Player.settings.playCount" ? Because this code can not be delayed before the release of each file. Thanks

My Code Is:

WMPLib.WindowsMediaPlayer Player = new WMPLib.WindowsMediaPlayer();

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{

    //...

    //********** Play audio of Word
    // sVoicePath = @"C:\4536.mp3"
      sVoicePath = Application.StartupPath + dataGridView1.CurrentRow.Cells[4].Value.ToString();
      PlayFile(sVoicePath);
    //...
}

//*****************************
private void PlayFile(String url)
        {
            for (int i = 0; i < 3 ;i++)
            {
                System.Threading.Thread.Sleep(2000);
                Player.URL = url;
                Player.controls.play();
            }
        }
//*****************************
        private void Player_PlayStateChange(int NewState)
        {
            if ((WMPLib.WMPPlayState)NewState == 
                    WMPLib.WMPPlayState.wmppsStopped)
            {
                //Actions on stop
            }
        }
2

There are 2 best solutions below

1
Enigmativity On BEST ANSWER

You can use Microsoft's Reactive Framework (aka Rx). NuGet System.Reactive and add using System.Reactive.Linq to your code. Then you can do this:

private void PlayFile(String url)
{
    Observable
        .Interval(TimeSpan.FromSeconds(1.0))
        .Take(3)
        .Subscribe(x =>
        {
            Player.URL = url;
            Player.controls.play();
        });
}
0
shahram On

Thanks for your answer, your code has solved my problem.

But the sound is still playing when the run comes out of the "dataGridView1_CellClick" event. Can the sound be broadcast when the execution of the program is still in the PlayFile(String url) function?


To inform the other friends, I have to encounter the following Error to run this code:

Error CS0012 The type 'IObservable<>' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.

To solve this problem, I performed three steps, until the problem was solved.

1) install nuget-client-tools ( You can get, for example, from the following sites: " https://learn.microsoft.com/en-us/nuget/install-nuget-client-tools#nugetexe-cli " OR " https://www.nuget.org/downloads " OR ... )

Then in Package Manager Console Run two Below Command:

2) PM> Install-Package System.Reactive -Version 4.1.3

3) PM> Install-Package NETStandard.Library.NETFramework -Version 2.0.0-preview2-25405-01 -Pre or PM>Install-Package NETStandard.Library.NETFramework -Version 2.0.0-preview1-25305-02 -Pre OR Install-Package NETStandard.Library.NETFramework -Version 2.0.0-preview1-25305-02 ( You must be connected to Internet)