Can you play different music randomly?

This forum is meant for requesting technical support or reporting bugs.

Moderators: time-killer-games, Vengeance66, Candle, reneuend, GM-Support

Can you play different music randomly?

Postby Morsy » Sun May 27, 2012 7:55 pm

Can you do this: When you go to a frame, it chooses a song randomly (Maybe from one of the channels).


That's about it. xD Can you?
User avatar
Morsy
Active Member
 
Posts: 88
Joined: Wed Feb 08, 2012 11:15 pm
Location: USA

Postby lbq_bcn » Mon May 28, 2012 12:41 am

Hello Morsy
See if you used this ....


Code: Select all
randomize timer
 x = int (rnd * 4) +1
 If x = 1 then PlaySound GetPath (4) + "music1.mp3", 0,9
 If x = 2 then PlaySound GetPath (4) + "music2.mp3", 0,9
 If x = 3 then PlaySound GetPath (4) + "music3.mp3", 0,9
 If x = 4 then PlaySound GetPath (4) + "music4.mp3", 0,9

you must place it in the VB frame, you have to define x as integer variables and use in routine PlaySound's here:
http://www.adventuremaker.com/phpBB2/viewtopic.php?t=5863&highlight=playsound
from my ignorance, I think is a good solution.

Regards!

Llu
User avatar
lbq_bcn
Member
 
Posts: 34
Joined: Tue May 15, 2012 10:50 pm
Location: Spain

Postby reneuend » Mon May 28, 2012 12:53 am

It's a good solution but you could take advantage of the fact that you used the random number in the filename!

Code: Select all
randomize timer
 x = int (rnd * 4) +1
PlaySound GetPath (4) + "music" + x + ".mp3", 0,9
---


Image
Image
User avatar
reneuend
Administrator
 
Posts: 2762
Joined: Sat Nov 22, 2008 8:37 pm
Location: Midwest Cornfield, USA

Postby lbq_bcn » Mon May 28, 2012 11:08 am

Hello Reneuend

It's a good solution but you could take advantage of the fact that you used the random number in the filename!


Much better your solution. It is "cleaner" and less lines of code.

Every day you learn something.

best regards!


Llu
User avatar
lbq_bcn
Member
 
Posts: 34
Joined: Tue May 15, 2012 10:50 pm
Location: Spain

Postby Morsy » Mon May 28, 2012 2:26 pm

reneuend wrote:It's a good solution but you could take advantage of the fact that you used the random number in the filename!

Code: Select all
randomize timer
 x = int (rnd * 4) +1
PlaySound GetPath (4) + "music" + x + ".mp3", 0,9

Example please? xD I'm lost here. Lets say I have 2 mp3s called SVS Theme Song and Battle Song, how do I play those randomly in code?
User avatar
Morsy
Active Member
 
Posts: 88
Joined: Wed Feb 08, 2012 11:15 pm
Location: USA

Postby reneuend » Mon May 28, 2012 2:43 pm

I blew it. I misunderstood. You don't need a timer because we aren't randomizing the time it will play.

NOTE: I tested the following and it works.


Code: Select all
rnd_play = RandomInt(1,2)
if rnd_play = 1 then
    PlaySoundWin7 GetPath(4) + "SVS Theme Song.mp3", 0, 9
else
     PlaySoundWin7 GetPath(4) + "Battle Song.mp3", 0, 9
end if




Do the following to set it up:

Advanced Tab - Variables:

rnd_play as integer

Advanced Tab - Procedures:

Code: Select all
Function RandomInt(lower,upper)
  Randomize
  RandomInt = int(rnd*(upper-lower+1))+lower
End Function 'RandomInt




The hotspot or frame properties or whatever it is that tiggers this event that plays the music

Code: Select all
rnd_play = RandomInt(1,2)
if rnd_play = 1 then
    PlaySoundWin7 GetPath(4) + "SVS Theme Song.mp3", 0, 9
    'NOTE: if not using AM4.6 use the following instead!
    PlaySound2 GetPath(4) + "SVS Theme Song.mp3", 9, 0
else
     PlaySoundWin7 GetPath(4) + "Battle Song.mp3", 0, 9
    'NOTE: if not using AM4.6 use the following instead!
    PlaySound2 GetPath(4) + "Battle Song.mp3", 9, 0
end if



For AM version 4.6 use the following
In the Advanced Tab - Procedures:

Code: Select all
Sub PlaySoundWin7(SoundFile, NumRepeat, ChannelNumber)
   If ChannelNumber < 9 Then
      'With VBScript it is currently not possible to interact with the 8 first audio channels.
      MsgBox "The specified audio channel is invalid.", vbCritical, "Error"
      Exit Sub
   End If
   If Action.IsComponentInstalled("NSPlay.NSPlayCtl.1") = False Then
      MsgBox "Unable to find the media player component.", vbCritical, "Error"
   Else
      If Action.DoesComponentExist("MediaPlayer" + CStr(ChannelNumber)) = false Then
         AddComponent "NSPlay.NSPlayCtl.1", "MediaPlayer" + CStr(ChannelNumber)
      End If
      Component("MediaPlayer" + CStr(ChannelNumber)).Object.FileName = SoundFile
      Component("MediaPlayer" + CStr(ChannelNumber)).Object.PlayCount = NumRepeat
      Component("MediaPlayer" + CStr(ChannelNumber)).Object.Play
   End If
End Sub



For older versions of AM use the following
In the Advanced Tab - Procedures:
Code: Select all
Sub PlaySound2(FileName, Channel, Repeat)
'PlaySound "c:\filename.mp3", False
'If the sound file is located in your "Areas" folder, use the "GetPath(4)" command:
'PlaySound GetPath(4) + "c:\filename.mp3", False
'To Stop a sound: StopSound
Action.LoadControl MediaPlayerObject(Channel)
MediaPlayerObject(Channel).FileName = FileName
If Repeat = True Then
MediaPlayerObject(Channel).PlayCount = 0
Else
MediaPlayerObject(Channel).PlayCount = 1
End If
MediaPlayerObject(Channel).Play
End Sub
Last edited by reneuend on Mon May 28, 2012 10:39 pm, edited 2 times in total.
---


Image
Image
User avatar
reneuend
Administrator
 
Posts: 2762
Joined: Sat Nov 22, 2008 8:37 pm
Location: Midwest Cornfield, USA

Postby mercedes » Mon May 28, 2012 10:32 pm

I used this once..for 3 sounds..to play randomly when they enter the frame..I adjusted it to 2...They repeat..

There is no clicking involved though, this is just when they enter the frame..;

Create Variable rndnum

Code: Select all
Sub randomsongs
randomize
rndnum = int(RND * 2)+1
If rndnum=1 Then
PlaySound GetPath(4) +  "SVS Theme Song.mp3", False
End If
If rndnum=2 Then
PlaySound GetPath(4) + "Battle Song.mp3", False
End If

End Sub



In the Frame's Advanced;
Code: Select all
Action.CreateTimedEvent 1, "randomsongs", True


**Make sure you have the Global procedures for Sound, be it 4.52 or 4.61..If you need help finding them, let us know..
The music should be in the correct folder..(4) is the Areas folder, I beleive..(2) is the External..***

Hope that helps..
User avatar
mercedes
VIP
 
Posts: 2460
Joined: Sun Mar 09, 2008 10:43 pm
Location: Canada..~

Postby reneuend » Mon May 28, 2012 10:41 pm

I included the sound procedures in my post for both AM 4.61 and for older versions. The only thing is, I renamed it from the original because I allowed for setting the channel as a parameter.
---


Image
Image
User avatar
reneuend
Administrator
 
Posts: 2762
Joined: Sat Nov 22, 2008 8:37 pm
Location: Midwest Cornfield, USA

Postby Morsy » Wed May 30, 2012 3:06 am

One more thing: Where will the code get the song from? (Folder?)
User avatar
Morsy
Active Member
 
Posts: 88
Joined: Wed Feb 08, 2012 11:15 pm
Location: USA

Postby reneuend » Wed May 30, 2012 4:08 am

GetPath(4) is the Areas folder
GetPath(2) is the external folder

You can set it up how you want when you use the GetPath command.
---


Image
Image
User avatar
reneuend
Administrator
 
Posts: 2762
Joined: Sat Nov 22, 2008 8:37 pm
Location: Midwest Cornfield, USA

Postby Morsy » Fri Jun 01, 2012 3:16 am

Code: Select all
rnd_play = RandomInt(1,2)
 if rnd_play = 1 then
      PlaySound2 GetPath(4) + "SVS Theme Song.mp3", 9, 0
 else
      PlaySound2 GetPath(4) + "Battle Song.mp3", 9, 0
 end if


I put this in the Advanced tab of the frame properties, put the songs in the areas folder, and test the frame.
What I get in return is an error message that says "Type mismatch "PlaySound2". D:
User avatar
Morsy
Active Member
 
Posts: 88
Joined: Wed Feb 08, 2012 11:15 pm
Location: USA

Postby mercedes » Fri Jun 01, 2012 3:20 am

Morsy wrote:
Code: Select all
rnd_play = RandomInt(1,2)
 if rnd_play = 1 then
      PlaySound2 GetPath(4) + "SVS Theme Song.mp3", 9, 0
 else
      PlaySound2 GetPath(4) + "Battle Song.mp3", 9, 0
 end if


I put this in the Advanced tab of the frame properties, put the songs in the areas folder, and test the frame.
What I get in return is an error message that says "Type mismatch "PlaySound2". D:



Double check procedures, where it says PlaySound7 and PlaySound2...One is for 4.61 [Win7] and one is for 4.52...[XP].

Make sure they correspond, when calling routine in the Advanced Tab of the frame's properties..
User avatar
mercedes
VIP
 
Posts: 2460
Joined: Sun Mar 09, 2008 10:43 pm
Location: Canada..~

Postby reneuend » Fri Jun 01, 2012 4:31 am

Morsy wrote:
Code: Select all
rnd_play = RandomInt(1,2)
 if rnd_play = 1 then
      PlaySound2 GetPath(4) + "SVS Theme Song.mp3", 9, 0
 else
      PlaySound2 GetPath(4) + "Battle Song.mp3", 9, 0
 end if


I put this in the Advanced tab of the frame properties, put the songs in the areas folder, and test the frame.
What I get in return is an error message that says "Type mismatch "PlaySound2". D:


You got a type mismatch for PlaySound2 means that the system thinks PlaySound2 is a variable probably. I'm guessing you didn't add the PlaySound2 function to the procedures area.

You need to go to Advanced Tab - PROCEDURES
Then add the following function and click on OK.

Code: Select all
Sub PlaySound2(FileName, Channel, Repeat)
'PlaySound "c:\filename.mp3", False
'If the sound file is located in your "Areas" folder, use the "GetPath(4)" command:
 'PlaySound GetPath(4) + "c:\filename.mp3", False
'To Stop a sound: StopSound
Action.LoadControl MediaPlayerObject(Channel)
MediaPlayerObject(Channel).FileName = FileName
If Repeat = True Then
MediaPlayerObject(Channel).PlayCount = 0
Else
MediaPlayerObject(Channel).PlayCount = 1
End If
MediaPlayerObject(Channel).Play
End Sub
---


Image
Image
User avatar
reneuend
Administrator
 
Posts: 2762
Joined: Sat Nov 22, 2008 8:37 pm
Location: Midwest Cornfield, USA

Postby Morsy » Fri Jun 01, 2012 9:48 pm

After putting the code in the procedures tab, I get this:
Type mismatch "RandomInt"
User avatar
Morsy
Active Member
 
Posts: 88
Joined: Wed Feb 08, 2012 11:15 pm
Location: USA

Postby reneuend » Sat Jun 02, 2012 12:32 am

You need to add RandomInt() to the procedures tab also!!!

Code: Select all
Function RandomInt(lower,upper)
  Randomize
  RandomInt = int(rnd*(upper-lower+1))+lower
End Function 'RandomInt
---


Image
Image
User avatar
reneuend
Administrator
 
Posts: 2762
Joined: Sat Nov 22, 2008 8:37 pm
Location: Midwest Cornfield, USA

Postby Morsy » Sat Jun 02, 2012 11:55 pm

OMG, type mismatch: PlaySound

Current Procedures tab:
Code: Select all
Sub PlaySound2(FileName, Channel, Repeat)
  PlaySound GetPath(4) + "c:\filename.mp3", False
 Action.LoadControl MediaPlayerObject(Channel)
 MediaPlayerObject(Channel).FileName = FileName
 If Repeat = True Then
 MediaPlayerObject(Channel).PlayCount = 0
 Else
 MediaPlayerObject(Channel).PlayCount = 1
 End If
 MediaPlayerObject(Channel).Play
 End Sub
Function RandomInt(lower,upper)
   Randomize
   RandomInt = int(rnd*(upper-lower+1))+lower
 End Function 'RandomInt


Current VBScript:
Code: Select all
rnd_play = RandomInt(1,2)
 if rnd_play = 1 then
      PlaySound2 GetPath(4) + "SVS Theme Song.mp3", 9, 0
 else
      PlaySound2 GetPath(4) + "Battle Song.mp3", 9, 0
 end if


:lol:
User avatar
Morsy
Active Member
 
Posts: 88
Joined: Wed Feb 08, 2012 11:15 pm
Location: USA

Postby reneuend » Sun Jun 03, 2012 3:39 am

Please tell me you have it working!!!! :P

If not, I will send you a sample project with instructions.
---


Image
Image
User avatar
reneuend
Administrator
 
Posts: 2762
Joined: Sat Nov 22, 2008 8:37 pm
Location: Midwest Cornfield, USA

Postby Morsy » Sun Jun 03, 2012 1:47 pm

reneuend wrote:Please tell me you have it working!!!! :P

If not, I will send you a sample project with instructions.

It said type mismatch: PlaySound, so no
User avatar
Morsy
Active Member
 
Posts: 88
Joined: Wed Feb 08, 2012 11:15 pm
Location: USA

Postby reneuend » Sun Jun 03, 2012 10:08 pm

You have a call to PlaySound instead of PlaySound2. Check you're code behind the hotspot advanced tab.
---


Image
Image
User avatar
reneuend
Administrator
 
Posts: 2762
Joined: Sat Nov 22, 2008 8:37 pm
Location: Midwest Cornfield, USA

Re: Can you play different music randomly?

Postby Morsy » Mon Jul 06, 2020 8:21 pm

First, this must be in the VBScript Global Procedures.
Code: Select all
Function RandomInt(lower,upper)
  Randomize
  RandomInt = int(rnd*(upper-lower+1))+lower
End Function 'RandomInt
Sub PlaySoundWin7(SoundFile, NumRepeat, ChannelNumber)
   If ChannelNumber < 9 Then
      'With VBScript it is currently not possible to interact with the 8 first audio channels.
      MsgBox "The specified audio channel is invalid.", vbCritical, "Error"
      Exit Sub
   End If
   If Action.IsComponentInstalled("NSPlay.NSPlayCtl.1") = False Then
      MsgBox "Unable to find the media player component.", vbCritical, "Error"
   Else
      If Action.DoesComponentExist("MediaPlayer" + CStr(ChannelNumber)) = false Then
         AddComponent "NSPlay.NSPlayCtl.1", "MediaPlayer" + CStr(ChannelNumber)
      End If
      Component("MediaPlayer" + CStr(ChannelNumber)).Object.FileName = SoundFile
      Component("MediaPlayer" + CStr(ChannelNumber)).Object.PlayCount = NumRepeat
      Component("MediaPlayer" + CStr(ChannelNumber)).Object.Play
   End If
End Sub


Secondly, there must be some VB Variables named rndnum and rnd_play. Both are integers.

Finally, here is what goes in the Frame itself for your project.
Code: Select all
randomize
rnd_play = int( RND * 4 ) + 1
If rnd_play = 1 Then
    PlaySoundWin7 GetPath(4) + "GMOD GMOD 1.mp3", 0, 9
    End If
If rnd_play = 2 Then
    PlaySoundWin7 GetPath(4) + "GMOD GMOD 2.mp3", 0, 9
    End If
If rnd_play = 3 Then
    PlaySoundWin7 GetPath(4) + "GMOD GMOD 3.mp3", 0, 9
    End If
If rnd_play = 4 Then
    PlaySoundWin7 GetPath(4) + "GMOD GMOD 4.mp3", 0, 9
    End If



The file names that begin with "GMOD GMOD" change depending on the name of your sound files that you want to roll the dice on. This is helpful for making a starting screen where you want the music to be randomly played/selected. Or any screen, it doesn't necessarily have to be a STARTING screen.
User avatar
Morsy
Active Member
 
Posts: 88
Joined: Wed Feb 08, 2012 11:15 pm
Location: USA

Re: Can you play different music randomly?

Postby reneuend » Mon Jul 12, 2021 4:09 am

You could also use the Select statement, which can be better when there are several IFs:

Code: Select all
Select Case (rnd_play)
Case 1:

Case 2:

Case 3:

Case 4:

End Select


or even better in this case, just replace the character in the string with rnd_play:

Code: Select all
If rnd_play > 0 And rnd_play <= 4 Then
    PlaySoundWin7 GetPath(4) + "GMOD GMOD" + rnd_play + ".mp3", 0, 9
Else
    'Do something here where rnd_play is what is expected
End
---


Image
Image
User avatar
reneuend
Administrator
 
Posts: 2762
Joined: Sat Nov 22, 2008 8:37 pm
Location: Midwest Cornfield, USA

Re: Can you play different music randomly?

Postby Morsy » Sat Jul 17, 2021 9:31 pm

reneuend wrote:You could also use the Select statement, which can be better when there are several IFs:

Code: Select all
Select Case (rnd_play)
Case 1:

Case 2:

Case 3:

Case 4:

End Select


or even better in this case, just replace the character in the string with rnd_play:

Code: Select all
If rnd_play > 0 And rnd_play <= 4 Then
    PlaySoundWin7 GetPath(4) + "GMOD GMOD" + rnd_play + ".mp3", 0, 9
Else
    'Do something here where rnd_play is what is expected
End


Thank you for your more efficient code reneuend.
User avatar
Morsy
Active Member
 
Posts: 88
Joined: Wed Feb 08, 2012 11:15 pm
Location: USA


Return to Adventure Maker Technical Support and Bug Reports

Who is online

Users browsing this forum: No registered users and 0 guests

cron