求LibMPG123的使用方法。。。
本帖最后由 mm304321141 于 2009-06-20 03:04:27 编辑 最近我写的程序要灵活播放mp3,播放好多不到1秒的小mp3文件。
DirectShow定位不准,有的文件打开就从一半开始播放。。。一共就不足秒,这。。。
但是下载下来了LibMPG123,编辑了dll文件。。。但是现在只是勉强能用来播放mp3而已。。。
问下有没有办法像CDXGraph类一样的现成的类?
// mp3_to_wav.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
static int which = 0;
static BYTE buffer[8192];
static BYTE outbuf[2][1638400];
static HWAVEOUT _ghWaveOut = NULL;
static HANDLE _ghEvent = NULL;
static WAVEHDR WaveHdr[2];
static int playSetup(long rate, int chanel)
{
int err = 0;
WAVEFORMATEX waveform;
waveform.wFormatTag =WAVE_FORMAT_PCM;
waveform.nChannels =chanel;
waveform.nSamplesPerSec =rate;
waveform.nAvgBytesPerSec=rate*chanel;
waveform.nBlockAlign =rate*chanel/8;
waveform.wBitsPerSample =16;
waveform.cbSize =0;
_ghEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
err = waveOutOpen(&_ghWaveOut, WAVE_MAPPER, &waveform, (DWORD)_ghEvent, 0, CALLBACK_EVENT);
memset(WaveHdr, 0, sizeof(WaveHdr));
WaveHdr[1].dwLoops = 1;
WaveHdr[0].dwLoops = 1;
waveOutPrepareHeader(_ghWaveOut, &WaveHdr[1], sizeof(WAVEHDR));
waveOutPrepareHeader(_ghWaveOut, &WaveHdr[0], sizeof(WAVEHDR));
return err;
}
static int playAudio(const BYTE buf[], int len)
{
int err = 0;
LPWAVEHDR pWaveHdr = &WaveHdr[which];
pWaveHdr->lpData = (LPSTR)(LPTSTR)buf ;
pWaveHdr->dwBufferLength = len;
err = waveOutWrite (_ghWaveOut, pWaveHdr, sizeof (WAVEHDR));
which = !which;
pWaveHdr = &WaveHdr[which];
while(pWaveHdr->dwFlags == 0x12){
WaitForSingleObject(_ghEvent, -1);
}
return err;
}
static int decode_file(mpg123_handle *mh, char *path)
{
int n;
int err;
size_t done;
FILE *fp = fopen(path, "rb");
if (fp == NULL){
return -1;
}
while(feof(fp) == 0){
n = fread(buffer, 1, sizeof(buffer), fp);
err = mpg123_decode(mh, buffer, n, outbuf[which], 1638400, &done);
if (err == MPG123_NEW_FORMAT){
long rate;
int channels, enc;
mpg123_getformat(mh, &rate, &channels, &enc);
printf("new format\n");
playSetup(rate, channels);
}
playAudio(outbuf[which], done);
while (err!=MPG123_NEED_MORE && err!=MPG123_ERR){
err = mpg123_decode(mh, NULL, 0, outbuf[which], 1638400, &done);
playAudio(outbuf[which], done);
}
if (err == MPG123_ERR){
printf("err = %s\n", mpg123_strerror(mh));
break;
}
}
waveOutReset(_ghWaveOut);
waveOutClose(_ghWaveOut);
fclose(fp);
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
int err = 0;
mpg123_init();
mpg123_handle *mh = mpg123_new(NULL, &err);
assert(mh != NULL);
err = mpg123_open_feed(mh);
assert(err == MPG123_OK);
char path[] = "300.mp3";
decode_file(mh, path);
mpg123_close(mh);
mpg123_delete(mh);
mpg123_exit();
return 0;
}