public void PlayMainSong(string identifier) { AudioContainer soundToPlay = sounds.Find(sound => sound.Identifier == identifier); // If the sound was properly loaded if (soundToPlay != null) { soundToPlay.Play(); Bass.BASS_ChannelStop(MainStream.Handle); MainStream = soundToPlay; Bass.BASS_ChannelSetAttribute(soundToPlay.Handle, BASSAttribute.BASS_ATTRIB_VOL, MainAudioVolume); } else { if (throwExceptions) { // The sound doesn't exist, throw an exception throw new NullReferenceException("Sound " + identifier + " does not exist"); } } }
/// <summary> /// Creates an AudioContainer given a filename and an identifier /// </summary> /// <param name="fullFileName">File path including extension</param> /// <param name="identifier">String used to identify the sound later on</param> public AudioContainer LoadAndReturnSound(string fullFileName, string identifier) { // TODO: METHOD WILL BE REWRITTEN AS OF V1.2 int newSoundHandle = 0; // Stream that the sound will be loaded in // Attempt to load sound into memory if (started) { // Create a handle from the given file with default flags newSoundHandle = Bass.BASS_StreamCreateFile(fullFileName, 0, 0, BASSFlag.BASS_DEFAULT); // If the file loaded successfully, it not newSoundHandle will be 0 if (newSoundHandle != 0) { AudioContainer newSound = new AudioContainer(newSoundHandle, identifier); sounds.Add(newSound); return newSound; } newSoundHandle = BassFlac.BASS_FLAC_StreamCreateFile(fullFileName, 0, 0, BASSFlag.BASS_DEFAULT); if (newSoundHandle != 0) { AudioContainer newSound = new AudioContainer(newSoundHandle, identifier); sounds.Add(newSound); return newSound; } // Alert the user that the file was not located MessageBox.Show("File: " + fullFileName + " could not be loaded, file is missing", "File could not be located", MessageBoxButtons.OK, MessageBoxIcon.Error); if (throwExceptions) { // The file could not be found, throw an exception throw new FileNotFoundException("File Could Not Be Loaded: " + fullFileName + "\nError Code: " + Bass.BASS_ErrorGetCode()); } return null; } // Alert the user that Bass did not start, therefore is unable to load any sounds MessageBox.Show("File: " + fullFileName + " could not be loaded, Bass did not initialize", "Bass failed to initialize", MessageBoxButtons.OK, MessageBoxIcon.Error); if (throwExceptions) { // Bass did not start, throw an exception throw new NullReferenceException("Attempted to load sound but Bass failed to initialize"); } return null; }