如何通过参数在基于C#控制台的应用程序中播放5.1 .wav
或.mp3
?
像这样:soundplayer.exe C:\sound\5.1.mp3
会播放文件并关闭控制台窗口。
我有这个:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Media;
namespace AudioPlayer
{
class Program
{
static void Main(string[] args)
{
SoundPlayer simpleSound = new SoundPlayer(@"c:\Windows\Media\chimes.wav");
simpleSound.Play();
}
}
}
但我不确定如何从参数中获取文件。
而且它不会播放代码中嵌入的文件。
程序打开和关闭,并且vs debug显示
The thread 0x243c has exited with code 0 (0x0).
最佳答案
发生此问题是因为Play()的SoundPlayer方法未阻止,因此在播放文件之前关闭了命令窗口。
请改用PlaySync()方法。
static void Main(string[] args)
{
var simpleSound = new SoundPlayer(@"C:\Windows\Media\chimes.wav");
simpleSound.PlaySync();
}
播放作为第一个参数提供的声音:
var simpleSound = new SoundPlayer(args[0]);
simpleSound.PlaySync();
在CMD中:
YourSoundPlayer.exe "C:\Windows\Media\chimes.wav"
关于c# - 如何从C#控制台应用程序中的参数播放5.1声音文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42537449/