When the program is in early warning, it will issue an alarm sound. When the alarm audio playback time exceeds the early warning frequency, it will send 10 printing tasks like we print documents like the printer. When the first sheet is printed, I want to cancel the printing. You can't operate it through software on the computer. Therefore, in order to avoid this, we have to wait until the printer finishes printing one before sending the next task. In this way, you can initiate the cancellation of the printing task at any time.
If the implementation does not give the task before the alarm audio is over
SoundPlayer
In C#, the SoundPlayer class itself does not directly provide events or attributes to detect whether the audio is played. However, you can do this by: The PlaySync method blocks the current thread until the audio playback is complete.You can place the play operation in a separate thread to avoid blocking the main thread.
Can't satisfy the function I want
using System;
using;
using;
class Program
{
static void Main()
{
string[] audioFiles = { "", "", "" };
SoundPlayer player = new SoundPlayer();
foreach (string file in audioFiles)
{
//TODO If the task is cancelled, break will break out of the loop
= file;
(); // Load audio file
(); // Block playback until the current audio playback is completed
($"Playing is completed: {file}");
}
("All audio playback is completed");
}
}
advantage:
- Simple implementation and intuitive code.
- No additional event processing is required.
shortcoming:
- PlaySync will block the current thread, which may cause the UI thread to get stuck (if running in the UI thread).
NAudio
C# Winform uses NAudio to obtain and control the computer operating system volume
NAudio library provides more powerful audio processing functions, allowing continuous playback through PlaybackStopped events
using System;
using;
using;
public class MyForm : Form
{
private Label myLabel;
public MyForm()
{
}
WaveOutEvent waveOut;
List<string> audioFiles = new List<string> { "", "", "" };
private void OnLineCustodyOrderFrm_Load(object sender, EventArgs e)
{
waveOut = new WaveOutEvent();
+= WaveOut_PlaybackStopped;
PlayNextAudio();
}
static void WaveOut_PlaybackStopped(object sender, StoppedEventArgs e)
{
//The playback is over, and resources must be released
if (waveOut != null && == )
{
();
}
($"Playing is completed: {audioFiles[currentIndex - 1]}");
PlayNextAudio();
}
static void PlayNextAudio()
{
if (currentIndex < )
{
var audioFile = new AudioFileReader(audioFiles[currentIndex]);
waveOut = new WaveOutEvent(); //The playback is completed and released, so you need to recreate WaveOutEvent here.
(audioFile);
();
($"Start playback: {audioFiles[currentIndex]}");
currentIndex++;
}
else
{
("All audio playback is completed");
}
}
private void MyForm_FormClosing(object sender, FormClosingEventArgs e)
{
//Form relationship, releasing resources
if (waveOut != null)
{
();
(); // Make sure the control is released
waveOut = null; // Prevent repeated releases and memory leaks
}
}
}
Key points
To explicitly release resources:
- After each audio playback is complete, call () to release the current WaveOutEvent object.
- Make sure to release all resources before the program exits.
Recreate WaveOutEvent:
- Each time a new audio file is played, a new WaveOutEvent object is recreated instead of reusing the previous object.
Release AudioFileReader:
- If you use AudioFileReader, you also need to make sure that you release it after playback is complete.