Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have a list of sound file info that its item modified in two thread by raised an event and i have a timer that check this list if an file be in list that need to play will pass to play method and play it, it work correctly but when an file is playing and another file added or removed the checker timer doesn't start again

this is my code:
C#
class MediaInfo
{
    public string FileName { get; set; }
    public bool PlayState { get; set; }
}

public class test
{
    List<MediaInfo> MediaFile = new List<MediaInfo>();
    System.Timers.Timer avilbeMediaChecher;
    public test()
    {
        avilbeMediaChecher = new System.Timers.Timer(5000);
        avilbeMediaChecher.Elapsed += avilbeMediaChecher_Elapsed;
        avilbeMediaChecher.Start();
    }
    //this method calls from another thread when an event ocuring
    public void AddMedia(MediaInfo info)
    {
        MediaFile.Add(info);
    }
    //this method calls form another thread when an event ocuring
    public void Remove(MediaInfo info)
    {
        MediaFile.Remove(info);
    }
    void avilbeMediaChecher_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        try
        {
            foreach (var item in MediaFile)
            {
                if (!item.PlayState)
                {
                    var idx = MediaFile.FindIndex(f => f == item);
                    item.PlayState = true;
                    MediaFile[idx] = item;
                    Play(item);

                }
            }
        }
        catch (InvalidOperationException)
        { }
    }

    private void Play(MediaInfo item)
    {
        WindowsMediaPlayer wmp = new WindowsMediaPlayer();
        wmp.PlayStateChange += wmp_PlayStateChange;
        wmp.currentPlaylist.clear();

        wmp.currentPlaylist.appendItem(wmp.newMedia(Environment.CurrentDirectory + @"\sound\1.wma"));

        wmp.currentPlaylist.appendItem(wmp.newMedia(item.FileName.Trim()));

        wmp.currentPlaylist.appendItem(wmp.newMedia(Environment.CurrentDirectory + @"\sound\2.wma"));
        wmp.controls.play();
        avilbeMediaChecher.Stop();
    }
    int palayedCount = 1;
    private void wmp_PlayStateChange(int NewState)
    {
        if (NewState == (int)WMPLib.WMPPlayState.wmppsMediaEnded)
            palayedCount++;

        if (NewState == (int)WMPLib.WMPPlayState.wmppsMediaEnded && palayedCount == 3)
        {
            palayedCount = 1;
            avilbeMediaChecher.Start();
        }
    }
}
Posted

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