Audio File Randomizer

BuddyV

New Member
Joined
Jun 12, 2008
Messages
21
I have pieced together a macro that will play a song according to a schedule that I have laid out. The problem that I am running into is that people are getting sick of the same song and its a pain to redefine the file destination all of the time.

I need a way to randomize the songs being selected.

In the Private module ThisWorkbook the code is:

Private Sub Workbook_Open()

Application.OnTime TimeValue("6:50:00"), "MyMacro"
Application.OnTime TimeValue("7:50:00"), "MyMacro"
Application.OnTime TimeValue("8:50:00"), "MyMacro"
Application.OnTime TimeValue("9:50:00"), "MyMacro"
Application.OnTime TimeValue("10:50:00"), "MyMacro"
Application.OnTime TimeValue("11:50:00"), "MyMacro"
Application.OnTime TimeValue("13:20:00"), "MyMacro"
Application.OnTime TimeValue("14:20:00"), "MyMacro"
Application.OnTime TimeValue("15:20:00"), "MyMacro"
Application.OnTime TimeValue("16:20:00"), "MyMacro"
Application.OnTime TimeValue("17:20:00"), "MyMacro"
Application.OnTime TimeValue("18:20:00"), "MyMacro"
Application.OnTime TimeValue("19:20:00"), "MyMacro"
Application.OnTime TimeValue("20:50:00"), "MyMacro"
Application.OnTime TimeValue("21:50:00"), "MyMacro"
Application.OnTime TimeValue("22:50:00"), "MyMacro"


End Sub

In the Standard Module the code is:


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

Sub MyMacro()

Application.OnTime TimeValue("6:50:00"), "MyMacro"
Application.OnTime TimeValue("7:50:00"), "MyMacro"
Application.OnTime TimeValue("8:50:00"), "MyMacro"
Application.OnTime TimeValue("9:50:00"), "MyMacro"
Application.OnTime TimeValue("10:50:00"), "MyMacro"
Application.OnTime TimeValue("11:50:00"), "MyMacro"
Application.OnTime TimeValue("13:20:00"), "MyMacro"
Application.OnTime TimeValue("14:20:00"), "MyMacro"
Application.OnTime TimeValue("15:20:00"), "MyMacro"
Application.OnTime TimeValue("16:20:00"), "MyMacro"
Application.OnTime TimeValue("17:20:00"), "MyMacro"
Application.OnTime TimeValue("18:20:00"), "MyMacro"
Application.OnTime TimeValue("19:20:00"), "MyMacro"
Application.OnTime TimeValue("20:50:00"), "MyMacro"
Application.OnTime TimeValue("21:50:00"), "MyMacro"
Application.OnTime TimeValue("22:50:00"), "MyMacro"



Dim handle As Long
handle = ShellExecute(0, "", "C:\Users\kwinnike\Desktop\Kalimba.mp3", 0, 0, SW_SHOWNORMAL)

End Sub

The ShellExecute is selecting a final file destination, but I would like to select final folder destination and be able to randomly select the files in the folder.

Any suggestions?

Thanks in advance for your help, its much appreciated!
 

Some videos you may like

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.

Nogslaw

Well-known Member
Joined
Jul 9, 2004
Messages
904
Just my first thought:

You could have a random number generator that picks a number between 1 and 10 lets say. Then use a Select Case to determine which song to play.

Code:
Dim MyNumber As Integer
Dim Song As String
Randomize Timer
MyNumber = Application.WorksheetFunction.RoundUp(Rnd() * 10, 0)

Select Case True
    Case MyNumber = 1
        Song = 'ENTER YOUR 1ST FILE'S NAME HERE
        
    Case MyNumber = 2
        Song = 'ENTER YOUR 2ND FILE'S NAME HERE
        
    Case MyNumber = 3
        Song = 'ENTER YOUR 3RD FILE'S NAME HERE
        
    Case MyNumber = 4
        Song = 'ENTER YOUR 4TH FILE'S NAME HERE
        
'ETC, ETC ...

    Case Else
        Song = 'ENTER A DEFAULT SONG HERE
        
End Select

Handle = ShellExecute(0, "", "C:\Users\kwinnike\Desktop\" & Song, 0, 0, SW_SHOWNORMAL)
 

BuddyV

New Member
Joined
Jun 12, 2008
Messages
21
Thanks for the reply. The code runs without any errors but it won't play any file on the times I specify in the schedule.

Here is the code:

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

Sub MyMacro()

Application.OnTime TimeValue("6:50:00"), "MyMacro"
Application.OnTime TimeValue("7:50:00"), "MyMacro"
Application.OnTime TimeValue("8:50:00"), "MyMacro"
Application.OnTime TimeValue("9:50:00"), "MyMacro"
Application.OnTime TimeValue("10:50:00"), "MyMacro"
Application.OnTime TimeValue("11:50:00"), "MyMacro"
Application.OnTime TimeValue("13:20:00"), "MyMacro"
Application.OnTime TimeValue("14:52:00"), "MyMacro"
Application.OnTime TimeValue("15:20:00"), "MyMacro"
Application.OnTime TimeValue("16:20:00"), "MyMacro"
Application.OnTime TimeValue("17:20:00"), "MyMacro"
Application.OnTime TimeValue("18:20:00"), "MyMacro"
Application.OnTime TimeValue("19:20:00"), "MyMacro"
Application.OnTime TimeValue("20:50:00"), "MyMacro"
Application.OnTime TimeValue("21:50:00"), "MyMacro"
Application.OnTime TimeValue("22:50:00"), "MyMacro"


Dim MyNumber As Integer
Dim Song As String
Randomize Timer
MyNumber = Application.WorksheetFunction.RoundUp(Rnd() * 10, 0)

Select Case True
Case MyNumber = 1
Song = "C:\Users\kwinnike\Music\Dazed and Confused Soundtrack\Low Rider.mp3"

Case MyNumber = 2
Song = "C:\Users\kwinnike\Music\Dazed and Confused Soundtrack\Slow Ride.mp3"

Case MyNumber = 3
Song = "C:\Users\kwinnike\Music\Dazed and Confused Soundtrack\Love Hurts.mp3"

Case MyNumber = 4
Song = "C:\Users\kwinnike\Music\Dazed and Confused Soundtrack\Cherry Bomb.mp3"

Case MyNumber = 5
Song = "C:\Users\kwinnike\Music\Dazed and Confused Soundtrack\Tush.mp3"

Case MyNumber = 6
Song = "C:\Users\kwinnike\Music\Dazed and Confused Soundtrack\Fox On The Run.mp3"

Case MyNumber = 7
Song = "C:\Users\kwinnike\Music\Dazed and Confused Soundtrack\Tuesday's Gone.mp3"

Case MyNumber = 8
Song = "C:\Users\kwinnike\Music\Dazed and Confused Soundtrack\Stranglehold.mp3"

Case MyNumber = 9
Song = "C:\Users\kwinnike\Music\Dazed and Confused Soundtrack\School's Out.mp3"

Case MyNumber = 10
Song = "C:\Users\kwinnike\Music\Dazed and Confused Soundtrack\Highway Star.mp3"

Case Else
Song = "C:\Users\kwinnike\Music\Dazed and Confused Soundtrack\Paranoid.mp3"

End Select

Dim handle As Long
handle = ShellExecute(0, "", "C:\Users\kwinnike\Music\Dazed and Confused Soundtrack\" & Song, 0, 0, SW_SHOWNORMAL)

End Sub

******************
Thanks for the help!
 

Nogslaw

Well-known Member
Joined
Jul 9, 2004
Messages
904
I believe the problem lies here:

handle = ShellExecute(0, "", "C:\Users\kwinnike\Music\Dazed and Confused Soundtrack\" & Song, 0, 0, SW_SHOWNORMAL)

End Sub

******************
Thanks for the help!

You've already specified the file path, so you don't need to duplicate it in your command on this line. Your variable 'Song' has been assigned a value of "C:\Users\kwinnike\Music\Dazed and Confused Soundtrack\Slow Ride.mp3"

so your command above compiles to:
handle = ShellExecute(0, "", "C:\Users\kwinnike\Music\Dazed and Confused Soundtrack\C:\Users\kwinnike\Music\Dazed and Confused Soundtrack\Slow Ride.mp3, 0, 0, SW_SHOWNORMAL)

While it might not show an error, it is not able to find a file at that filepath location. Instead, shorten the code to this:

Rich (BB code):
handle = ShellExecute(0, "", Song, 0, 0, SW_SHOWNORMAL)
 

Watch MrExcel Video

Forum statistics

Threads
1,123,265
Messages
5,600,605
Members
414,394
Latest member
mahendar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top