当我在C和Ubuntu中使用fork()时,Vlc库失败

时间:2019-10-07 10:57:36

标签: c ubuntu fork libvlc

我不明白为什么会一直出现此错误:

    ALSA lib pcm_dmix.c:1108:(snd_pcm_dmix_open) unable to open slave
[00005617d3cf0630] alsa audio output error: cannot open ALSA device "default": No such file or directory
[00005617d3cf0630] main audio output error: Audio output failed
[00005617d3cf0630] main audio output error: The audio device "default" could not be used:
No such file or directory.
[00005617d3cf0630] main audio output error: module not functional
[00007f2ce803b080] main decoder error: failed to create audio output

当我尝试使用fork()复制mp3文件时。

如果我在fork()之外使用它,则代码可以正常工作。 子进程似乎无法访问硬件声卡? 我真的不明白如何解决这个问题。

pid = fork();
     printf("il pid: %d\n", pid);
     if (pid == 0) {
         libvlc_instance_t *inst;
    libvlc_media_player_t *mp;
    libvlc_media_t *m;

    // load the vlc engine
    inst = libvlc_new(0, NULL);
    printf("apro il file \n");
    // create a new item
    m = libvlc_media_new_path(inst, "/home/robodyne/Downloads/file.mp3");

    // create a media play playing environment
    mp = libvlc_media_player_new_from_media(m);

    // no need to keep the media now
    libvlc_media_release(m);

    // play the media_player
    libvlc_media_player_play(mp);

    sleep(10);

    // stop playing
    libvlc_media_player_stop(mp);

    // free the media_player
    libvlc_media_player_release(mp);

    libvlc_release(inst);

        exit(0);
}

EDIT1:我正在尝试使用THREADS

按照Antti Haapala的建议,我尝试使用线程而不是fork(),但是当我调用“ pthread_cancel(thread);”时,mp3不会停止。

这是我的新代码:

pthread_t thread;

void *wait(void*)
{
    libvlc_instance_t *inst;
    libvlc_media_player_t *mp;
    libvlc_media_t *m;
    // load the vlc engine
    inst = libvlc_new(0, NULL);
    printf("apro il file %d\n", inst);
    // create a new item
    m = libvlc_media_new_path(inst, "/home/robodyne/Downloads/file.mp3");
    // create a media play playing environment
    mp = libvlc_media_player_new_from_media(m);
    // no need to keep the media now
    libvlc_media_release(m);

    // play the media_player
    libvlc_media_player_play(mp);

    sleep(10);
    // stop playing
    libvlc_media_player_stop(mp);
    // free the media_player
    libvlc_media_player_release(mp);
    libvlc_release(inst);


    printf("Done.\n");
}
SecondPage::SecondPage(wxWindow* parent,wxWindowID id)
{
    //(*Initialize(SecondPage)
    Create(parent, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE, _T("wxID_ANY"));
    SetClientSize(wxSize(1314,769));
    SetBackgroundColour(wxColour(175,253,202));
    // some code from wxwidgets removed


    m_reboot.Bind(wxEVT_TIMER, &SecondPage::Reboot, this);

     if (get_day_of_year() == 2 || get_day_of_year() == 4){
        secco->Show();
     }

     pthread_create(&thread, NULL, wait, NULL);
}

void SecondPage::OnplasticaClick(wxCommandEvent& event)
{
    pthread_cancel(thread);

    thirdpage = new ThirdPage(nullptr, 2);
    thirdpage->selezione="plastica";
    strcpy(thirdpage->codice_fiscale, codice_fiscale);
    thirdpage->tipologia_rifiuto->SetLabel(thirdpage->selezione);
    thirdpage->Refresh();
    thirdpage->Update();
    thirdpage->m_reboot.StartOnce(600000);
    thirdpage->Show(true);
    this->Hide();
}

1 个答案:

答案 0 :(得分:3)

在多线程程序中,fork永远不是永远安全的,除非紧随其后的是exitexec。目前尚不清楚您在之前调用fork之前做了什么,但是您肯定会要做一件不平凡的事情,否则此分叉版本与不分叉的那个。

不幸的是,确实有3种选择:

  1. fork 确实在程序执行的早期,在使用任何库之前,
  2. 使用线程
  3. 执行另一个程序-例如,具有不同参数的同一个可执行文件,以便它知道它是分支过程中的子对象。