⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 soundbuffer.cs

📁 Beginning C# Game Programming 的源代码
💻 CS
字号:
using System;
using System.Configuration;
using Microsoft.DirectX;
using Microsoft.DirectX.DirectSound;
using Buffer = Microsoft.DirectX.DirectSound.SecondaryBuffer;
using Device = Microsoft.DirectX.DirectSound.Device;

namespace SpaceDonuts {
	/// <summary>
	/// A single sound buffer for use by the SoundHandler class.
	/// </summary>
	public class SoundBuffer {
		Buffer buffer;
		Sounds thisSound;
		bool looping;
		bool lastValue;

		public SoundBuffer(Device soundDevice, string filename, Sounds thisSound, bool looping) {
			this.thisSound = thisSound;
			this.looping = looping;

			try {
				buffer = new SecondaryBuffer(filename, soundDevice);
			}
			catch (Exception e) {
				throw new Exception(String.Format("Error opening {0}", filename), e);
			}
		}

		public Sounds Sound {
			get {
				return thisSound;
			}
		}

		public int Volume { 
			get { 
				return buffer.Volume;
			} 
			set { 
				buffer.Volume = value;
			} 
		}

		public void Play(bool on) {
			// looping sounds don't get restarted
			if (looping) {
				if (on) {
					if (!lastValue) {
						buffer.SetCurrentPosition(1000);
						buffer.Play(0,BufferPlayFlags.Looping);
					}
				}
				else {
					buffer.Stop();
				}
				lastValue = on;
			}
			else {
				if (on) {
					buffer.SetCurrentPosition(0);
					buffer.Play(0,BufferPlayFlags.Default);
				}
			}
		}

		public void Stop() {
			if(buffer != null)
				buffer.Stop();
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -