gpt4 book ai didi

c# - 播放随机 mp3 文件未知错误

转载 作者:太空宇宙 更新时间:2023-11-03 22:20:38 25 4
gpt4 key购买 nike

这在我只调用该函数并播放文件时有效,但是如果我想从文件夹中选择一个随机 mp3 来播放,它会停止说“索引超出数组范围”。

任何人都可以找出问题所在吗?

public void playrandomsound()
{
if (mp3 != null)
{
mp3.Dispose();
}

var files = new DirectoryInfo(Application.StartupPath).GetFiles(".mp3");
int index = new Random().Next(0, files.Length);


string filename = files[index].Name.ToString(); <-- App stops here

string tune = string.Concat(Application.StartupPath, "//", fiilename);
mp3 = new Audio(tune);
mp3.Play();



}

如果我删除错误行上的“tostring”方法,它仍然会给出相同的错误。

最佳答案

正如 Skurmedel 所说,您需要防止出现空集合。但是,您遇到此问题的原因是您的收藏中永远没有任何 .mp3 文件。另一个错误在这里:.GetFiles(".mp3")。你的意思是 .GetFiles("*.mp3")

关于c# - 播放随机 mp3 文件未知错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3209050/

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