[英]SoundPlayer.Stop does not stop sound playback
我有一個用C#編寫的應用程序,它可以播放很少的.wav文件。 它使用System.Media名稱空間中的SoundPlayer
類來播放聲音,使用調用SoundPlayer.PlaySync
方法的線程來播放.wav文件。 這一切都包含在一個看起來像這樣的類中:
public class SoundController {
private object soundLocker = new object();
protected Thread SoundThread { get; set; }
protected string NextSound { get; set; }
protected AutoResetEvent PlayASoundPlag { get; set; }
protected Dictionary<string, SoundPlayer> Sounds { get; set; }
protected bool Stopping { get; set; }
public string SoundPlaying { get; private set; }
public SoundController() {
PendingCount = 0;
PlayASoundFlag = new AutoResetEvent( false );
Sounds = new Dictionary<string, SoundPlayer>();
soundLocker = new object();
Stopping = false;
SoundThread = new Thread( new ThreadStart( SoundPlayer ) ) { Name = "SoundThread", IsBackground = true };
SoundThread.Start();
}
private void SoundPlayer() {
do {
PlayASoundFlag.WaitOne();
bool soundWasPlayed = false;
while ( !Stopping && NextSound != null ) {
lock ( soundLocker ) {
SoundPlaying = NextSound;
NextSound = null;
}
Sounds[ SoundPlaying ].PlaySync();
lock ( soundLocker ) {
SoundPlaying = null;
soundWasPlayed = true;
}
}
} while ( !Stopping );
}
public bool HasSound( string key ) {
return Sounds.ContainsKey( key );
}
public void PlayAlarmSound( string key, bool stopCurrentSound ) {
if ( !Sounds.ContainsKey( key ) )
throw new ArgumentException( "Sound unknown", "key" );
lock ( soundLocker ) {
NextSound = key;
if ( SoundPlaying != null && stopCurrentSound )
Sounds[ SoundPlaying ].Stop();
PlayASoundFlag.Set();
}
}
}
當我的程序調用PlaySound
方法並且當前正在播放聲音時,將調用Stop
方法,但正在播放的聲音實際上並未停止。 我已經在Stop
的呼叫上放置了跟蹤點,並且我在它之后添加了一條線路,這樣我就可以看到呼叫何時以及何時返回,同時使用耳機收聽。 很明顯,聲音一直播放到最后。
如何讓聲音可靠地停止播放?
不幸的是,這是一個混亂的過程,但這有效:
class Program
{
static void Main(string[] args)
{
SoundPlayerEx player = new SoundPlayerEx(@"c:\temp\sorry_dave.wav");
player.SoundFinished += player_SoundFinished;
Console.WriteLine("Press any key to play the sound");
Console.ReadKey(true);
player.PlayAsync();
Console.WriteLine("Press a key to stop the sound.");
Console.ReadKey(true);
player.Stop();
Console.WriteLine("Press any key to continue");
}
static void player_SoundFinished(object sender, EventArgs e)
{
Console.WriteLine("The sound finished playing");
}
}
public static class SoundInfo
{
[DllImport("winmm.dll")]
private static extern uint mciSendString(
string command,
StringBuilder returnValue,
int returnLength,
IntPtr winHandle);
public static int GetSoundLength(string fileName)
{
StringBuilder lengthBuf = new StringBuilder(32);
mciSendString(string.Format("open \"{0}\" type waveaudio alias wave", fileName), null, 0, IntPtr.Zero);
mciSendString("status wave length", lengthBuf, lengthBuf.Capacity, IntPtr.Zero);
mciSendString("close wave", null, 0, IntPtr.Zero);
int length = 0;
int.TryParse(lengthBuf.ToString(), out length);
return length;
}
}
public class SoundPlayerEx : SoundPlayer
{
public bool Finished { get; private set; }
private Task _playTask;
private CancellationTokenSource _tokenSource = new CancellationTokenSource();
private CancellationToken _ct;
private string _fileName;
private bool _playingAsync = false;
public event EventHandler SoundFinished;
public SoundPlayerEx(string soundLocation)
: base(soundLocation)
{
_fileName = soundLocation;
_ct = _tokenSource.Token;
}
public void PlayAsync()
{
Finished = false;
_playingAsync = true;
Task.Run(() =>
{
try
{
double lenMs = SoundInfo.GetSoundLength(_fileName);
DateTime stopAt = DateTime.Now.AddMilliseconds(lenMs);
this.Play();
while (DateTime.Now < stopAt)
{
_ct.ThrowIfCancellationRequested();
//The delay helps reduce processor usage while "spinning"
Task.Delay(10).Wait();
}
}
catch (OperationCanceledException)
{
base.Stop();
}
finally
{
OnSoundFinished();
}
}, _ct);
}
public new void Stop()
{
if (_playingAsync)
_tokenSource.Cancel();
else
base.Stop(); //To stop the SoundPlayer Wave file
}
protected virtual void OnSoundFinished()
{
Finished = true;
_playingAsync = false;
EventHandler handler = SoundFinished;
if (handler != null)
handler(this, EventArgs.Empty);
}
}
那么為什么它不能“正常”工作呢? 這是一個眾所周知的問題。 SoundPlayer
是一個“即SoundPlayer
即忘”的代碼段,如果你沒有在你啟動它的同一個線程上取消它,它就不會做任何事情。 很多人抱怨它,因為我相信你已經看到使用原始wave_out
調用或移動到DirectX(或使用WPF,使用MediaPlayer
控件)的解決方案很少。
此SoundPlayerEx
類有幾個屬性可以讓您知道聲音何時結束或取消播放您異步啟動的聲音。 無需創建新線程即可使用,使其更易於使用。
隨意擴展代碼,這是一個快速和骯臟的解決方案來解決您的問題。 您需要的兩個類是SoundInfo類和SoundPlayerEx類,上面的其余代碼是一個演示(用您自己的一個替換wav文件)。
請注意,這不是一個通用的解決方案,因為它依賴於winmm.dll,所以這不會移植到Mono(不確定Mono是否有SoundPlayer
類)。 此外,由於它是一個臟的解決方案,如果您不使用PlayAsync
調用,您將無法獲得Finished
事件或屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.