我有一些歌曲想通过代码播放 我在 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/