音频播放库 libZPlay(c++)

C/C++代码 blackfeather

 

最近开发过程中用到了语音播放,查了一下,写一篇总结。

功能很强大的,有一个bass24,不开源但是提供了各个语言的调用接口和例子,因为项目设计不想额外带一个DLL,所以放弃了,于是有了libZPlay这个库。

 

libZPlay库集成了所有支持格式(mp3, mp2, mp1, ogg, flac, ac3, aac, oga, wav and pcm )的编码解码器。库本身是由WINAPI编写,你无须额外的库,也不需要MFC / .NET的支持,只能在Windows下运行。

库直接对声卡播放音乐,简单容易。仅仅3行代码(创建类,打开文件,开始播放),你就可以播放音乐。

使用所支持的编码器,你也同样可以直接从声卡上录制声音(microphone, line-in, CD, ...)和把录制的声音保存到磁盘。

简单,简单,再简单。。。。。。在你的应用程序中只需3行代码便可播放和录制声音。


一段播放mp3的示例:

/* 
* libZPlay example 
* 
* Play test.mp3 to sound card output. 
* 
*/ 
 
#include <windows.h> 
#include <stdio.h> 
#include <conio.h> 
 
#include "libzplay.h" 
 
using namespace libZPlay; 
 
int main(int argc, char **argv) 
{ 
    printf("Playing test.mp3\n\nPress q to end\n\n"); 
 
    // create class instance using class factory. 
    ZPlay *player = CreateZPlay(); 
 
    // open file 
    int result = player->OpenFile("test.mp3", sfAutodetect); 
    if(result == 0) 
    { 
        // display error message 
        printf("Error: %s\n", player->GetError()); 
        player->Release(); 
        return 0; 
    } 
 
    // get song length 
    TStreamInfo info; 
    player->GetStreamInfo(&info); 
 
    printf("Length: %02u:%02u:%02u:%03u\n\n", info.Length.hms.hour,  
                        info.Length.hms.minute, 
                        info.Length.hms.second, 
                        info.Length.hms.millisecond); 
 
    // start playing 
    player->Play(); 
 
    // display position and wait for song end 
    while(1) 
    { 
        // check key press 
        if(kbhit()) 
        { 
            int a = getch(); 
            if(a == 'q' || a == 'Q') 
                break; // end program if Q key is pressed 
        } 
 
        // get stream status to check if song is still playing 
        TStreamStatus status; 
        player->GetStatus(&status);  
        if(status.fPlay == 0) 
            break; // exit checking loop 
 
        // get current position 
        TStreamTime pos; 
        player->GetPosition(&pos); 
        // display position 
        printf("Pos: %02u:%02u:%02u:%03u\r", pos.hms.hour, pos.hms.minute, pos.hms.second, pos.hms.millisecond); 
 
        Sleep(300); // wait 300 ms 
    } 
 
    // destroy class instance 
    player->Release(); 
 
    return 0; 
}


下载链接:http://libzplay.sourceforge.net/

评论列表:

发表评论: