• 首頁
  • 文件
  • 下載
  • 狀態
  • 常見問題
  • 郵件列表
  • 應用程式
  • 外部鏈結
  • 版權
  • 志願者
  • 聯繫

範例程式:播放音訊檔

「CIOS音訊核心」播放音訊檔的方式極為簡單,僅需一行程式。

#include "CiosAudio.hpp"

int main(int argc,char * argv[])
{
  if (argc<2) return 0 ;
  CiosAudio::MediaPlay(argv[1]) ;
  return 1 ;
}


您必須要在編譯的設定上,加上
  • CAUTILITIES
  • FFMPEGLIB

一般是在編譯時加上"-DCAUTILITIES -DFFMPEGLIB"。

這兩個定義。也可以直接在#include "CiosAudio.hpp"之前直接加上

#define CAUTILITIES 1
#define FFMPEGLIB 1
#include "CiosAudio.hpp"


編譯時需要連結的函式庫,每個平台均不相同,請見《一 般編譯須知》。

平行播放

如果您希望播放音訊檔是不會阻塞主程式的程序,您必須使用Thread。您可以自行寫一個線緒程式,或是繼承使用CIOS Audio Core當中附帶的Thread類別。

#include "CiosAudio.hpp"

namespace CiosAudio {

class MyThread : Thread
{
  public:

    int started ;

    explicit MyThread(void) ;
    virtual ~MyThread(void) ;

  protected:

    virtual void run (int Type,ThreadData * Data) ;

};

MyThread::MyThread(void)
  :Thread()
{
  started = 0 ;
}

MyThread::~MyThread(void)
{
}

void MyThread::run(int Type,ThreadData * Data)
{
  if ( 1001 != Type ) return ;
  started = 1 ;
  MediaPlay(Data->Arguments) ;
  started = 0 ;
}

}

int main(int argc,char * argv[])
{
  if (argc<2) return 0 ;
  CiosAudio::MyThread myThread ;
  myThread.start(1001,argv[1]);
  do {
    Timer::Sleep(1000) ;
  } while ( 1 == myThread.started ) ;
  return 1 ;
}

CaPlay當中的範例


int                   at       = -1                                        ;
int deviceId = -1 ; int decodeId = -1 ;
bool                  debug    = false                                     ;
CiosAudio::Debugger * debugger = NULL                                      ;
while ( at < argc )                                                        {
  if ( 0 == strcmp(argv[at],"--output") )                                  {
    at++                                                                   ;
    deviceId = atoi(argv[at])                                              ;
    at++                                                                   ;
  } else
  if ( 0 == strcmp(argv[at],"--decode") )                                  {
    at++                                                                   ;
    decodeId = atoi(argv[at])                                              ;
    at++                                                                   ;
  } else
  if ( 0 == strcmp(argv[at],"--debug" ) )                                  {
    at++                                                                   ;
    debug = true                                                           ;
  } else                                                                   {
    if ( debug && ( NULL == debugger ) )                                   {
      debugger = new CiosAudio::Debugger ( )                               ;
    }                                                                      ;
    CiosAudio::MediaPlay ( argv [ at ] , deviceId , decodeId , debugger )  ;
    at++                                                                   ;
  }                                                                        ;
}                                                                          ;