PDA

View Full Version : Example: OGG Audio Player with Tag Updates


mrjoz
06-15-2006, 07:28 PM
Hi all, i hope this will be of use to a few ppl. What ive done is add/doctor examples to get what i believe is a dynamic audio player. but what i really want is to be able to call funtions from the Global Events so i dont have to put all this code on each and every page. As my project has multiple pages i feel that im wasting code and therefore resouces. So if anyone can workout how to make it Global it'll be even more productive to us all.

At Preload:
-- this gets the OGG files in the below folder and loads them into the listbox
atbFiles = File.Find("AutoPlay\\Audio\\music", "*.ogg", false, false, nil, nil);
TextFile.WriteFromTable("AutoPlay\\Audio\\music\\playlist.ini", atbFiles, false);
if atbFiles then
for i,v in atbFiles do
ListBox.AddItem("Music list", String.SplitPath(v).Filename, v);
end
end
audio = File.Find("AutoPlay\\Audio\\music", "*.ogg", false, false, nil, nil);



ListBox.SetVisible("Music list", false);

this gets the OGG Audio files from a folder and stores them in a table, while also writing the table to "playlist.ini" (this is because i am using multiple pages and it seems easier to call/clear a file). the last line of code also hides the listbox that the table is stored in (wasn't needed for me).

On show:
-- this gets the selected song and loads it.
ListBox.SelectItem("Music list", 1)
tbSelected = ListBox.GetSelected("Music list")
for index, item in tbSelected do
output = item;
end
if tbSelected then
strScript = ListBox.GetItemData("Music list", tbSelected[1]);
Audio.Load(CHANNEL_BACKGROUND, strScript, true, false)
end

-- Make sure the Load action was successful
if (Application.GetLastError() == 0) then

-- Check for file description tags
tags = Audio.GetOggTags(CHANNEL_BACKGROUND);
if (tags ~= nil) then
albumtitle = tags.TITLE;
artist = tags.ARTIST;
album = tags.ALBUM;
track = tags.TRACKNUMBER;
end
end

-- Update the status text display
Paragraph.SetText("track_title", albumtitle);
Paragraph.SetText("artist", artist);
Paragraph.SetText("current_ablum", album);
Paragraph.SetText("track_number", track);


this code selects the first Ogg track and starts playing it, while the latter code collects the OggTags and stores them in variables to be called on the paragraph objects displaying the Tag data

On audio:
-- This is to set up the next song when the one playing finishes.
--if you want more than 4 songs, just continue the code as below.
if e_State == "Finish" then
if output == 1 then
ListBox.SelectItem("Music list", 2)
strScript = ListBox.GetItemData("Music list", 2);
elseif output == 2 then
ListBox.SelectItem("Music list", 3)
strScript = ListBox.GetItemData("Music list", 3);
elseif output == 3 then
ListBox.SelectItem("Music list", 4)
strScript = ListBox.GetItemData("Music list", 4);
elseif output == 4 then
ListBox.SelectItem("Music list", 1)
strScript = ListBox.GetItemData("Music list", 1);
end

--this just gets the selected song and loads it.
tbSelected = ListBox.GetSelected("Music list")
for index, item in tbSelected do
output = item;
end
if tbSelected then
Audio.Load(CHANNEL_BACKGROUND, strScript, true, false)
end
end

tags = Audio.GetOggTags(CHANNEL_BACKGROUND);
if (tags ~= nil) then
albumtitle = tags.TITLE;
artist = tags.ARTIST;
album = tags.ALBUM;
track = tags.TRACKNUMBER;
end

-- Update the status text display
Paragraph.SetText("track_title", albumtitle);
Paragraph.SetText("artist", artist);
Paragraph.SetText("current_ablum", album);
Paragraph.SetText("track_number", track);


all the on audio code does is select the next track and play it after the current track finishes playing. oh, and it also updates the OggTags data which is displayed along side the listbox

On key:
-- e_Key is a built in variable that gets generated by the On Key event.
-- 13 is the virtual key code value for the [Enter] key.

if e_Key == 222 then
ListBox.SetVisible("Music list", true);
else
end

sets the listbox "music list" to visible on ---> # <--- key press

i have only been using this to troubleshoot and make sure the code is doing what i want it to do when i have no visial signs to work from.

On close
ListBox.DeleteItem("Music list", -1);


Code for listbox named "Music list"

tbSelected = ListBox.GetSelected("Music list")
for index, item in tbSelected do
output = item;
end
if tbSelected then
strScript = ListBox.GetItemData("Music list", tbSelected[1]);
Audio.Load(CHANNEL_BACKGROUND, strScript, true, false)
end


as i said, i use multiple pages. this code clears the listbox so when i jump page i dont get repeat items (tracks) in my now newly populated playlist

spunicher
09-04-2006, 10:31 AM
-- Update the status text display
Paragraph.SetText("track_title", albumtitle);
Paragraph.SetText("artist", artist);
Paragraph.SetText("current_ablum", album);
Paragraph.SetText("track_number", track);


not working Audio.GetOggTags please mp3 track?

please select all track play automatic??

Josué Alba
09-10-2006, 09:43 AM
Nice example you have there. I'm using it right now. :)

mrjoz
09-10-2006, 07:11 PM
thanx josue, u've got mail ;)

Jonas DK
12-28-2006, 08:18 PM
Changed the code to be a little bit more dynamicly.

instead of limiting your playlist to 4 songs I made it loop continuasly so that every time a song ends it just loads the next song on the list and when it hits the last song it starts over from 1.

you can also now add song will playing from the list. They are just added to the end because every time a song ends it checks the listbox to see how many items are in it.

anyway here is the code, maybe someone can fin tune it.

On Audio:
nPlaylist = ListBox.GetCount("liPlaylist");
if e_State == "Finish" then
if output >= nPlaylist then
output = 1
ListBox.SelectItem("liPlaylist", output)
strScript = ListBox.GetItemData("liPlaylist", output);
else output = output+1
ListBox.SelectItem("liPlaylist", output)
strScript = ListBox.GetItemData("liPlaylist", output);
end

--this just gets the selected song and loads it.
tbSelected = ListBox.GetSelected("liPlaylist")
for index, item in tbSelected do
output = item;
end
if tbSelected then
Audio.Load(CHANNEL_BACKGROUND, strScript, true, false)
end
end

By the way, the aydio player works fine with mp3 files as well they are just not officially supported by AMS.
cheers,
Jonas

SiNisTer
03-30-2007, 06:50 AM
nice piece of coding u got there!!
but i was wondering if u could help me out for an audio player i designed. I need a slider to keep track of the audio length :o as it plays like all other audio players...
wondering if u or anyone else can lend a hand..please!!

Thanks n,
keep it rockin