Beispiel #1
0
        /// <summary>
        /// Set the volume level for each channel of the audio stream.
        /// </summary>
        /// <param name="levels">An array of volume levels (between 0.0 and 1.0) one for each channel.</param>
        /// <remarks>
        /// A volume level MUST be supplied for reach channel in the audio stream.
        /// </remarks>
        /// <exception cref="ArgumentOutOfRangeException">
        /// Thrown when <paramref name="levels"/> does not contain <see cref="ChannelCount"/> elements.
        /// </exception>
        public void SetAllVolumes(float[] levels)
        {
            // Make friendly Net exceptions for common problems:
            int channelCount = ChannelCount;

            if (levels == null)
            {
                throw new ArgumentNullException(nameof(levels));
            }
            if (levels.Length != channelCount)
            {
                throw new ArgumentOutOfRangeException(
                          nameof(levels),
                          string.Format("SetAllVolumes MUST be supplied with a volume level for ALL channels. The AudioStream has {0} channels and you supplied {1} channels.",
                                        channelCount, levels.Length));
            }
            for (int i = 0; i < levels.Length; i++)
            {
                float level = levels[i];
                if (level < 0.0f)
                {
                    throw new ArgumentOutOfRangeException(nameof(levels), "All volumes must be between 0.0 and 1.0. Invalid volume at index: " + i.ToString());
                }
                if (level > 1.0f)
                {
                    throw new ArgumentOutOfRangeException(nameof(levels), "All volumes must be between 0.0 and 1.0. Invalid volume at index: " + i.ToString());
                }
            }
            Marshal.ThrowExceptionForHR(audioStreamVolumeInterface.SetAllVoumes(unchecked ((uint)channelCount), levels));
        }