1

I want to accomplish to play an mp3 song with mpg123 in a different thread passing the trackid as argument so I can pick the right song. The code I am using is the code below.

I have two problems. Sometimes the program doesn't create the thread and when it does and the right values are passed and printed the song never plays.

I also tried using the mpg123 library with system() and when the thread is correctly created the song also plays correctly. Am I doing something wrong with the mpg code in the thread? Thank you.

For the compilation of the code:

g++ -g -Wall -pthread mpg.cpp -lmpg123 -lao -lpthread -o mpg

#include <ao/ao.h>
#include <mpg123.h>
#include "pthread.h"
#define BITS 8

#include <iostream>
using namespace std;




int playaudio(int  trakid)
{

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

if (tid==1){
    cout<<tid<<endl;
    traklink="/home/mixa/karavi.mp3";}
   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();
//system("mpg123 -q traklink");


  return 0;
}


void *threading (void *trakid)
{
long tid=(long)trakid;
cout<<"sound plays on:Thread id, "<<tid<<endl;
playaudio(tid);
pthread_exit(NULL);
}

int main()
{
int rc;
long trakid=1;
//const char * trakl="/home/mixa/karavi.mp3";
pthread_t hThread;

rc=pthread_create(&hThread,NULL,threading,(void *)trakid);
cout<<rc<<endl;
//playaudio( trakl);

return 0;
}    

1 Answer 1

1

Your program is exiting from main before you have finished playing the mp3 file. You need to wait before you exit main. You can accomplish this by waiting for your thread to exit, by adding a pthread_join, e.g.

rc=pthread_create(&hThread,NULL,threading,(void *)trakid);
cout<<rc<<endl;
// don't exit until thread has finished
pthread_join(hThread, NULL);

or with a semaphore or by calling sleep.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.