sound.cs

来自「一个用java设计的推箱子的程序。很不错的。呵呵」· CS 代码 · 共 114 行

CS
114
字号
using System;
using System.Collections.Generic;
using System.Text;
using nBASS;

namespace PushBox
{
    class Sound : IDisposable
    {
        private static nBASS.BASS soundDevice = null;


        private AdvancedChannel soundBuffer = null;





        public Sound(System.Windows.Forms.Form windowHandle)
        {

            if (Sound.soundDevice == null)
            {

                soundDevice = new nBASS.BASS();
                Sound.soundDevice.BeginInit();
                soundDevice.Device = "Default";
                soundDevice.Frequency = 44100;
                soundDevice.MusicVolume = 700;
                soundDevice.ParentForm = windowHandle;
                soundDevice.SampleVolume = 100;
                soundDevice.SetupFlags = nBASS.DeviceSetupFlags.Default;
                soundDevice.StreamVolume = 100;
                soundDevice.EndInit();
            }
        }


        ~Sound()
        {
            // Call the dispose method.
            Dispose();
        }

        public void Dispose()
        {

            try
            {
                soundBuffer.Dispose();
            }
            catch
            {
            }

            soundBuffer = null;


        }

        /// <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 void Load(string fileName)
        {
            // Holds the result for if the file was successful loaded or not.


            // 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.

            }
            catch
            {

            }

            // Returns the result.

        }

        //		public void AddVolume()
        //		{
        //			Sound.soundDevice .MusicVolume+=100;
        //			Sound.soundDevice.SampleVolume+=100;
        //			Sound.soundDevice.StreamVolume +=100;
        //		}

        /// <summary>
        /// Method that plays the sound in the current buffer.
        /// </summary>
        public void Play()
        {
            soundBuffer.Play(true, StreamPlayFlags.Default);
        }

        public void LoopPlay()
        {
            soundBuffer.Play(true, StreamPlayFlags.Loop);
        }

        public void Stop()
        {
            soundBuffer.Stop();
        }
    }
}

⌨️ 快捷键说明

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