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: How to add file to playlist (Read 6906 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

How to add file to playlist

Hello everyone.

I'm having problems with addition of new file to the playlist. Here's my code:

Code: [Select]
void someFunction(char *fileName) {

    static_api_ptr_t<playlist_manager> plm;

    playable_location_impl loc;
    loc.set_path(fileName);

    metadb_handle_ptr locHandle;
    static_api_ptr_t<metadb>()->handle_create(locHandle, loc);
    pfc::list_t<metadb_handle_ptr> list;
    list.add_items(list);

    plm->activeplaylist_add_items(list, bit_array_true());
}


Last line of the code doesn't change anything in the playlist. What does p_selection parameter mean in function activeplaylist_add_items after all?

Thanks.

How to add file to playlist

Reply #1
personally I would prefer to use activeplaylist_add_locations() instead, it's a lot easier.

Code: [Select]
void someFunction(char *fileName) {

    static_api_ptr_t<playlist_manager> plm;
    pfc::list_t<char *> list;
        
    list.add_item(fileName);
    plm->activeplaylist_add_locations(list, true, NULL);
}


Quote
What does p_selection parameter mean

it means if the items added to the current playlist by default selected or not.

How to add file to playlist

Reply #2
Some rewriting of your code is in order:
Code: [Select]
void someFunction(char const *fileName) { // added const modifier

    static_api_ptr_t<playlist_manager> plm;

    metadb_handle_ptr handle;
    static_api_ptr_t<metadb>()->handle_create(handle, make_playable_location(fileName, 0));

    metadb_handle_list list; // special list type for metadb_handle_ptr
    list.add_item(handle); // add the handle to the list, not the list to itself

    plm->activeplaylist_undo_backup(); // allow user to undo the playlist modification
    plm->activeplaylist_add_items(list, bit_array_true()); // p_selection sets the selection state of the added items
}

It is recommended to use (lists of) metadb handles to pass track parameters to functions instead of raw paths. Even playable locations are preferable to raw paths, since the subsong is significant for some inputs. Also, a better way to turn an arbitrary path into a list of metadb handles is to use playlist_loader::process_path(_ex). What exactly are you trying to do?

How to add file to playlist

Reply #3
I'm trying to replicate functionality similar to foobar's open file menu in my plugin, i.e., I need to be able to open/add playable locations, playlists of files and cue sheets.

I came to the way of adding playables and playlists, via playlist manager (as was suggested in above posts) and playlist_loader::g_load_playlist, respectively. It works, although I'm not satisfied that I have to manually check filename to be added to distinguish playable location from playlist, and, what is worse for me, that foobar won't load meta(?) data of added files unless it starts to play them, displaying only filenames of added files in its playlist.
So the problem is I haven't found the fay of asking foobar to refresh (load) whole playlist's meta info at once.

And the question of correctly adding cue sheets remains open.

What safe and reliable method of handling all these tasks would you suggest?

Thanks in advance.


 

How to add file to playlist

Reply #5
playlist_incoming_item_filter_v2::process_locations_async().


D'uh, and the elephant, of course, has been left unmentioned. Thanks!

Is something like this is correct? I'm a bit worried about the way I'm handling the callback.

Code: [Select]
class process_locations_notify_my : public process_locations_notify {
public:
    void on_completion(const pfc::list_base_const_t<metadb_handle_ptr> & p_items)
    {
        static_api_ptr_t<playlist_manager> plm;
        plm->activeplaylist_add_items(p_items, bit_array_true());
    };

    void on_aborted() { };
};

[..........]

service_ptr_t<process_locations_notify_my> p_notify = new service_impl_t<process_locations_notify_my>();

static_api_ptr_t<playlist_incoming_item_filter_v2>()->process_locations_async(
    files,
    playlist_incoming_item_filter_v2::op_flag_background,
    NULL,
    NULL,
    NULL,
    p_notify
);