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

📄 sound.cs

📁 这个程序是基于c#平台的俄罗斯方框游戏。拿来共享。要求加分哦!
💻 CS
字号:


using System;
using nBASS;

namespace AnotherBlock
{
	/// <summary>
	/// Represents a sound.
	/// </summary>
	public class Sound: IDisposable
	{
		/// <summary>
		/// Private static attribute that holds the sound device.
		/// </summary>
		private static nBASS.BASS soundDevice = null;
		/// <summary>
		/// Private attribute that holds if the sound will loop or not.
		/// </summary>
		private bool loop = false;
		/// <summary>
		/// Private attribute that holds the sound buffer.
		/// </summary>
		private AdvancedChannel soundBuffer = null;

		/// <summary>
		/// Class constructor that creates a sound.
		/// </summary>
		/// <param name="windowHandle">The control that will interact with the sound device.</param>
		public Sound(System.Windows.Forms.Form windowHandle)
		{
			// Checks if there isn't a current sound device.
			if (Sound.soundDevice == null)
			{
				// There isn't a current sound device.
				// Creates a new sound device.
				Sound.soundDevice = new nBASS.BASS();
				// Starts the digital output port, configuring volume and everything.
				Sound.soundDevice.BeginInit();
				Sound.soundDevice.Device = "Default";
				Sound.soundDevice.Frequency = 44100;
				Sound.soundDevice.MusicVolume = 100;
				Sound.soundDevice.ParentForm = windowHandle;
				Sound.soundDevice.SampleVolume = 100;
				Sound.soundDevice.SetupFlags = nBASS.DeviceSetupFlags.Default;
				Sound.soundDevice.StreamVolume = 100;
				Sound.soundDevice.EndInit();
			}
		}

		/// <summary>
		/// Class destructor that destroys the sound.
		/// </summary>
		~Sound()
		{
			// Call the dispose method.
			Dispose();
		}

		/// <summary>
		/// Method that disposes the class resources.
		/// </summary>
		public void Dispose()
		{
			// Disposes the sound buffer.
			try
			{
				soundBuffer.Dispose();
			}
			catch
			{
			}

			soundBuffer = null;
		}

		/// <summary>
		/// Property that holds and sets if the sound will loop or not.
		/// </summary>
		public bool Loop
		{
			get
			{
				// Returns the value in the loop attribute.
				return loop;
			}

			set
			{
				// Sets the loop attribute to the value.
				loop = value;
			}
		}

		/// <summary>
		/// Method that loads a sound file to the buffer.
		/// </summary>
		/// <param name="fileName">Name of the file to be loaded.</param>
		/// <returns>True if the file was successful loaded, or false if it wasn't.</returns>
		public bool Load(string fileName)
		{
			// Holds the result for if the file was successful loaded or not.
			bool result = false;

			// Tries to open the sound file.
			try
			{
				// Opens the sound file.
				soundBuffer = Sound.soundDevice.LoadStream(fileName, 0, 0, 0);
				// If there was no error, sets the result as true.
				result = true;
			}
			catch
			{
			}

			// Returns the result.
			return result;
		}

		/// <summary>
		/// Method that plays the sound in the current buffer.
		/// </summary>
		public void Play()
		{
			// Checks if the loop attribute is set to true.
			if (loop)
			{
				// Loop attribute is set to true.
				// Plays the sound looping.
				soundBuffer.Play(false, StreamPlayFlags.Loop);
			}
			else
			{
				// Loop attribute is not set to true.
				// Plays the sound without looping.
				soundBuffer.Play(true, StreamPlayFlags.Default);
			}
		}

		/// <summary>
		/// Method that stops playing the sound in the current buffer.
		/// </summary>
		public void Stop()
		{
			// Checks if the current sound is playing.
			if (soundBuffer.ActivityState == State.Playing)
			{
				// Current sound is playing.
				// Stops the current playing sound.
				soundBuffer.Stop();
			}
		}
	}
}

⌨️ 快捷键说明

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