How to add volume control

 

I had to add volume control to my application in order to comply with the marketplace review from the first time I sent it this was the solution I decided upon to have a scroller with 4 levels for audio and just modify the SoundEffect.MasterVolume to match the scroller. I’m going to post the code since it might help people implement a scroller or implement their own window. The code will be below.

Capture

Code

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using AvatareGameLibrary;
using Microsoft.Xna.Framework.Input.Touch;

namespace NoPlaceForGnomesLibrary
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class VolumeControl
    {
        GlobalResources resources;
        GraphicsDevice device;

        private int volumeLevel;

        public int VolumeLevel
        {
            get { return volumeLevel; }
            set {
                if (volumeLevel >= 0 && volumeLevel <= 3)
                    volumeLevel = value;
                else
                {
                    throw new ArgumentException(“Volume Out Of Range”);
                }
            }
        }
        private bool isShowing = false;

        public bool IsShowing
        {
            get { return isShowing; }
            set {
                if (!value)
                {
                    alpha = 0;
                }
                isShowing = value; }
        }

        public VolumeControl(GraphicsDeviceManager game, GlobalResources resources)           
        {
            device = game.GraphicsDevice;
            this.resources = resources;
            notchControl = (int)(SoundEffect.MasterVolume * 240);
        }

        int width = 340;
        int height = 400;
        float alpha = 0f;
        int notchControl = 0;

        public int NotchControl
        {
            get { return notchControl; }
            set
            {
                if (value < 0)
                    notchControl = 0;
                else if (value > 240)
                    notchControl = 240;
                else
                notchControl = value; }
        }
        string VolumeLevelText = “”;
        public void Draw(int left, int top, SpriteBatch batch)
        {
           
            batch.Draw(resources.ScoredBoardBack, new Rectangle(left, top, width, height), Color.White*alpha);

          
            //Draw the TExt of the current musci Status
            Vector2 wordSize = resources.ScoreBoardFont.MeasureString(VolumeLevelText);
            batch.DrawString(resources.ScoreBoardFont, VolumeLevelText, new Vector2(left + (width – wordSize.X) / 2, top + 40), Color.Gold * alpha);
            //Drawing line with notch to move
            batch.Draw(resources.ScoredBoardBack, new Rectangle(left + 40, top + 210, 260, 15), Color.White * alpha);
            //Draw the Notch
            notchRectangle =  new Rectangle(left + 55 + notchControl, top + 195, 15, 45);
            batch.Draw(resources.ScoredBoardBack, notchRectangle, Color.White * alpha);
            int center = (width – 160) / 2;
            okButton = new Rectangle(left + center, top + 270, 160, 80);
            batch.Draw(resources.Ok, okButton, Color.White*alpha);
            wordSize = resources.ScoreBoardFont.MeasureString(“OK”);
            float secondCenter = (160 – wordSize.X) / 2;
            batch.DrawString(resources.ScoreBoardFont, “OK”, new Vector2(left + center + secondCenter, top + 288), Color.Gold*alpha);
        }
        Rectangle okButton;
        Rectangle notchRectangle;
        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name=”gameTime”>Provides a snapshot of timing values.</param>
        public void Update()
        {
            if(isShowing)
            {
                if(alpha – 1 < 0)
                {
                    alpha+=.05f;
                }
            }
             TouchCollection collection = TouchPanel.GetState();
                if (collection.Count > 0)
                {
                    foreach (TouchLocation tl in collection)
                    {
                         if(TouchDetector.HasBeenTouched(tl,okButton))
                         {
                             IsShowing = false;
                             SoundEffect.MasterVolume = notchControl / 240f;
                         }
                         else if(TouchDetector.HasBeenTouched(Swipes.GetFirstLocation(tl) , notchRectangle ))
                         {
                             Swipes s = new Swipes(0);
                             NotchControl+=(int)(s.GetDirectionVector(tl).X);
                             notchMoved = true;
                         }
                    }

                }

          
            if(notchMoved)
            {
                if(notchControl >=0 &&  notchControl <60)
                {
                    VolumeLevelText = “OFF”;
                }
                else if(notchControl >= 60 && notchControl <60*2)
                {
                    VolumeLevelText = “Low”;
                }
                else if (notchControl >= 60*2 && notchControl < 60 * 3)
                {
                    VolumeLevelText = “Medium”;
                }
                else if (notchControl >= 60*3 && notchControl <= 240)
                {
                    VolumeLevelText = “Max”;
                }
                notchMoved = false;
            }
        }

        private bool notchMoved = true;
    }
}