2

What I'm trying to do is play a music file for a specified duration, and then stop playing. However, the whole music file is being played. Any ideas?

I've tried starting a new thread, still doesnt work.

Dot NET
  • 4,891
  • 13
  • 55
  • 98

3 Answers3

0

The problem is that PlaySync blocks the thread, so other messages won't be processed. This includes your stop command from the Tick event. You have to use the regular Play function, which will be asynchronous and creates a new thread to play the file in. You will have to handle the resulting multithreading situation depending on how your application works.

Michael Yoon
  • 1,606
  • 11
  • 9
  • I've tried doing this, and updated the code above. Could you take a look please, as it's still not working? I'm guessing it's a threading problem. – Dot NET Dec 16 '11 at 09:13
  • What does the rest of your program do? Your code works for the most part, except you should Stop() and Dispose() your timer on ClockTick to stop the timer from firing repeatedly. If your program exits at the end then the file will never play, like if you have a console application that doesn't wait for any user input. – Michael Yoon Dec 20 '11 at 09:57
0

I would build something approximately like this: It is just written out of hand in the edit window so don't expect it to compile just like that. It is only meant to illustrate the idea.

internal class MusicPlayer
{
    private const int duration = 1000;
    private Queue<string> queue;
    private SoundPlayer soundPlayer;
    private Timer timer;

    public MusicPlayer(params object[] filenames)
    {
        this.queue = new Queue<string>();
        foreach (var filenameObject in filenames)
        {
            var filename = filenameObject.ToString();
            if (File.Exists(filename))
            {
                this.queue.Enqueue(filename);
            }
        }

        this.soundPlayer = new SoundPlayer();
        this.timer = new Timer();
        timer.Elapsed += new System.Timers.ElapsedEventHandler(ClockTick);
    }

    public event EventHandler OnDonePlaying;

    public void PlayAll()
    {
        this.PlayNext();
    }

    private void PlayNext()
    {
        this.timer.Stop();
        var filename = this.queue.Dequeue();
        this.soundPlayer.SoundLocation = filename;
        this.soundPlayer.Play();
        this.timer.Interval = duration;
        this.timer.Start();
    }

    private void ClockTick(object sender, EventArgs e)
    {
        if (queue.Count == 0 ) {
            this.soundPlayer.Stop();
            this.timer.Stop();
            if (this.OnDonePlaying != null)
            {
                this.OnDonePlaying.Invoke(this, new EventArgs());
            }
        }
        else 
        {
            this.PlayNext();
        }
    }
}
Jaapjan
  • 3,365
  • 21
  • 25
  • Thanks for your suggestion - I'm trying to use threads for now. Do you think you could take a look at the edited code please? – Dot NET Dec 16 '11 at 09:21
0

try this:

ThreadPool.QueueUserWorkItem(o => {
                                    note.Play();
                                    Thread.Sleep(1000);
                                    note.Stop();
                                   });
Sebastian Piu
  • 7,838
  • 1
  • 32
  • 50