Click here to Skip to main content
15,891,828 members
Articles / Programming Languages / Visual Basic

Fun with Sound

Rate me:
Please Sign up or sign in to vote.
4.92/5 (25 votes)
10 Jul 2014CPOL5 min read 42.2K   2.2K   53  
Playing your favorite music and sound files all at once
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace MCIDEMO

{   

    public partial class Form1 : Form
    {
        internal class ListItem
        {
            string _filename = "";

            public string Filename
            {
                get { return _filename; }
                set { _filename = value; }
            }
            string _alias = "";

            public string Alias
            {
                get { return _alias; }
                set { _alias = value; }
            }

            public ListItem(string filename, string alias)
            {
                _filename = filename;
                _alias = alias;
            }

            public override string ToString()
            {
                return _filename;
            }
        }


        static int nextnum=0;
        public const int MM_MCINOTIFY = 953;
        Form2 f2;


        public Form1()
        {
            InitializeComponent();
        }

        private bool IsAnsiName(string s)
        {
            UnicodeEncoding u = new UnicodeEncoding();
            byte[] b=u.GetBytes(s);
            for (int i = 1; i < b.Length; i += 2)
            {
                if (b[i] != 0) return false;
            }

            return true;
        }

        private string getAliasFromFileName(string s)
        {
            if (s == "") return "";
            string s1 = s.Replace(' ', '_');
            return s1;
        }

        private MciPlayer CreateMCIPlayer(string filename, ref string alias)
        {
            bool isansiname = this.IsAnsiName(filename);

            MciPlayer m = null;
            if (isansiname)
            {
                alias = getAliasFromFileName(filename);
                m = new MciPlayer(Application.StartupPath + @"\" + filename, getAliasFromFileName(filename));

            }
            else
            {
                nextnum++;
                string ext = filename.Substring(filename.Length - 4);
                string relocatedfile = Application.StartupPath + "\\unicodenamesupport\\" + nextnum + ext;
                if (System.IO.File.Exists(relocatedfile))
                    System.IO.File.Delete(relocatedfile);
                System.IO.File.Copy(Application.StartupPath + @"\" + filename, relocatedfile);
                alias = nextnum + "";
                m = new MciPlayer(relocatedfile, nextnum + "");
            }
            return m;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex < 0) return;
           
            ListItem itm=(ListItem)listBox1.SelectedItem;
            string filename = itm.ToString();
            bool isansiname = this.IsAnsiName(filename);
      
            if (f2.Tag == null)
            {
                string alias = "";
                 MciPlayer m = CreateMCIPlayer(filename,ref alias);
                 itm.Alias = alias;
                f2.Tag = filename;
                m.PlayFromStart(f2.Handle);
            }
            else
            {
                MessageBox.Show("Notifer is busy. Please try again!");
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            f2 = new Form2();
            f2.Left = this.Left + this.Width;
            f2.Top = this.Top;
            f2.Show();

            string apppath = Application.StartupPath;
            var files_mp3=System.IO.Directory.GetFiles(apppath,"*.mp3");
            var files_wav = System.IO.Directory.GetFiles(apppath, "*.wav");
            var files = new string[files_mp3.Length  + files_wav.Length];
            files_mp3.CopyTo(files, 0);
            files_wav.CopyTo(files, files_mp3.Length);
            foreach( var s in files)
            {
                var s1 = s.Substring(apppath.Length + 1);

                ListItem itm= new ListItem(s1, "");
                listBox1.Items.Add(itm);
            }

            if(!System.IO.Directory.Exists(apppath +"\\unicodenamesupport"))
                System.IO.Directory.CreateDirectory(apppath +"\\unicodenamesupport");          

        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex < 0) return;

            ListItem itm = (ListItem)listBox1.SelectedItem;
            string filename = (itm).ToString();
            string alias = "";
            MciPlayer m = CreateMCIPlayer(filename, ref alias); 
            itm.Alias=alias;
            m.PlayFromStart();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex < 0) return;
            ListItem itm = (ListItem)listBox1.SelectedItem;
            string filename = (itm).ToString();
            if (filename.LastIndexOf(".wav") == filename.Length - 4)
            {
                MessageBox.Show("Repeat play for .wav file not supported");
                return;
            }
            string alias = "";
  
            MciPlayer m = CreateMCIPlayer(filename,ref alias); 
            itm.Alias = alias;
            m.PlayLoop();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex < 0) return;
            ListItem itm = (ListItem)listBox1.SelectedItem;
            string filename = itm.ToString();
            MciPlayer m = new MciPlayer();
            m.Alias = itm.Alias;//getAliasFromFileName(filename);
            if(m.Alias !="")
               m.StopPlaying();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            foreach (ListItem itm in listBox1.Items)
            {
                string filename = itm.ToString();
                MciPlayer m = new MciPlayer();
                m.Alias = itm.Alias;//getAliasFromFileName(filename);
                if(m.Alias !="")
                   m.StopPlaying();
            }
        }
    }
   

}

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 (Senior)
Singapore Singapore
Coder. Hacker. Fixer.

Comments and Discussions