Code: play.cpp #include <mpg123.h> #include <ao.h> #include <malloc.h> #include <stdlib.h> #include <windows.h> #define BITS 8 int main(int argc, char *argv[]) { mpg123_handle *mh; unsigned char *buffer; size_t buffer_size; size_t done; int err, driver, channels, encoding;
ao_device *dev; ao_sample_format format; long rate; ao_initialize(); driver = ao_default_driver_id(); // ========= inisial library ============= mpg123_init(); mh = mpg123_new(NULL, &err); buffer_size = mpg123_outblock(mh); buffer = (unsigned char*)malloc(buffer_size * sizeof(unsigned char)); mpg123_param(mh, MPG123_RESYNC_LIMIT, -1, 0); mpg123_open(mh, argv[1]); mpg123_getformat(mh, &rate, &channels, &encoding); // ************************************ format.bits = mpg123_encsize(encoding) * BITS; format.rate = rate; format.channels = channels; format.byte_format = AO_FMT_NATIVE; format.matrix = 0; dev = ao_open_live(driver, &format, NULL); while (mpg123_read(mh, buffer, buffer_size, &done) == MPG123_OK) { ao_play(dev, reinterpret_cast<char*>(buffer), done); fprintf(stderr, "Frame: %li\n", mpg123_tellframe(mh)); } //===========reinterpret_cast<char*>(buffer) : digunakan untuk mengkonversikan type buffer unsigned char menjadi char. /* clean up */ free(buffer); ao_close(dev); mpg123_close(mh); mpg123_delete(mh); mpg123_exit(); ao_shutdown(); return 0; }code dikompilasi dengan mingw gcc:
gcc -O2 -o play play.c -lmpg123 -lao
dapat pula menggunakan code::block dengan menset linker: libmpg123.a libao.a pada project option dan tentu juga opsi search directory-nya.jangan lupa download library libao dan libmpg123.
hasil kompilasi adalah play.exe. dapat digunakan dari console command dengan syntax: play.exe namafile.mp3 ----- enter.
Tidak ada komentar:
Posting Komentar