Click here to Skip to main content
15,881,733 members
Articles / Programming Languages / C#

Audio Mixer - Console Simulator

Rate me:
Please Sign up or sign in to vote.
3.39/5 (43 votes)
22 Mar 2010CPOL4 min read 128.7K   4.2K   84  
C# .NET - MCI audio mixer with multiple tracks and buses
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using AudioVideoPlayer;
using System.IO;

namespace PF_Consola
{
    public partial class Player : UserControl
    {
        MCIPlayback mci;

        public Player()
        {
            InitializeComponent();
            mci = new MCIPlayback();
        }

        int _PlayState = 0;
        private int PlayState
        {
            set{
                _PlayState = value;

                //0 = Closed
                //1 = Stopped
                //2 = Playing
                //3 = Paused

                switch (_PlayState)
                {
                    case 0:
                        picPlayPause.Enabled = false;
                        picStop.Enabled = false;
                        picPlayPause.Image = picPlayPauseOFF.Image;
                        picStop.Image = picStopOFF.Image;

                        try
                        {
                            mci.Stop();
                            mci.Close();
                            lblPlayState.Text = "Closed";
                        }
                        catch (MCIPlaybackException ex)
                        {
                            MessageBox.Show(ex.Message, "MCI Error");
                        }

                        break;

                    case 1:
                        picPlayPause.Enabled = true;
                        picStop.Enabled = false;
                        picPlayPause.Image = picPlayPauseOFF.Image;
                        picStop.Image = picStopON.Image;

                        try
                        {
                            mci.Stop();
                            lblPlayState.Text = "Stopped";
                        }
                        catch (MCIPlaybackException ex)
                        {
                            MessageBox.Show(ex.Message, "MCI Error");
                        }

                        break;

                    case 2:
                        picPlayPause.Enabled = true;
                        picStop.Enabled = true;
                        picPlayPause.Image = picPlayPauseON.Image;
                        picStop.Image = picStopOFF.Image;

                        try
                        {
                            mci.Play();
                            lblPlayState.Text = "Playing";
                        }
                        catch (MCIPlaybackException ex)
                        {
                            MessageBox.Show(ex.Message, "MCI Error");
                        }

                        break;

                    case 3:
                        picPlayPause.Enabled = true;
                        picStop.Enabled = true;
                        picPlayPause.Image = picPlayPauseOFF.Image;
                        picStop.Image = picStopOFF.Image;

                        try
                        {
                            mci.Pause();
                            lblPlayState.Text = "Paused";
                        }
                        catch (MCIPlaybackException ex)
                        {
                            MessageBox.Show(ex.Message, "MCI Error");
                        }

                        break;
                }
            }
            get{
                return _PlayState;
            }
        }

        private void butLoad_Click(object sender, EventArgs e)
        {
            openFileDialog.Filter = "Archivos de audio soportados (*.mp3)|*.*";

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                lblPath.Text = openFileDialog.FileName;
                lblFileName.Text = openFileDialog.FileName.Substring(openFileDialog.FileName.LastIndexOf(@"\")+1);
                PlayState = 0;

                try
                {
                    mci.Open(lblPath.Text, "mpegvideo");
                }
                catch (MCIPlaybackException ex)
                {
                    MessageBox.Show(ex.Message, "MCI Error");
                    mci.Close();
                }

                if (mci.IsOpen)
                {
                    mci.SetVolume(0, 0);
                    mci.SetTimeFormat("ms");
                    lLength.Text = mci.GetLength();
                    PlayState = 1;
                }
            }
        }


        public int CurrentPosition()
        {
            int _posicion = 0;
            if (mci.IsOpen)
            {
                _posicion = Convert.ToInt32(mci.GetCurrentPosition());
            }

            return _posicion;
        }


        public void Restart()
        {
            PlayState = 0;
            lblPath.Text = "Path";
            lblFileName.Text = "FileName";
            lLength.Text = "0";
        }


        public void PlayExternal()
        {
            if (mci.IsOpen) {
                if (picPlayPause.Enabled == true)
                {
                    PlayState = 2;
                }
            }
        }

        public void StopExternal()
        {
            if (mci.IsOpen)
            {
                if (picStop.Enabled == true)
                {
                    PlayState = 1;
                }
            }
        }


        private void picPlayPause_Click(object sender, EventArgs e)
        {
            if (lblPlayState.Text == "Playing")
            {
                // PAUSE
                PlayState = 3;
            }
            else
            {
                // PLAY
                PlayState = 2;
            }
        }

        private void picStop_Click(object sender, EventArgs e)
        {
            // STOP
            PlayState = 1;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Argentina Argentina
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions