Click here to Skip to main content
15,885,767 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
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Imports System.Runtime.InteropServices

Namespace MCIDEMO

	Public Partial Class Form1
		Inherits Form
		Public Class ListItem
			Private _playlooping As Boolean = False
			Public Property Playlooping() As Boolean
				Get
					Return _playlooping
				End Get
				Set
					_playlooping = value
				End Set
			End Property

			Private _deviceid As Integer = 0
			Public Property DeviceId() As Integer
				Get
					Return _deviceid
				End Get
				Set
					_deviceid = value
				End Set
			End Property

			Private _filename As String = ""

			Public Property Filename() As String
				Get
					Return _filename
				End Get
				Set
					_filename = value
				End Set
			End Property
			Private _alias As String = ""

			Public Property [Alias]() As String
				Get
					Return _alias
				End Get
				Set
					_alias = value
				End Set
			End Property


			Public Sub New(filename As String, [alias] As String)
				_filename = filename
				_alias = [alias]
			End Sub




			Public Overrides Function ToString() As String
				Return _filename
			End Function
		End Class


		Public Shared nextnum As Integer = 0
		Public Const MM_MCINOTIFY As Integer = 953
		Private f2 As Form2


		Public Sub New()
			InitializeComponent()
		End Sub

		Private Function IsAnsiName(s As String) As Boolean
			Dim u As New UnicodeEncoding()
			Dim b As Byte() = u.GetBytes(s)
			For i As Integer = 1 To b.Length - 1 Step 2
				If b(i) <> 0 Then
					Return False
				End If
			Next

			Return True
		End Function


		Private Function CreateMCIPlayer(filename As String, ByRef [alias] As String) As MciPlayer
			Dim isansiname As Boolean = Me.IsAnsiName(filename)

			Dim m As MciPlayer = Nothing

			nextnum += 1

			If isansiname Then
				m = New MciPlayer(Application.StartupPath & "\" & filename, nextnum & "")

				[alias] = nextnum & ""
			Else

				Dim ext As String = filename.Substring(filename.Length - 4)
				Dim relocatedfile As String = Application.StartupPath & "\unicodenamesupport\" & nextnum & ext
				If System.IO.File.Exists(relocatedfile) Then
					System.IO.File.Delete(relocatedfile)
				End If
				System.IO.File.Copy(Application.StartupPath & "\" & filename, relocatedfile)
				[alias] = nextnum & ""
				m = New MciPlayer(relocatedfile, nextnum & "")
			End If
			Return m
		End Function


        Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click
            If listBox1.SelectedIndex < 0 Then
                Return
            End If

            Dim itm As ListItem = DirectCast(listBox1.SelectedItem, ListItem)
            Dim filename As String = itm.ToString()
            Dim m As MciPlayer = Nothing

            If itm.[Alias] <> "" Then
                m = New MciPlayer()
                m.[Alias] = itm.[Alias]
                m.Isloaded = True
            Else
                Dim [alias] As String = ""
                m = CreateMCIPlayer(filename, [alias])
                itm.[Alias] = [alias]
                itm.DeviceId = m.Deviceid
            End If
            itm.Playlooping = False
            m.PlayFromStart(f2.Handle)

        End Sub

        Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
            f2 = New Form2()
            f2.Left = Me.Left + Me.Width
            f2.Top = Me.Top
            f2.parent = Me
            f2.Show()

            Dim apppath As String = Application.StartupPath
            Dim files_mp3 = System.IO.Directory.GetFiles(apppath, "*.mp3")
            Dim files_wav = System.IO.Directory.GetFiles(apppath, "*.wav")
            Dim files = New String(files_mp3.Length + (files_wav.Length - 1)) {}
            files_mp3.CopyTo(files, 0)
            files_wav.CopyTo(files, files_mp3.Length)
            For Each s As Object In files
                Dim s1 = s.Substring(apppath.Length + 1)

                Dim itm As New ListItem(s1, "")
                listBox1.Items.Add(itm)
            Next

            If Not System.IO.Directory.Exists(apppath & "\unicodenamesupport") Then
                System.IO.Directory.CreateDirectory(apppath & "\unicodenamesupport")
            End If

        End Sub


        Private Sub button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button2.Click
            If listBox1.SelectedIndex < 0 Then
                Return
            End If

            Dim itm As ListItem = DirectCast(listBox1.SelectedItem, ListItem)
            Dim filename As String = (itm).ToString()
            Dim m As MciPlayer = Nothing
            If itm.[Alias] <> "" Then
                m = New MciPlayer()
                m.[Alias] = itm.[Alias]
                m.Isloaded = True
            Else
                Dim [alias] As String = ""
                m = CreateMCIPlayer(filename, [alias])
                itm.[Alias] = [alias]
                itm.DeviceId = m.Deviceid
            End If
            itm.Playlooping = False
            m.PlayFromStart()
        End Sub

        Private Sub button3_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button3.Click
            If listBox1.SelectedIndex < 0 Then
                Return
            End If
            Dim itm As ListItem = DirectCast(listBox1.SelectedItem, ListItem)
            Dim filename As String = (itm).ToString()
            'if (filename.LastIndexOf(".wav") == filename.Length - 4)
            '{
            '    MessageBox.Show("Repeat play for .wav file not supported");
            '    return;
            '}
            Dim m As MciPlayer = Nothing
            If itm.[Alias] <> "" Then
                m = New MciPlayer()
                m.[Alias] = itm.[Alias]
                m.Isloaded = True
            Else
                Dim [alias] As String = ""
                m = CreateMCIPlayer(filename, [alias])
                itm.DeviceId = m.Deviceid
                itm.[Alias] = [alias]
            End If
            itm.Playlooping = True
            If filename.Substring(filename.Length - 4).ToUpper() = ".WAV" Then
                '  if (filename.LastIndexOf(".wav") == filename.Length - 4)
                m.PlayFromStart(f2.Handle)
            Else
                m.PlayLoop()
            End If
        End Sub

        Private Sub button4_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button4.Click
            If listBox1.SelectedIndex < 0 Then
                Return
            End If
            Dim itm As ListItem = DirectCast(listBox1.SelectedItem, ListItem)
            Dim filename As String = itm.ToString()
            Dim m As New MciPlayer()
            m.[Alias] = itm.[Alias]
            If m.[Alias] <> "" Then
                m.StopPlaying()
            End If
        End Sub

        Private Sub button5_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button5.Click
            For Each itm As ListItem In listBox1.Items
                Dim filename As String = itm.ToString()
                Dim m As New MciPlayer()
                m.[Alias] = itm.[Alias]
                If m.[Alias] <> "" Then
                    m.StopPlaying()
                End If
            Next
        End Sub



	End Class


End Namespace

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