Skip to content

Instantly share code, notes, and snippets.

@ksasao
Created January 6, 2018 12:30
Show Gist options
  • Save ksasao/a69a492f4d4773eb388d33976f8293ff to your computer and use it in GitHub Desktop.
Save ksasao/a69a492f4d4773eb388d33976f8293ff to your computer and use it in GitHub Desktop.
音を鳴らすスピーカーを選択して音声ファイルを再生。数字,ファイル名、でそのスピーカーから音を再生。w,ミリ秒 でwミリ秒待ち。ビルドするときは、NAudio を nuget で追加してください。
using NAudio.Wave;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace MultiSpeaker
{
public class PlayScript
{
string[] _list;
public PlayScript(string filename)
{
Initialize(filename);
}
public void Play()
{
for(int i=0; i<_list.Length; i++)
{
Console.WriteLine(_list[i]);
string[] d = _list[i].Split(',');
if(d.Length == 2)
{
if (d[0].Trim() == "w")
{
Thread.Sleep(Convert.ToInt32(d[1]));
}
else
{
PlaySound(d[1], Convert.ToInt32(d[0]));
}
}
}
}
public void PlaySound(string filePath, int index)
{
var waveReader = new NAudio.Wave.WaveFileReader(filePath);
var waveOut = new NAudio.Wave.WaveOut();
waveOut.DeviceNumber = index;
waveOut.Init(waveReader);
waveOut.Play();
}
private void Initialize(string filename)
{
_list = File.ReadAllLines(filename);
string[] device = GetDevices();
for(int i = 0; i < device.Length; i++)
{
Console.WriteLine($"{i} = {device[i]}");
}
}
private string[] GetDevices()
{
List<string> deviceList = new List<string>();
for (int i = 0; i < WaveOut.DeviceCount; i++)
{
var capabilities = WaveOut.GetCapabilities(i);
deviceList.Add(capabilities.ProductName);
}
return deviceList.ToArray();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MultiSpeaker
{
class Program
{
static void Main(string[] args)
{
var p = new PlayScript("script.txt");
p.Play();
Console.ReadKey();
}
}
}
0,1.wav
w,5000
0,2.wav
w,5000
0,3.wav
w,5000
0,4.wav
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment