c++ - 从 C++ 代码触发 vlc 播放器

标签 c++ ubuntu vlc

我有一些歌曲想通过代码播放 我在 ubuntu 中安装了 vlc 播放器,我希望代码能够通过 vlc 播放器播放指定文件夹中的所有歌曲...是否有解决此问题的方法?我们将不胜感激。

    int main (int argc, const char * argv[])
{   

    char *input=argv[1];
    if(input=="play"){

        //trigger vlc

}
}

该文件夹将已经包含所有需要的歌曲..用这些歌曲触发 vlc 的方法是什么

最佳答案

为您制作了一个原型(prototype),它不安全并且没有错误检查,但它可以工作。

代码:

#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <errno.h>
#include <vector>
#include <string>
#include <iostream>

int scan(std::string dir, std::vector<std::string> &files)
{
    DIR* dr = opendir(dir.c_str());
    struct dirent *drp;

    while ((drp = readdir(dr)) != NULL)
    {
        struct stat s;
        stat((dir + "/" + std::string(drp->d_name)).c_str(), &s);

        if (s.st_mode & S_IFREG)
        {
            files.push_back(std::string(drp->d_name));
        }
    }

    closedir(dr);

    return 0;
}

int main()
{
    std::string dir = ".", cmd = "vlc";
    std::vector<std::string> files, vfiles;

    scan(dir, files);

    for (unsigned int i = 0; i < files.size(); i++)
    {
        if (files[i].substr(files[i].find(".")) == ".mp3")
        {
            vfiles.push_back(std::string(files[i]));
        }
    }

    for (unsigned int i = 0; i < vfiles.size(); i++)
    {
        cmd += " " + dir + "/" + vfiles[i];
    }

    printf("%s\n", cmd.c_str());
    system(cmd.c_str());

    return 0;
}

输出:

vlc ./test.mp3 ./test2.mp3

它的作用是:它列出指定文件夹 "." 中的所有文件,默认情况下,它检查该文件实际上是文件而不是文件夹,然后列出所有文件以 .mp3" 结尾的文件,然后运行 ​​vlc file1.mp3 file2.mp3 file4.mp3。 VLC 将按顺序播放所有列出的文件。

将 (VLC media player 2.0.6 Twoflower) 添加到 Path 后在 Windows 8 上使用。

关于c++ - 从 C++ 代码触发 vlc 播放器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16568640/

相关文章:

python - 没有文件名的文件的md5sum分配给python中的变量

android - Windows 上的 Ubuntu 上的 Bash,编译 AOSP

windows - VLC 保存流到文件

c++ - 调试断言失败!表达式 : is_block_type_valid(header->_block_use). 对象不会初始化和推送错误

python - 如何在 Python 中捕获系统挂起事件?

python - fatal error C1083 : Cannot open include file: 'Carbon/Carbon.h' : No such file or directory

c++ - 有人在 Mac 上使用过 libvlc 吗?

c++ - 正确使用和实现单例

c++ - 有人可以告诉我任何不同的方法来使这段代码更快吗?

c++ - 使用 unix 终端运行 c++ 程序