Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
squirreltown
Roku Guru

Array index

Trying to get a value from an array of audio tracks. I cant use the index from AudioplayerEvent because it always starts at O even if you decide to start playing from track 3 so I've put an index value in the array, an integer, to identify the track so the screen can be accurate.
Num:"0"
When i try to access the value i get the debugger error "non-numeric array index not allowed" code excerpted below, which i dont understand, can someone explain?
thanks

item=[
{
Url: "http://www.xxx.com/roku/songs/song.mp3",
....other parameters
Num:"0"
},
{
.....other tracks
}]


if type (msg) = "roAudioPlayerEvent"
if msg.isListItemSelected()
m.song = msg.GetIndex()
end if
if msg.isStatusMessage() then
if msg.getmessage() = "start of play"
m.song = item["Num"]
scr.SetContent(item[m.song])
end if
end if



231: End Function
Attempt to use a non-numeric array index not allowed. (runtime error &he8) in ...c/pkg:/source/audioscreen.brs(219)

219: m.song = item["Num"]
Kinetics Screensavers
0 Kudos
20 REPLIES 20
RokuJoel
Binge Watcher

Re: Array index

if you put quotes around a number, it becomes a string, if you want to use a string variable, you'll have to use the toint() function to convert to integer when you use it as an array index.


-Joel
0 Kudos
squirreltown
Roku Guru

Re: Array index

Thanks Joel, I'll do that - It didn't work without the quotes , I guess that is because the array format is only allowing strings?
Kinetics Screensavers
0 Kudos
squirreltown
Roku Guru

Re: Array index

If this information were available some where else i would be able to find it and not have to ask basic questions like this.

m.song = item["Num"]

what is the syntax for converting "num" in this context with toInt()?

all the examples of toInt() I've found are like - Playstart.ToInt() a variable with a dot operator

So I've tried a bunch of things that dont work Like this:

var = item["Num"]
m.song = var.toInt()
scr.SetContent(item[m.song])

I keep getting the "non-numeric index" error on the first line.
What am i doing wrong?
Kinetics Screensavers
0 Kudos
RokuJoel
Binge Watcher

Re: Array index

Ok, looking at your code. Item is an array of associative arrays each containing the information to play one song. The first song in that array would be item[0], the next song in the array would item[1] etc. You probably don't need to store the array index with the song in the array, since the song is already at that index value in the array.

if you wanted for some reason to get the value of a variable within one of the songs in your array, lets say the second song, you would need to do something like this:

m.song=item[1]["num"]

or

m.song=item[1].num


You might want to track your position in the song array with a variable that you increment yourself, for example a variable called songindex:

else if msg.getmessage()="end of stream" then
if songindex < item.count() -1 then
songindex=songindex+1
scr.setcontent(item[songindex])
end if


- Joel
0 Kudos
squirreltown
Roku Guru

Re: Array index

Thank you very much Joel, syntax is everything.

"RokuJoel" wrote:
You probably don't need to store the array index with the song in the array, since the song is already at that index value in the array.


You are absolutely correct. I really dont need to do that. Except that I dont know the syntax for getting the current position of an array and believe me I've looked for it everywhere. If i knew that i wouldn't be doing it this silly way. It has to be something like

item.index
return item.index

But then "something like" just gets me hours of errors so I'm going with the only thing i can think of. I'd love to use your very helpful "songindex" code, but i cant without defining "songindex" as the current array position first right?

thats the problem i have here, i dont know the index - I need to ask the array where its at. Th audio is playing in the background and all is fine until the track advances and then my screens are off.

edit - this is happening in a slideshow, you turn the music on, leave the springboard for the slideshow, and i want to have the correct track showing when you return to it . The audio part goes along just fine its getting the screen to track what the audio is doing is my issue.


Thanks again for your help.
Kinetics Screensavers
0 Kudos
RokuJoel
Binge Watcher

Re: Array index

you have to track it manually so to speak:

index=0

currentsong=item[index]

screen.setcontent(currentsong)

if the current playing song has ended then check if index is less than the maximum number of items, if so then increment index:


if index < item.count() - 1 then index=index+1

currentsong=item[index]

screen.setcontent(currentsong)


- Joel
0 Kudos
squirreltown
Roku Guru

Re: Array index

Ok i probably could have saved you a bunch of time by asking the correct question, as I am already doing what you've described - tracking it manually.

Opening the springboard screen sets the index variable (m.song in my case) at 0 if the music is not playing.
You either start the music then or select another track. If you start from another track, the m.song variable gets set manually the way you said.
Now close the screen and open the slideshow. Now pause the slideshow and open the springboard screen again.
Because of the m. in the index variable the springboard screen remembers the track you chose to start from and shows it, so everything so far works just fine until:

the audio advances to the next song.

Here is the correct question i should have asked: How do i ask the array - not the audio player, where its currently at?
If i could get that info, I could come back to the right screen info from anywhere and i could make the screen update on track change when its open.

I'm sorry i didn't ask this right away, theres a lot of running in circles when you dont know what your doing.
Kinetics Screensavers
0 Kudos
squirreltown
Roku Guru

Re: Array index

This advances the screen info to match the audio when the springboard is open so thats one problem solved. Hopefully, since the m. causes the variable to be saved when the springboard closes and re-opens, i can add this to the other message loops and it will increment then too. Hope to edit this post after with positive results.

if type (msg) = "roAudioPlayerEvent"
if msg.isListItemSelected()
m.song = msg.GetIndex()
end if
if msg.isStatusMessage() then
if msg.getmessage() = "start of play"
scr.SetContent(item[m.song])
end if
end if


Edit - Nope that doesnt work in the background. Still think my "index in the array" scheme will work if i can figure out the item["Num"].toInt() syntax.
Kinetics Screensavers
0 Kudos
RokuJoel
Binge Watcher

Re: Array index

You need to increment the index when the track ends. If you are calling a slideshow, you need to pass the index to the slideshow, and return the index back to the springboard function when the slideshow ends:

index=slideshow(item,index)


and your slideshow:

function slideshow(item as object, index as integer) as integer
.....
return index

end function
0 Kudos
Need Assistance?
Welcome to the Roku Community! Feel free to search our Community for answers or post your question to get help.

Become a Roku Streaming Expert!

Share your expertise, help fellow streamers, and unlock exclusive rewards as part of the Roku Community. Learn more.