【问题标题】:C# - Playing random sound files from folder [closed]C# - 从文件夹播放随机声音文件[关闭]
【发布时间】:2014-01-23 08:38:00
【问题描述】:

我正在尝试创建一个 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),但我不确定如何在给定的场景中实现它。

【问题讨论】:

    标签: c# audio random directory enumeration


    【解决方案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】:
    // 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】:

      从文件夹中检索声音文件列表,然后只需在 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();
      

      【讨论】:

        【解决方案4】:

        试试this

        换行

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

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

        (未测试)

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-08-29
          • 1970-01-01
          • 1970-01-01
          • 2020-09-12
          • 2012-07-03
          • 2015-02-19
          相关资源
          最近更新 更多