c# - 我无法让声音在我的小行星游戏中正常工作?
问题描述
所以我一直在 C# Visual Studio 2019 Windows Forms 应用程序中对 1979 年 Atari 游戏 Asteroids 的翻拍进行编码。我遇到的问题是声音无法正常工作:
这是我实现声音的地方,当流星撞击船时:
private void DetectShipToMeteorCollisions()
{
if (!_ship.IsActive)
return;
for (int meteorIndex = 0; meteorIndex < _meteors.Count; meteorIndex++)
{
if (_meteors[meteorIndex].CollidesWithShip(_ship))
{
_meteors[meteorIndex].IsActive = false;
GameManager.RaiseMeteorHit(_meteors[meteorIndex]);
BlowUpShip();
}
SoundPlayer1 sound = new SoundPlayer1("bang_6.wav");
sound.Play();
}
}
// “bang_6.wav”被导入到 Visual Studio 项目中。
这是 SoundPlayer1 的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;
using System.Reflection;
namespace AsteroidsGdiApp.Core
{
public class SoundPlayer1
{
private byte[] m_soundBytes;
private string m_fileName;
private enum Flags
{
SND_SYNC = 0x0000, /* play synchronously (default) */
SND_ASYNC = 0x0001, /* play asynchronously */
SND_NODEFAULT = 0x0002, /* silence (!default) if sound not found */
SND_MEMORY = 0x0004, /* pszSound points to a memory file */
SND_LOOP = 0x0008, /* loop the sound until next sndPlaySound */
SND_NOSTOP = 0x0010, /* don't stop any currently playing sound */
SND_NOWAIT = 0x00002000, /* don't wait if the driver is busy */
SND_ALIAS = 0x00010000, /* name is a registry alias */
SND_ALIAS_ID = 0x00110000, /* alias is a predefined ID */
SND_FILENAME = 0x00020000, /* name is file name */
SND_RESOURCE = 0x00040004 /* name is resource name or atom */
}
[DllImport("CoreDll.DLL", EntryPoint = "PlaySound", SetLastError = true)]
private extern static int WCE_PlaySound(string szSound, IntPtr hMod, int flags);
[DllImport("CoreDll.DLL", EntryPoint = "PlaySound", SetLastError = true)]
private extern static int WCE_PlaySoundBytes(byte[] szSound, IntPtr hMod, int flags);
/// <summary>
/// Construct the Sound object to play sound data from the specified file.
/// </summary>
public SoundPlayer1(string fileName)
{
m_fileName = fileName;
}
/// <summary>
/// Construct the Sound object to play sound data from the specified stream.
/// </summary>
public SoundPlayer1(Stream stream)
{
// read the data from the stream
m_soundBytes = new byte[stream.Length];
stream.Read(m_soundBytes, 0, (int)stream.Length);
}
/// <summary>
/// Play the sound
/// </summary>
public void Play()
{
// if a file name has been registered, call WCE_PlaySound,
// otherwise call WCE_PlaySoundBytes
if (m_fileName != null)
WCE_PlaySound(m_fileName, IntPtr.Zero, (int)(Flags.SND_ASYNC | Flags.SND_FILENAME));
else
WCE_PlaySoundBytes(m_soundBytes, IntPtr.Zero, (int)(Flags.SND_ASYNC | Flags.SND_MEMORY));
}
public static void PlayEmbeddedSound(Assembly assembly, string resourceName)
{
SoundPlayer1 sound = new SoundPlayer1(assembly.GetManifestResourceStream(resourceName));
sound.Play();
}
public static void PlayFromFile(string filePath)
{
SoundPlayer1 sound = new SoundPlayer1(filePath);
sound.Play();
}
}
}
插入声音后尝试编译会产生以下错误:
我想知道为什么会这样?非常感谢。
因此,在修复错误后,我试图为子弹发射时添加声音效果。但是由于某种原因,当它以相同的方式导入项目时,它没有检测到这个 wav 文件。
我想知道为什么它没有找到这个 wav 文件。
这是,导入到项目中。谢谢。
private void DetectBulletCollisions()
{
for(int meteorIndex = 0; meteorIndex < _meteors.Count; meteorIndex++)
{
for (int bulletIndex = 0; bulletIndex < _photons.Length; bulletIndex++)
{
//TODO get rid of event drive desing here
_meteors[meteorIndex].TestIfShot(_photons[bulletIndex]);
System.IO.Stream str = Properties.Resources.mySoundFile;
System.Media.SoundPlayer snd = new System.Media.SoundPlayer("gunshot.wav");
snd.Play();
}
}
}
这里文件在同一目录中。因此,Visual Studio 没有看到它的事实很奇怪。它也在visual studio项目中导入,谢谢。
解决方案
推荐阅读
- embedded - 擦除 TIVA TM4C123 微控制器上的闪存
- crate - CrateDB 创建自定义分析器
- javascript - 获取用户注册日期
- react-native - 有没有一种可能的方法可以在 react-native-router-flux 中使用 react-native-router-flux 制作子标题
- unity-container - 像使用 Unity IoC 一样映射
- angularjs - 尝试检索 JSON 时出现角度错误
- python - 带有 mpi4py 的 Openmpi 不能在多个节点上工作
- javascript - d3.js 使用 Class 和 Attr 为文本添加颜色
- javascript - 将 html 表单发送到 Django 项目中的电子邮件地址的最佳方式?
- image - 尝试在空对象引用上调用接口方法 java.lang.String com.facebook.react.bridge.ReadableMap 等