简体   繁体   English

使用NAudio 1.7+播放音频文件

[英]Playing Audio File using NAudio 1.7+

I was having issues playing back network streams as well as audio files using AAC and MP3 using NAudio. 我在使用NAudio使用AAC和MP3播放网络流以及音频文件时遇到了问题。 Took a while to figure out but the below solution works. 花了一段时间才弄明白,但下面的解决方案有效。

Hope to helps anyone else that is having a hard time utilizing NAudio properly. 希望能帮助其他正在努力利用NAudio的人。

//Create Output Stream with Data from Audio File / Network Stream
WaveOutputStream outputStream = new MediaFoundationReader("Path to File"); 
//Create Volume Stream to control volume of output 
//ex: volumeStream.Volume = 0.5f; Float between 0 & 1 
WaveChannel32 volumeStream = new WaveChannel32(outputStream);
//Create WaveOutEvent since it works in Background and UI Threads
WaveOutEvent player = new WaveOutEvent();
//Init Player with Configured Volume Stream
player.Init(volumeStream);
player.Play();

This code can play any audio file that MediaFoundationReader supports (MP3,AAC,WAV) as well as network streams of these codecs. 此代码可以播放MediaFoundationReader支持的任何音频文件(MP3,AAC,WAV)以及这些编解码器的网络流。 To reuse the above player call Dispose() on player,outputStream, and volumeStream then set each to null. 要重复使用上面的播放器,请在player,outputStream和volumeStream上调用Dispose(),然后将每个设置为null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM