gpt4 book ai didi

c# - NAudio-如何在结束后立即播放音频而没有任何延迟?

转载 作者:行者123 更新时间:2023-12-03 01:10:39 30 4
gpt4 key购买 nike

这是我的简单代码

var reader = new Mp3FileReader("Audio/Battle Wild Intro.mp3");
var reader2 = new Mp3FileReader("Audio/Battle Wild Loop.mp3");
var waveOut = new DirectSoundOut(); // or WaveOutEvent()
var waveOut2 = new DirectSoundOut();
waveOut.Init(reader);
waveOut2.Init(reader2);
waveOut.Play();
while (waveOut.PlaybackState != PlaybackState.Stopped) ;
waveOut2.Play();
基本上是线程吗?会播放音频直到结束,然后再播放另一音频...正在运行,但是下一播放之前会有0.1秒的延迟。
我想做的是播放一场战斗简介,然后在完成简介后开始音乐循环。基本上像《神奇宝贝》中的:3(例如: https://www.youtube.com/watch?v=aQrmCB4ZGGo)
我对线程有点陌生,非常感谢您的帮助! ^^
另外,如果我可以在Midi中使用自定义SF而不是mp3 / wav来做到这一点,那就太棒了:D

最佳答案

这可能不是最佳解决方案,但我要做的是将播放列表连接到一个mp3文件中,然后播放该mp3文件。当一首首首播放时,由于下一首歌曲正在加载,因此会暂停一毫秒。
首先,创建一个名为LoopStream.cs的新类文件名,并将此代码放在其中:

public class LoopStream : WaveStream
{
WaveStream sourceStream;

/// <summary>
/// Creates a new Loop stream
/// </summary>
/// <param name="sourceStream">The stream to read from. Note: the Read method of this stream should return 0 when it reaches the end
/// or else we will not loop to the start again.</param>
public LoopStream(WaveStream sourceStream)
{
this.sourceStream = sourceStream;
this.EnableLooping = true;
}

/// <summary>
/// Use this to turn looping on or off
/// </summary>
public bool EnableLooping { get; set; }

/// <summary>
/// Return source stream's wave format
/// </summary>
public override WaveFormat WaveFormat
{
get { return sourceStream.WaveFormat; }
}

/// <summary>
/// LoopStream simply returns
/// </summary>
public override long Length
{
get { return sourceStream.Length; }
}

/// <summary>
/// LoopStream simply passes on positioning to source stream
/// </summary>
public override long Position
{
get { return sourceStream.Position; }
set { sourceStream.Position = value; }
}

public override int Read(byte[] buffer, int offset, int count)
{
int totalBytesRead = 0;

while (totalBytesRead < count)
{
int bytesRead = sourceStream.Read(buffer, offset + totalBytesRead, count - totalBytesRead);
if (bytesRead == 0)
{
if (sourceStream.Position == 0 || !EnableLooping)
{
// something wrong with the source stream
break;
}
// loop
sourceStream.Position = 0;
}
totalBytesRead += bytesRead;
}
return totalBytesRead;
}
}
然后,这是您的主要 Program.cs,以循环播放mp3文件,我们将使用 LoopStream类循环播放mp3文件:
    static void Main(string[] args)
{
var playlist = new List<string>{ @"c:\Users\User\Music\sound1.mp3",
@"c:\Users\User\Music\sound2.mp3" };

var mp3OutputFile = File.Create(@"c:\Users\User\Music\soundnew3.mp3");

Concatenate(playlist, mp3OutputFile);
mp3OutputFile.Close();

var combinedList = new List<string> { @"c:\Users\User\Music\soundnew3.mp3" };

var playr = new playr(combinedList);
playr.PlaySong();

Console.WriteLine("Playing your songs..");
Console.ReadLine();
}

public static void Concatenate(List<string> mp3FileNames, Stream output)
{
foreach (string file in mp3FileNames)
{
Mp3FileReader reader = new Mp3FileReader(file);
if ((output.Position == 0) && (reader.Id3v2Tag != null))
{
output.Write(reader.Id3v2Tag.RawData,
0,
reader.Id3v2Tag.RawData.Length);
}
Mp3Frame frame;
while ((frame = reader.ReadNextFrame()) != null)
{
output.Write(frame.RawData, 0, frame.RawData.Length);
}
}
}

public class playr
{
private Queue<string> playlist;
private IWavePlayer player;
private WaveStream fileWaveStream;

public playr(List<string> startingPlaylist)
{
playlist = new Queue<string>(startingPlaylist);
}

public void PlaySong()
{
if (fileWaveStream != null)
{
fileWaveStream.Dispose();
}

if (playlist.Count < 1)
{
return;
}

if (player != null && player.PlaybackState != PlaybackState.Stopped)
{
player.Stop();
}

if (player != null)
{
player.Dispose();
player = null;
}


player = new WaveOutEvent();

fileWaveStream = new AudioFileReader(playlist.Dequeue());
LoopStream loop = new LoopStream(fileWaveStream);
player.Init(loop);
player.PlaybackStopped += (sender, evn) => { PlaySong(); };
player.Play();

}
}
如果要合并WAV文件,可以这样进行:
public static void Concatenate(List<string> sourceFiles)
{
byte[] buffer = new byte[1024];
WaveFileWriter waveFileWriter = null;
string outputFile = @"c:\Users\User\Music\soundnew3.wav";

try
{
foreach (string sourceFile in sourceFiles)
{
using (WaveFileReader reader = new WaveFileReader(sourceFile))
{
if (waveFileWriter == null)
{
// first time in create new Writer
waveFileWriter = new WaveFileWriter(outputFile, reader.WaveFormat);
}
else
{
if (!reader.WaveFormat.Equals(waveFileWriter.WaveFormat))
{
throw new InvalidOperationException("Can't concatenate WAV Files that don't share the same format");
}
}

int read;
while ((read = reader.Read(buffer, 0, buffer.Length)) > 0)
{
waveFileWriter.WriteData(buffer, 0, read);
}
}
}
}
finally
{
if (waveFileWriter != null)
{
waveFileWriter.Dispose();
}
}

}

关于c# - NAudio-如何在结束后立即播放音频而没有任何延迟?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63714376/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com