C# - 播放文件夹中的随机声音文件

时间:2014-01-23 08:38:00

标签: c# audio random directory enumeration

我正在尝试创建一个Oracle(阅读:Magic 8 Ball)。 其背后的想法是,在每个按钮按下时,播放具有明智单词的声音文件(随机选取)。 我让它使用开关工作,但是我正在寻找一种方法来使它更符合逻辑。

这是当前的样子,开关一直打开:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _8ball
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Random rnd = new Random(Guid.NewGuid().GetHashCode());
            int choices = rnd.Next(0, 62);
            switch(choices)
            { 
                case 0:
                    System.Media.SoundPlayer player = new System.Media.SoundPlayer(@"c:\Lyde\0.wav");
                    player.Play();
                    break;
                case 1:
                    System.Media.SoundPlayer player1 = new System.Media.SoundPlayer(@"c:\Lyde\1.wav");
                    player1.Play();
                    break;
                case 2:
                    System.Media.SoundPlayer player2 = new System.Media.SoundPlayer(@"c:\Lyde\2.wav");
                    player2.Play();
                    break;
                case 3:
                    System.Media.SoundPlayer player3 = new System.Media.SoundPlayer(@"c:\Lyde\3.wav");
                    player3.Play();
                    break;

当然有一种方法可以对它进行编程,以便它在给定的文件夹中查找,然后选择一个随机文件,而不需要在程序本身中说明所述文件(就像使用开关一样)。 我偶然发现了文件夹枚举(http://code.msdn.microsoft.com/windowsapps/Folder-enumeration-sample-33ebd000),但我不知道如何在我给定的场景中实现它。

4 个答案:

答案 0 :(得分:2)

// List of files from directory, sorted by *.wav type.
string[] filePaths = Directory.GetFiles(@"F:\Tankat\Music", "*.wav",
                                     SearchOption.AllDirectories);

// Random number from 0 to the amount of files you have
Random rnd = new Random(Guid.NewGuid().GetHashCode());
int choices = rnd.Next(filePaths.Length);

// Create a new player with a random filepath from the array
SoundPlayer player = new SoundPlayer(filePaths[choices]);
player.Play();

答案 1 :(得分:1)

如果你有一个特定的文件夹,你可以做这样的事情;

var soundsRoot = @"c:\lyde";
var rand = new Random();
var soundFiles = Directory.GetFiles(sounds, "*.wav");
var playSound = soundFiles[rand.Next(0, soundFiles.Length)];
System.Media.SoundPlayer player1 = new System.Media.SoundPlayer(playSound);

答案 2 :(得分:1)

从文件夹中检索声音文件列表,然后选择介于0和list.Length-1之间的随机数并选择该文件。

//Untested code, but should give you an idea.
string[] files = Directory.GetFiles("path");
Random rnd = new Random(Guid.NewGuid().GetHashCode());
int choice = rnd.Next(0, files.Length - 1);
string soundFile = files[choice];
System.Media.SoundPlayer player = new System.Media.SoundPlayer(soundFile);
player.Play();

答案 3 :(得分:0)

尝试this

并更改行

string[] dirs = Directory.GetFiles(@"c:\", "c*");

string[] dirs = Directory.GetFiles(@"c:\Lyde\", "c:\Lyde\*.wav");

(未经测试)

然后你应该有一个包含所有.wav文件的数组。