gpt4 book ai didi

c# - 在队列中播放 .wav 文件

转载 作者:行者123 更新时间:2023-12-03 13:20:44 27 4
gpt4 key购买 nike

尝试在线程安全队列中播放 .wav 文件。任何线程都可以随机触发调用,但必须按顺序播放。问题是当我调用 Play(string[] files)假设有 3 个文件名(目前正在从 UI 线程测试),最后一个文件播放了 3 次,而第一个文件从未播放过。看不出这是什么原因造成的..?

希望提供更简单的方法来执行此操作,但主要想知道为什么这不起作用。

public class Audio
{
static ActionQueue actionQueue;
public static string MediaFolder { get; set; }
private static object syncroot = new object();

static Audio()
{
actionQueue = new ActionQueue();
}

/// <summary>
/// Plays .wav in async queue.
/// </summary>
/// <param name="fileName"></param>
public static void Play(string fileName)
{
actionQueue.Add(() => PlaySound(fileName));
}

public static void Play(string[] files)
{
Console.WriteLine("ID0: " + Thread.CurrentThread.ManagedThreadId);
foreach (string f in files.ToList())
{
Console.WriteLine("Queue file: " + f);
actionQueue.Add(() => PlaySound(f));
}
}

private static void PlaySound(string f)
{
Console.WriteLine("ID1: " + Thread.CurrentThread.ManagedThreadId);

var fileName = f;

Console.WriteLine("Play queue: " + fileName);

fileName = Path.Combine(MediaFolder, fileName);

if (!Path.HasExtension(fileName))
fileName = fileName + ".wav";

if (!File.Exists(fileName)) return;
string ext = Path.GetExtension(fileName);
if (ext != ".wav") return;

Console.WriteLine("Playing: " + fileName);

SoundPlayer player = new SoundPlayer(fileName);
player.PlaySync();
}
}

public class ActionQueue
{
private BlockingCollection<Action> persisterQueue = new BlockingCollection<Action>();

public ActionQueue( )
{
var thread = new Thread(ProcessWorkQueue);
thread.IsBackground = true;
thread.Start();
}

private void ProcessWorkQueue()
{
while (true)
{
var nextWork = persisterQueue.Take();
nextWork();
}
}

public void Add(Action action)
{
persisterQueue.Add(action );
}
}

最佳答案

经典captured-loop-variable-in-a-closure问题。

您需要复制循环变量,即

    foreach (string f in files.ToList())
{
var copy = f;
Console.WriteLine("Queue file: " + f);
actionQueue.Add(() => PlaySound(copy));
}

原因是您的代表被传递给 actionQueue在循环完成之前不会执行。当然,到那个时候,变量 f已经改变了值(value)。

关于c# - 在队列中播放 .wav 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15006698/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com