gpt4 book ai didi

c# - SoundPlayer.Play的实现是什么,它不允许播放多个声音异步声音?

转载 作者:行者123 更新时间:2023-12-03 01:51:15 26 4
gpt4 key购买 nike

我正在尝试了解SoundPlayer.Play的实现中不允许播放多于1个声音异步声音的含义(例如,如herehere所述)。我调查了它的source code,但我不知道。

为了方便起见,这是SoundPlayer.PlaySoundPlayer.LoadAndPlay的代码,这是SoundPlayer.Play调用的方法:

public void Play() {
LoadAndPlay(NativeMethods.SND_ASYNC);
}

private void LoadAndPlay(int flags) {
//
if (String.IsNullOrEmpty(soundLocation) && stream == null) {
SystemSounds.Beep.Play();
return;
}

if (uri != null && uri.IsFile) {
// VSW 580992: With more than one thread, someone could call SoundPlayer::set_Location
// between the time LoadAndPlay demands FileIO and the time it calls PlaySound under elevation.
//
// Another scenario is someone calling SoundPlayer::set_Location between the time
// LoadAndPlay validates the sound file and the time it calls PlaySound.
// The SoundPlayer will end up playing an un-validated sound file.
// The solution is to store the uri.LocalPath on a local variable
string localPath = uri.LocalPath;

// request permission to read the file:
// pass the full path to the FileIOPermission
FileIOPermission perm = new FileIOPermission(FileIOPermissionAccess.Read, localPath);
perm.Demand();

// play the path
isLoadCompleted = true;
System.Media.SoundPlayer.IntSecurity.SafeSubWindows.Demand();

System.ComponentModel.IntSecurity.UnmanagedCode.Assert();
// ValidateSoundFile calls into the MMIO API so we need UnmanagedCode permissions to do that.
// And of course we need UnmanagedCode permissions to all Win32::PlaySound method.
try {
// don't use uri.AbsolutePath because that gives problems when there are whitespaces in file names
ValidateSoundFile(localPath);
UnsafeNativeMethods.PlaySound(localPath, IntPtr.Zero, NativeMethods.SND_NODEFAULT | flags);
} finally {
System.Security.CodeAccessPermission.RevertAssert();
}
} else {
LoadSync();
ValidateSoundData(streamData);
System.Media.SoundPlayer.IntSecurity.SafeSubWindows.Demand();

System.ComponentModel.IntSecurity.UnmanagedCode.Assert();
try {
UnsafeNativeMethods.PlaySound(streamData, IntPtr.Zero, NativeMethods.SND_MEMORY | NativeMethods.SND_NODEFAULT | flags);
} finally {
System.Security.CodeAccessPermission.RevertAssert();
}
}
}

最佳答案

UnsafeNativeMethods.PlaySound(winmm.dll)仅允许在每个过程的特定时间播放单个声音。

关于c# - SoundPlayer.Play的实现是什么,它不允许播放多个声音异步声音?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39487815/

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