Библиотека NAudio - C#
Формулировка задачи:
Как реализовать с помощью библиотеки NAudio воспроизведение куска файла с определённой позиции определённой длинны ( позиция уже реализована, осталось длину )?
namespace techno_pranker
{
public partial class Form1 : Form
{
IWavePlayer waveOutDevice;
WaveStream mainOutputStream;
WaveChannel32 volumeStream;
private WaveStream CreateInputStream(string fileName)
{
WaveChannel32 inputStream;
if (fileName.EndsWith(".mp3"))
{
WaveStream mp3Reader = new Mp3FileReader(fileName);
inputStream = new WaveChannel32(mp3Reader);
}
else
{
throw new InvalidOperationException("Unsupported extension");
}
volumeStream = inputStream;
return volumeStream;
}
private void CloseWaveOut()
{
if (waveOutDevice != null)
{
waveOutDevice.Stop();
}
if (mainOutputStream != null)
{
// this one really closes the file and ACM conversion
volumeStream.Close();
volumeStream = null;
// this one does the metering stream
mainOutputStream.Close();
mainOutputStream = null;
}
if (waveOutDevice != null)
{
waveOutDevice.Dispose();
waveOutDevice = null;
}
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
waveOutDevice = new WaveOut();
}
private void button1_Click(object sender, EventArgs e)
{
mainOutputStream = CreateInputStream("C:\\1.mp3");
//начинает проигрывать с 1.5 секунды
mainOutputStream.CurrentTime = TimeSpan.FromMilliseconds(1500);
waveOutDevice.Init(mainOutputStream);
waveOutDevice.Play();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
CloseWaveOut();
}
}
}Решение задачи: «Библиотека NAudio»
textual
Листинг программы
if (mainOutputStream.CurrentTime > begin + TimeSpan.FromSeconds(10)) // например 10 секунд. waveOutDevice.Stop();