コード例 #1
0
ファイル: MusicPlayer.cs プロジェクト: MHLoppy/Rise_of_Music
        /// <summary>
        /// Returns a file from the current mood directory.
        /// This function will only repeat a file when every other file has been played at least once.
        /// </summary>
        /// <returns></returns>
        private String GetUnplayedFileForCurrentMood()
        {
            // Get the audio file list for the current music mood
            List<AudioFile> audioFiles = this.MoodToAudioFileDictionary[this.Mood];

            // If the list is empty, return null
            if (audioFiles.Count == 0)
                return null;

            // First, let's check to make sure that at least one audio file that hasn't been played
            bool atLeastOneHasNotBeenPlayed = false;
            foreach (AudioFile audioFile in audioFiles)
            {
                if (!audioFile.HasBeenPlayed)
                {
                    atLeastOneHasNotBeenPlayed = true;
                    break;
                }
            }

            // If all of the audio files have been played in this mood, reset the HasBeenPlayed property
            if (!atLeastOneHasNotBeenPlayed)
            {
                foreach(AudioFile audioFile in audioFiles)
                {
                    // Make sure to not change the current AudioFile, because we don't want to play that song twice in a row.
                    // It'll be reset on the next refresh like this.
                    if (audioFile != this.currentAudioFile)
                    {
                        audioFile.HasBeenPlayed = false;
                    }
                }
            }

            // Select a random index
            int index = this.r.Next(0, audioFiles.Count);

            // Loop until we find a file to play and return that file
            while (true)
            {
                // If the selected audio file hasn't been played
                if (!audioFiles[index].HasBeenPlayed)
                {
                    // Set it's HasBeenPlayed value
                    audioFiles[index].HasBeenPlayed = true;

                    // Set this audio file as the current audio file
                    this.currentAudioFile = audioFiles[index];

                    // Return this audio file to be played
                    return audioFiles[index].Path;
                }
                else // The file at this index has already been played; try the next index
                {
                    // Try the next index
                    ++index;

                    // If the index is equal to the total number of audio files in the list, set it to 0
                    if (index >= audioFiles.Count)
                    {
                        // Reset the index to zero
                        index = 0;
                    }
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Returns a file from the current mood directory.
        /// This function will only repeat a file when every other file has been played at least once.
        /// </summary>
        /// <returns></returns>
        private String GetUnplayedFileForCurrentMood()
        {
            // Get the audio file list for the current music mood
            List <AudioFile> audioFiles = this.MoodToAudioFileDictionary[this.Mood];

            // If the list is empty, return null
            if (audioFiles.Count == 0)
            {
                return(null);
            }

            // First, let's check to make sure that at least one audio file that hasn't been played
            bool atLeastOneHasNotBeenPlayed = false;

            foreach (AudioFile audioFile in audioFiles)
            {
                if (!audioFile.HasBeenPlayed)
                {
                    atLeastOneHasNotBeenPlayed = true;
                    break;
                }
            }

            // If all of the audio files have been played in this mood, reset the HasBeenPlayed property
            if (!atLeastOneHasNotBeenPlayed)
            {
                foreach (AudioFile audioFile in audioFiles)
                {
                    // Make sure to not change the current AudioFile, because we don't want to play that song twice in a row.
                    // It'll be reset on the next refresh like this.
                    if (audioFile != this.currentAudioFile)
                    {
                        audioFile.HasBeenPlayed = false;
                    }
                }
            }

            // Select a random index
            int index = this.r.Next(0, audioFiles.Count);

            // Loop until we find a file to play and return that file
            while (true)
            {
                // If the selected audio file hasn't been played
                if (!audioFiles[index].HasBeenPlayed)
                {
                    // Set it's HasBeenPlayed value
                    audioFiles[index].HasBeenPlayed = true;

                    // Set this audio file as the current audio file
                    this.currentAudioFile = audioFiles[index];

                    // Return this audio file to be played
                    return(audioFiles[index].Path);
                }
                else // The file at this index has already been played; try the next index
                {
                    // Try the next index
                    ++index;

                    // If the index is equal to the total number of audio files in the list, set it to 0
                    if (index >= audioFiles.Count)
                    {
                        // Reset the index to zero
                        index = 0;
                    }
                }
            }
        }
コード例 #3
0
ファイル: MusicPlayer.cs プロジェクト: MHLoppy/Rise_of_Music
        /// <summary>
        /// Inits the MoodToAudioFile and MoodToAudioFileDictionary with the files from each mood directory.
        /// </summary>
        /// <param name="mood"></param>
        private void InitAudioFilesForMood(String mood)
        {
            String appDataMusicMoodDir = @"C:\Users\" + Environment.UserName + @"\AppData\Roaming\microsoft games\rise of nations\Rise_of_Music\sounds\tracks\" + mood;

            // Create the list of AudioFile objects
            List<AudioFile> audioFiles = new List<AudioFile>();

            foreach (String filePath in Directory.GetFiles(appDataMusicMoodDir))
            {
                // Make sure we're only using WAV or MP3 files (ignoring case)
                if (filePath.EndsWith("wav", true, System.Globalization.CultureInfo.CurrentCulture) ||
                    filePath.EndsWith("mp3", true, System.Globalization.CultureInfo.CurrentCulture))
                {
                    // Create a new AudioFile
                    AudioFile audioFile = new AudioFile(filePath);

                    // Add this file path to the list of AudioFile objects
                    audioFiles.Add(audioFile);
                }
            }

            // Add the list of AudioFile objects in the dictionary for the given mood
            this.MoodToAudioFileDictionary.Add(mood, audioFiles);
        }