Visuals, audio, animation. Blended, not stirred. If LiveCode is part of your rich media production toolbox, this is the forum for you.
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
-
newrev
- Posts: 6
- Joined: Thu Nov 05, 2009 7:29 am
Post
by newrev » Wed Dec 02, 2009 9:04 pm
Another newbie question for you.
Is there a way to load a folder containing audio files into an array and then play those files in the player? I can do this if I load the folder into a list but cannot do it when I load the folder into a data grid (or array). I cannot figure out how to access the elements of the array in such a way that the player can play them (and, finally, randomize the play back of them).
Here is the code I have for displaying the array of audio files on a data grid:
Code: Select all
on mouseUp
put listFiles(currentFolder(), currentRecurse()) into myArrayA
set the dgText of group "DataGrid 1" to myArrayA
end mouseUp
And here is the code for the "play" button (thanks to SparkOut for his previous help on sequential file play):
Code: Select all
on mouseUp
//Attempting to loop through all the audio files in the array, playing the entirety of each one
repeat for each element AudioFile in myArrayA
set the uStoppedFlag of player 1 to false
if the filename of player 1 is " " then exit mouseUp
lock messages
//can you set the filename of a player to an element of an array?
set the filename of player 1 to AudioFile
unlock messages
start player 1
wait until the uStoppedFlag of player 1 is true with messages
end repeat
end mouseUp
Ultimately, I need to be able to access the audio files in the array randomly. Something along the lines of:
Code: Select all
put the keys of myArrayA into myKeys
sort lines of myKeys by random(number of lines of myKeys)
repeat for each line myKey in myKeys
--play audio file
end repeat
Much thanks!
-
SparkOut
- Posts: 2944
- Joined: Sun Sep 23, 2007 4:58 pm
Post
by SparkOut » Thu Dec 03, 2009 8:11 pm
Would repeats be a problem? You could do this very simply by choosing "any line of the keys of..." but of course you may already have picked that random line before having played every one in the list.
-
newrev
- Posts: 6
- Joined: Thu Nov 05, 2009 7:29 am
Post
by newrev » Sat Dec 05, 2009 1:16 am
hi SparkOut
Yeah, repeats would be a problem. I would like the audio files in the array to be randomized before they are played. Something along the lines of:
Load ArrayA with audio files from FolderX
Randomize the elements of ArrayA
Play each element in ArrayA
But right now I am unclear how to work with the elements in an array. I've scoured the dictionary, the user guide, and the forums with no luck. It should be so simple...i must be overlooking the obvious

. The following code does not list the elements of my array (or any one element for that matter). Why?
Code: Select all
on mouseUp
//Create an array from files contained in a folder
put listFiles(currentFolder(), currentRecurse()) into myArrayB
//Now to look at the elements in myArrayB...
//but nothing appears in Field B
put the keys of myArrayB into fld "Field B"
or
put any line of the keys of myArrayB into fld "Field B"
end mouseUp
-
SparkOut
- Posts: 2944
- Joined: Sun Sep 23, 2007 4:58 pm
Post
by SparkOut » Sat Dec 05, 2009 9:05 pm
If nothing is showing then it suggests that there is no data in the array to interrogate. Can you expand on the functions you are using to load the data in the first place?
If you use the debugger in the IDE to insert a breakpoint and step through the code of your "mouseUp" handler, you can use the variable watcher to see what is in the array. (Use the disclosure arrow to view the array keys and select them to view the contents).
-
newrev
- Posts: 6
- Joined: Thu Nov 05, 2009 7:29 am
Post
by newrev » Sun Dec 06, 2009 11:51 pm
Well, here is the code I am using for loading the array:
Code: Select all
put listFiles(currentFolder(), currentRecurse()) into myArrayB
put empty into tArray
put empty into tArrayIndex
repeat for each item tNum in myArrayB
put tNum into tArray[tArrayIndex]
add 1 to tArrayIndex
end repeat
put tArray into tWorkArray
combine tWorkArray using return
//Now to randomize the files in tWork Array
sort lines of tWorkArray by random(number of lines of tWorkArray)
And here is code (on a button) to check the randomization:
Code: Select all
//If I simply check the array, it shows the randomization worked...
put tWorkArray into fld "Field B" --this works
//But if I want to check the keys of the array, nothing appears in field b.
put the keys of tWorkArray into fld "Field B" --this does not work
-
SparkOut
- Posts: 2944
- Joined: Sun Sep 23, 2007 4:58 pm
Post
by SparkOut » Mon Dec 07, 2009 11:55 am
Ah, well (although I'd still like to see your listfiles() and currentfolder() and currentrecurse() functions) it seems that you have at least seen part of the problem yourself.
When you have taken the "combine tWorkArray using return" step, then you have converted the array into a list (delimited by return) - so the "elements" are now "lines" of the variable. That is implicit in your "sort lines of tWorkArray by random(number of lines of tWorkArray)" step.
If you now do a "repeat for each line tLine in tWorkArray" then tLine should have the "element" you referred to in the original array each time, as part of the line string, separated from the original key by a default delimiter (expected tab) because the "elements" are now only "lines" not addressed by "keys" in an array any more.
-
bn
- VIP Livecode Opensource Backer

- Posts: 4171
- Joined: Sun Jan 07, 2007 9:12 pm
Post
by bn » Mon Dec 07, 2009 12:43 pm
Newrev,
if you want to see tArrayIndex after combining try
combine tWorkArray using return and comma
this gives you "1,something" 1 beeing the the key and something the content of myArray[key] as a return delimited list of the array. So you can sort by item 1 of the list etc.
As Sparkout pointed out after combining an array there are no more keys, it is not an array anymore.
regards
Bernd
-
newrev
- Posts: 6
- Joined: Thu Nov 05, 2009 7:29 am
Post
by newrev » Tue Dec 08, 2009 5:13 pm
Thank you both for the clarification re: arrays. I believe the documentation is rather confusing, especially regarding the sort and combine functions.
The user guide (page 192) states that:
combine: convert text to an array using delimiters that you define
[...]
split: converts an array into text, placing delimiters you separate between the elements
But the dictionary states the opposite:
For the combine command
Summary: Transforms an array into a list.
The combine command combines the elements of the array into a single variable.
[...]
For the split command:
Summary: Transforms a list into an array.
Comments: The split command separates the parts of the variable into elements of an array.
Given you clarification, I now have the program working properly.
-
bn
- VIP Livecode Opensource Backer

- Posts: 4171
- Joined: Sun Jan 07, 2007 9:12 pm
Post
by bn » Wed Dec 09, 2009 12:13 pm
Newrev,
I looked up the contradiction you found in user documentation / dictionary in the quality control center and it has been reported for 3.0 already. I added a note that is still not fixed.
http://quality.runrev.com/qacenter/show_bug.cgi?id=7126
This kind of error is not helpful for anyone and should be easy to fix.
regards
Bernd