Creating an Object to use .play from the value in a string

Hey All,

I have MP3 Files that I’ve added to an XoJo project and named them R0 through R21. I want to randomly play one of the sounds.

The following code chooses the random number and then forms the name of the sound I want to play in a string. How do I create an object using the name in in the string “MySound” that I can then use the ObjectName.Play and have it play that sound?

i = System.Random.Inrange(0,21)
lbl_result.value = aResponse(i)
MySound = “R” + i.tostring

It seems like a few lines added to my code will do the trick, but I’ve yet to figure out how to do it. Any suggestions?

Create a dictionary with keys as the strings, and values as the MP3 files. https://documentation.xojo.com/api/language/dictionary.html

Hi Jason, thanks for the info on Dictionaries. From what I can see, with the simple app I’m working on, Dictionaries will actually require more code and make it a bit more complicated since I need a dictionary entry for each sound file. Then I need the code to access each item. For this situation, an if then statement is much more straightforward and less code. I was hoping for something that could make a decision based on the MyString contents and some how cast that to that correct format to use .play. That would reduce the amount of code.

I will certainly learn about using Dictionaries for future use! :smiley:

Written in post editor, may need minor corrections:

// List the playable sound objects
var aroSounds(21) as Sound = Array( _
  aroSounds(0) = R0, _
  aroSounds(1) = R1, _
  aroSounds(2) = R2, _
  aroSounds(3) = R3, _
  aroSounds(4) = R4, _
  aroSounds(5) = R5, _
  aroSounds(6) = R6, _
  aroSounds(7) = R7, _
  aroSounds(8) = R8, _
  aroSounds(9) = R9, _
  aroSounds(10) = R10, _
  aroSounds(11) = R11, _
  aroSounds(12) = R12, _
  aroSounds(13) = R13, _
  aroSounds(14) = R14, _
  aroSounds(15) = R15, _
  aroSounds(16) = R16, _
  aroSounds(17) = R17, _
  aroSounds(18) = R18, _
  aroSounds(19) = R10, _
  aroSounds(20) = R20, _
  aroSounds(21) = R21 _
)

// Pick a random sound
var iRandom as Integer = System.Random.InRange(0, aroSounds.LastIndex)

// Play it
aroSounds(iRandom).Play
2 Likes

Thanks Tim, I will check this out. It does still require defining each item.

The short answer is, no, you cannot get the Xojo object directly from a string. It might actually be better to have the sound files in your resources folder and access them from there, rather than dragging them into your project.

i = System.Random.Inrange(0,21)
lbl_result.value = aResponse(i)
MyFileName = “R” + i.tostring + ".snd"
MyFile = SpecialFolder.Resource(MyFileName)
MySound = Sound.Open(MyFile)
MySound.Play
1 Like

Thanks Tim, this looks promising! I’m assuming that the “.snd” in the 3rd line could be “.mp3” which is the format of the audio files.

After you add the setup in an open event, accessing a dictionary entry is very easy: Example I typed into the forum:

// in a window, module, app property:
var mySoundDictionary as Dictionary

// in open event
mySoundDictionary = new Dictionary
mySoundDictionary.value("R0") = R0
mySoundDictionary.value("R1") = R1
// etc ...
mySoundDictionary.value("R21") = R21

// when accessing sound to play
i = System.Random.Inrange(0,21)
lbl_result.value = aResponse(i)
MySoundString = “R” + i.tostring
MySound = mySoundDictionary.value(MySoundString)
MySound.Play()

Thanks Jason, it’s still more code required than my simple “if then statements” I do appreciate your suggestion.

Yes, sound.open() can open mp3 files. Check your Resources file in your build folder, the mp3 files might already be there since you dragged them in to the project.