使用n音讯播放.wav文件,播放1秒后停止 [英] Playing a .wav file using naudio, playback stops after 1 sec
问题描述
我用在C#中的n音讯lib和想打一个简单的文件。问题是,在1秒钟后的播放停止。我无法弄清楚它之所以这样做的。
使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;
使用NAudio.Wave;
命名空间NAudioTest
{
类节目
{
静态IWavePlayer waveout的;
静态的WaveStream的OutputStream;
静态字符串文件名= NULL;
静态无效的主要(字串[] args)
{
waveout的=新waveout的();
文件名=C:\\ 1.wav;
的OutputStream = CreateInputStream(文件名);
尝试
{
waveout.Init(OutputStream的);
}
赶上(例外前)
{
Console.WriteLine(错误而载入输出);
Console.WriteLine(详细信息:+ ex.Message);
到Console.ReadLine();
返回;
}
Console.WriteLine(preSS [Enter]键启动播放);
到Console.ReadLine();
waveout.Play(); // 1秒后,该停止。应该发挥它,直到我打进入下一行的原因是什么?
Console.WriteLine(preSS [Enter]键中止);
到Console.ReadLine();
waveout.Dispose();
到Console.ReadLine();
}
静态的WaveStream CreateInputStream(字符串名称)
{
WaveChannel32的InputStream;
如果(name.EndsWith(。WAV))
{
的WaveStream readerStream =新WaveFileReader(名称);
如果(readerStream.WaveFormat.Encoding!= WaveFormatEncoding.Pcm)
{
readerStream = WaveFormatConversionStream.CreatePcmStream(readerStream);
readerStream =新BlockAlignReductionStream(readerStream);
}
如果(readerStream.WaveFormat.BitsPerSample!= 16)
{
VAR格式=新WAVEFORMAT(readerStream.WaveFormat.SampleRate,16,readerStream.WaveFormat.Channels);
readerStream =新WaveFormatConversionStream(格式,readerStream);
}
的InputStream =新WaveChannel32(readerStream);
}
其他
{
抛出新的InvalidOperationException异常(无效的扩展名);
}
返回InputStream的;
}
}
}
您需要确保您使用的函数回调,如果你正试图从一个控制台应用程序播放音频,因为默认为waveout的是使用窗口回调。
新waveout的(WaveCallbackInfo.FunctionCallback())
更新:对于较新的版本n音讯的我现在建议您避免函数回调,因为它们会导致死锁在特定的驱动。相反, WaveOutEvent
,它使用事件回调,并在后台线程是preferred机制:
新WaveOutEvent()
I'm using the naudio lib in C# and want to play a simple file. The problem is, the playback stops after 1 second. I cant figure out the reason why it does that.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NAudio.Wave;
namespace NAudioTest
{
class Program
{
static IWavePlayer waveout;
static WaveStream outputStream;
static string filename = null;
static void Main(string[] args)
{
waveout = new WaveOut();
filename = "C:\\1.wav";
outputStream = CreateInputStream(filename);
try
{
waveout.Init(outputStream);
}
catch (Exception ex)
{
Console.WriteLine("Error while loading output");
Console.WriteLine("Details: " + ex.Message);
Console.ReadLine();
return;
}
Console.WriteLine("Press [Enter] to start playback");
Console.ReadLine();
waveout.Play(); //this stops after 1 sec. should it play until i hit enter cause of the next line?
Console.WriteLine("Press [Enter] to abort");
Console.ReadLine();
waveout.Dispose();
Console.ReadLine();
}
static WaveStream CreateInputStream(string name)
{
WaveChannel32 inputStream;
if (name.EndsWith(".wav"))
{
WaveStream readerStream = new WaveFileReader(name);
if (readerStream.WaveFormat.Encoding != WaveFormatEncoding.Pcm)
{
readerStream = WaveFormatConversionStream.CreatePcmStream(readerStream);
readerStream = new BlockAlignReductionStream(readerStream);
}
if (readerStream.WaveFormat.BitsPerSample != 16)
{
var format = new WaveFormat(readerStream.WaveFormat.SampleRate, 16, readerStream.WaveFormat.Channels);
readerStream = new WaveFormatConversionStream(format, readerStream);
}
inputStream = new WaveChannel32(readerStream);
}
else
{
throw new InvalidOperationException("Invalid extension");
}
return inputStream;
}
}
}
You need to make sure you are using function callbacks if you are trying to play audio from a console app, since the default for WaveOut is to use window callbacks.
new WaveOut(WaveCallbackInfo.FunctionCallback())
Update: With newer versions of NAudio I now recommend that you avoid function callbacks, as they can cause deadlocks with certain drivers. Instead, WaveOutEvent
which uses event callbacks and a background thread is the preferred mechanism:
new WaveOutEvent()
这篇关于使用n音讯播放.wav文件,播放1秒后停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!