HydrogenAudio

Hosted Forums => foobar2000 => Development - (fb2k) => Topic started by: adamh on 2007-06-27 14:37:33

Title: playback_control problem
Post by: adamh on 2007-06-27 14:37:33
I'm trying to write some general purpose plugin, i only need start playing the current song and seek at specified position after fb2k start. So i tryed this code

Code: [Select]
class initquit_foobarstarter : public initquit
{
    void on_init();
    void on_quit();
};    

void initquit_foobarstarter::on_init()
{
    static_api_ptr_t<playback_control> pc;    
    pc->play_or_pause();  // start playing
    pc->pc->playback_seek(/*cfg_time*/90);      // seek at 90s    
}


but it doesnt work, the problem is brobably there

Code: [Select]
pc->play_or_pause();
pc->pc->playback_seek(/*cfg_time*/90);


is this fb2k behave correct, or do i something wrong ??
therefore i used timer, it seems that the solution works, but its not very clear

Code: [Select]
VOID CALLBACK seek_proc(HWND hWnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
{
    static_api_ptr_t<playback_control> pc;
    pc->playback_seek(cfg_time);
    KillTimer(NULL, g_seek_timer_id);
}

class initquit_foobarstarter : public initquit
{
    void on_init();
    void on_quit();
};    

void initquit_foobarstarter::on_init()
{
    static_api_ptr_t<playback_control> pc;
    pc->play_or_pause();
    g_seek_timer_id = SetTimer(NULL, NULL, 200, seek_proc);
}

its possible do this without using timer ????
Title: playback_control problem
Post by: bubbleguuum on 2007-06-27 16:00:43
I'm trying to write some general purpose plugin, i only need start playing the current song and seek at specified position after fb2k start. So i tryed this code

Code: [Select]
class initquit_foobarstarter : public initquit
{
    void on_init();
    void on_quit();
};    

void initquit_foobarstarter::on_init()
{
    static_api_ptr_t<playback_control> pc;    
    pc->play_or_pause();  // start playing
    pc->pc->playback_seek(/*cfg_time*/90);      // seek at 90s    
}


I'm not sure but it may be too soon to start playback

You may want to try:

Code: [Select]
class main_thread_play_callback : public main_thread_callback {

public:
    void callback_run() {
             static_api_ptr_t<playback_control> pc;    
             pc->play_or_pause();  // start playing
             pc->pc->playback_seek(/*cfg_time*/90);      // seek at 90s    
        }
};


and replace your code in on_init() by:

Code: [Select]
service_ptr_t<main_thread_play_callback> cb = new  service_impl_t<main_thread_play_callback>();
static_api_ptr_t<main_thread_callback_manager> cb_manager;
cb_manager->add_callback(cb);


This will ensure the task will run when all other plugins have been initialized
Title: playback_control problem
Post by: foosion on 2007-06-27 16:57:11
The safest way is to use a main_thread_callback as bubbleguuum said. If I understand correctly, you want to start the current song at a specific position. The best way to do this is this (put this in callback_run()):
Code: [Select]
static_api_ptr_t<playback_control> pc;

// position from song start (you don't need this local variable, it's just for clarity)
double pos = 90.0;

// start playback in paused mode
pc->start(playback_control::track_command_play, true);

// seek to desired position
pc->seek(pos);

// unpause playback
pc->pause(false);
Title: playback_control problem
Post by: adamh on 2007-06-27 17:30:56
thanks for replay, i will try
Title: playback_control problem
Post by: adamh on 2007-06-27 19:10:42
so i used main_thread_callback as you both said, my callback_run() code is

Code: [Select]
void main_thread_callback_play::callback_run()
{
    static_api_ptr_t<playback_control> pc;    
    pc->start(playback_control::track_command_play, true);  // start playback in paused mode
    pc->playback_seek(cfg_time);
    pc->pause(false);  // unpause playback

    // create report message
    ostringstream ostr("callback_run\n");
    ostr << "can_seek: " << pc->playback_can_seek() << "\n";
    ostr << "desired seek position: " << cfg_time << "\n";
    ostr << "track position after seek: " << pc->playback_get_position() << "\n";

    popup_message::g_show(ostr.str().c_str(), "info");    
}


and report message created in callback_run() is:

can_seek: 0
desired seek position: 128
track position after seek: 0

so the problem is that fb2k is not ready to seek after playback_control::start(track_command_play)/playback_control::play_or_pause()  is executed and playback_control::playback_seek(pos) is ignored

any ideas ????