Skip to main content

Notice

Please note that most of the software linked on this forum is likely to be safe to use. If you are unsure, feel free to ask in the relevant topics, or send a private message to an administrator or moderator. To help curb the problems of false positives, or in the event that you do find actual malware, you can contribute through the article linked here.
Topic: Locating track index in playlist (Read 3385 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

Locating track index in playlist

This may be a simple question, so please forgive me.  I am attempting to locate a track in the current playlist, set the selection to that track and then play it.  I seem to be having trouble finding out how to get the index of a track when I know it's name. Could anyone provide any pointers here..?

Thanks.

Locating track index in playlist

Reply #1
What do you mean by name? The filename or the value of some tag fields?

For the filename, it would be something like this:
Code: [Select]
// tracks are identified by filename and subsong index.
// the subsong index is usually 0 for files that contain only a single track.
const char * filename = "bla.mp3";
int subsong = 0;
// retrieve the unique metadb_handle for the wanted track
metadb_handle * handle = metadb::get()->handle_create(make_playable_location(filename, subsong);
// get pointer to playlist_oper service
playlist_oper * pl = playlist_oper::get();
// search track in playlist
int n, count = pl->get_count();
for (n = 0; n < count; n++)
{
   // metadb_handles can be compared by reference when checking for identity
   metadb_handle * handle2 = pl->get_item(n);
   if (handle == handle2)
   {
        pl->set_focus_sel(n);
        pl->play_item(n);
        n = count;
   }
   // we have to release all handles returned by get_item()
   handle2->handle_release();
}
// release the handle returned by handle_create()
handle->handle_release();

Please note that the above code is written from memory, so it may not compile as-is.

Locating track index in playlist

Reply #2
I am going to use the file path if that will work correctly as that is guaranteed to be unique.  I had started to make some progress towards it, but this helps a lot.  Many Thanks.

Update:
OK, I have got exactly what I wanted working with a modified version of foo_uie_albumlist, where another option has been added to allow for double click/middle click to locate a track in the current playlist, or to locate and play the first track of an album if an album node is selected in the list.  Thanks for the help.

Re: Locating track index in playlist

Reply #3
<<<<<make_playable_location(filename, subsong);
If there are other language's characters in "filename",
How to do?!

Re: Locating track index in playlist

Reply #4
foobar2000 expects string to be UTF-8 encoded. This also works for the filename. If you have an UTF-16 (WCHAR based) string, you can convert that using the helper pfc::stringcvt::string_utf8_from_wide.

 

Re: Locating track index in playlist

Reply #5
Thank you ^^