2013-05-15 51 views
0

我有一些歌曲,我想通过代码播放我有vlc播放器安装在Ubuntu中,我想通过vlc播放器播放指定文件夹中的所有歌曲的代码...是有这方面的解决办法吗?任何帮助将appriciated。从C++代码触发vlc播放

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

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

     //trigger vlc 

} 
} 

的文件夹就已经拥有所有必需的歌曲..什么是触发VLC与这些歌曲

+0

[可能的重复](http://stackoverflow.com/questions/6143100/how-do-i-open-a-file-in-its-default-program-linux)。那么,不完全相同,但可能是你的意思(如果他们不使用vlc呢?) – BoBTFish

+0

@BoBTFish:Vlc在这种情况下是特定的 – Rishabh

+0

[''g_app_info_launch'](https://developer.gnome.org/ GIO/2.32/GAppInfo.html#G-APP-信息推出)? – BoBTFish

回答

1

做了一个原型你,其不安全的方式,没有错误检查,但它的工作原理。

代码:

#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将按顺序播放所有列出的文件。

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

+0

我正在使用ubuntu – Rishabh

+0

@Rishabh那么你有什么意见? ['system'](http://linux.die.net/man/3/system)是跨平台的,'dirent'也是。 –

+0

你怎么指定存放文件的文件夹 – Rishabh