2013-05-15 50 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