如何播放音乐文件指定的时间

发布于 2024-12-21 15:43:24 字数 83 浏览 0 评论 0原文

我想做的是播放音乐文件指定的持续时间,然后停止播放。但是,正在播放整个音乐文件。有什么想法吗?

我尝试过启动一个新线程,但仍然不起作用。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

妳是的陽光 2024-12-28 15:43:24

问题是 PlaySync 会阻塞线程,因此其他消息不会被处理。这包括来自 Tick 事件的停止命令。您必须使用常规的 Play 函数,该函数将是异步的,并创建一个新线程来播放文件。您必须根据应用程序的工作方式来处理由此产生的多线程情况。

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.

祁梦 2024-12-28 15:43:24

我会构建大约像这样的东西:它只是在编辑窗口中手写的,所以不要指望它会像那样编译。它只是为了说明这个想法。

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();
        }
    }
}

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();
        }
    }
}
两仪 2024-12-28 15:43:24

试试这个:

ThreadPool.QueueUserWorkItem(o => {
                                    note.Play();
                                    Thread.Sleep(1000);
                                    note.Stop();
                                   });

try this:

ThreadPool.QueueUserWorkItem(o => {
                                    note.Play();
                                    Thread.Sleep(1000);
                                    note.Stop();
                                   });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文