-3

我是 C++ 编程的新手,我基本上是在尝试使用 mpg123 制作一个简单的 mp3 播放器,代码在 main() 中运行良好,但是当我尝试在 sub 中调用它并提交路径+文件来播放时,我就是不能让它工作。

gcc 抱怨 const char... 和 char 转换...“1.c:71:18: 错误:从 'char' 到 'const char*' [-fpermissive] 的无效转换”

#include <ao/ao.h>
#include <mpg123.h>

#define BITS 8

#include <iostream>
using namespace std;

void playaudio(const char * trakl)
{

//works when called here including path
//const char *traklink="/home/pi/downloads/00000050.mp3";
const char * traklink=trakl;



   mpg123_handle *mh;
    unsigned char *buffer;
    size_t buffer_size;
    size_t done;
    int err;

    int driver;
    ao_device *dev;

    ao_sample_format format;
    int channels, encoding;
    long rate;

   /* if(argc < 2)
        exit(0);
*/
    /* initializations */
    ao_initialize();
    driver = ao_default_driver_id();
    mpg123_init();
    mh = mpg123_new(NULL, &err);
    buffer_size = mpg123_outblock(mh);
    buffer = (unsigned char*) malloc(buffer_size * sizeof(unsigned char));

    /* open the file and get the decoding format */
    //mpg123_open(mh,traklink);
    mpg123_open(mh,traklink);
     mpg123_getformat(mh, &rate, &channels, &encoding);

    /* set the output format and open the output device */
   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);

    /* decode and play */
    while (mpg123_read(mh, buffer, buffer_size, &done) == MPG123_OK)
        //ao_play(dev, buffer, done);
        ao_play(dev, (char*)buffer, done);
    /* clean up */
    free(buffer);
    ao_close(dev);
    mpg123_close(mh);
    mpg123_delete(mh);
    mpg123_exit();
    ao_shutdown();

//  return 0;
}

int main()
{
const char * trakl="/home/pi/downloads/00000050.mp3";

playaudio(* trakl);

return 0;
}    
4

1 回答 1

0

你这样打电话playaudio

playaudio(* trakl);

这是无效的。trak1is of type const char *,这意味着*trak1is of type const charplayaudio期望 a const char *,你应该这样称呼它:

playaudio(trakl);
于 2013-10-26T07:40:35.903 回答