Click here to Skip to main content
15,895,746 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.3K   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 delegate void MyDelegate(ref Message m);

    public partial class Form1 : Form
    {
        //// Declare the nofify constant
        public const int MM_MCINOTIFY = 953;
        Form2 f2;
        protected void mydelegate(ref Message m)
        {
            if (m.Msg == MM_MCINOTIFY)
            {
                // The file is done playing, do whatever
                System.Diagnostics.Debug.WriteLine(m.ToString());
            }
        }

        public void ProcessDelegate(MyDelegate d)
        {
            
        }



        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex < 0) return;
            string filename = (string)listBox1.SelectedItem;
            MciPlayer m = new MciPlayer(Application.StartupPath + @"\"+filename, filename);
            if (f2.Tag == null)
            {
                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);
                listBox1.Items.Add(s1);
              
            }
            

        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex < 0) return;
            string filename = (string)listBox1.SelectedItem;
            MciPlayer m = new MciPlayer(Application.StartupPath + @"\" + filename, filename);
            m.PlayFromStart();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex < 0) return;
            string filename = (string)listBox1.SelectedItem;
            if (filename.LastIndexOf(".wav") == filename.Length - 4)
            {
                MessageBox.Show("Repeat play for .wav file not supported");
                return;
            }
            MciPlayer m = new MciPlayer(Application.StartupPath + @"\" + filename, filename);
            m.PlayLoop();
        }

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

        private void button5_Click(object sender, EventArgs e)
        {
            foreach (var s in listBox1.Items)
            {
                string filename = (string)s;
                MciPlayer m = new MciPlayer();
                m.Alias = filename;
                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