playing mp3 files on user input

M

Maxi

I have few .mp3 files in C:\MP3 folder
a.mp3
b.mp3
c.mp3
..
..
..
z.mp3

These files are of duration 10 seconds

In an excel file, I want a text box that should allow two characters.

The moment I open the excel file, it should take the cursor in that
textbox automatically.

If I type "A" in the textbox, it should play a.mp3 file without opening
the mp3 player. (I am using musicmatch/windows media player mp3 player)

If I type "B" as the second character in the textbox, it should erase
"A" which was entered earlier, show the character "B" in the text box
and play b.mp3 file.

If b.mp3 is playing and before the song is over (before 10 seconds) if
I enter C as the second character in the textbox, it should stop b.mp3
song and start plaing c.mp3

If I type any other character or if I press any other key besides A-Z,
it should stop the current song and should not play anything, it should
just erase value in the text box and place the cursor there and wait
for an A-Z input.
 
M

moon

Studying a language and excersising alphabet pronunciation..?
I don't think VBA can do this without using a self-made or downloaded
ActiveX control.
 
M

moon

Answered to soon...
Playing an MP3 without showing Mediaplayer seems not too hard.

Private Declare Function ShellExecute Lib "shell32.dll" Alias _
"ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal _
lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String,
_
ByVal nShowCmd As Long) As Long

Private Sub wsBtnMP3_Click()

ShellExecute vbNull, "Open", "C:\a.mp3", vbNullString, "C:\", 0

End Sub
 
M

Michel Pierron

Hi Maxi,

In ThisWorkbook module:
Private Sub Workbook_Open()
ActiveSheet.OLEObjects("TextBox1").Object.MaxLength = 2
ActiveSheet.OLEObjects("TextBox1").Object.Value = ""
ActiveSheet.OLEObjects("TextBox1").Activate
End Sub

In Sheet module:
Private Declare Function mciSendString Lib "winmm.dll" Alias _
"mciSendStringA" (ByVal lpstrCommand As String _
, ByVal lpstrReturnString As String, ByVal uReturnLength As Long _
, ByVal hwndCallback As Long) As Long
Private Declare Function GetShortPathName Lib "kernel32" _
Alias "GetShortPathNameA" (ByVal lpszLongPath As String _
, ByVal lpszShortPath As String, ByVal lBuffer As Long) As Long
Private Playing As Boolean, Current As String

Private Sub PlaySong(Play As Boolean, FullPathSong$, Optional Reprise& = 0)
On Error Resume Next
Call mciSendString("Stop MPFE", 0&, 0, 0)
Call mciSendString("Close MPFE", 0&, 0, 0)
If Not Play Then Exit Sub
Dim Song As String
Song = FullPathSong
If Dir(Song) = "" Then Exit Sub
Song = GetShortPath(Song)
Call mciSendString("Open " & Song & " Alias MPFE", 0&, 0, 0)
Call mciSendString("play MPFE from " & Reprise, 0&, 0, 0)
End Sub

Private Function GetShortPath(strFileName As String) As String
Dim lngRes As Long, strPath As String
strPath = String(165, 0)
lngRes = GetShortPathName(strFileName, strPath, 164)
GetShortPath = Left(strPath, lngRes)
End Function

Private Sub TextBox1_Change()
If TextBox1.Text = "" Then GoTo 1
If Len(TextBox1.Text) > 1 Then TextBox1.Text = Right$(TextBox1.Text, 1)
Const DefaultPath$ = "C:\MP3\"
If Dir(DefaultPath & TextBox1.Text & ".mp3") = "" Then
1: If Playing Then Playing = False Else Exit Sub
Else
Playing = True
Current = DefaultPath & TextBox1.Text & ".mp3"
End If
Call PlaySong(Playing, Current)
End Sub

MP
 
R

raghu

Hat's to you.....you are great....
I was looking for such an excel to play & manage songs....
I appreciate your work.....
Thanks & Regards,
Raghu
 
M

Maxi

Thanks Michael, it is awesome. However, there is a small problem.

The entire code works fine but when the song is playing, it gives me a
lot of hissing sound. When I play the file in Windows Media Player or
Musicmatch seperately it plays fine. It gives hissing sound only if I
play through this file.

Here is a link to download sample a.mp3, b.mp3 and c.mp3 file.
http://www40.brinkster.com/maxlott/mp3.htm

Any ideas??
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top