C# - Playing random sound files from folder [close

2019-02-28 23:37发布

问题:

I'm attempting to create an Oracle (read: Magic 8 Ball). The idea behind it is that on each button press, a sound file with wise words is played (picked at random). I have it working using switches, however I'm searching for a way to make it more.. logical.

This is how it currently looks, with the switches going on and on:

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;

Surely there is a way to program it as such so that it looks in a given folder, then picks a random file, without having said file stated in the program itself (like how it was done with switches). I stumbled upon folder enumeration (http://code.msdn.microsoft.com/windowsapps/Folder-enumeration-sample-33ebd000), but I'm uncertain as to how to implement it in my given scenario.

回答1:

If you have a specific folder you could do something like this;

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:

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


回答3:

Retrieve a list of soundfiles from the folder, then just pick a random number between 0 and list.Length-1 and select that file.

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


回答4:

Try this

and change the line

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

to

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

(not Tested)

Then you should have an Array with all the .wav Files.