Welcome! Log In Create A New Profile

Advanced

Playing MP3 from buffer not file

Posted by scanff 
Playing MP3 from buffer not file
January 27, 2009 08:47AM
Hello,

I've been messing around trying to create an internet radio of sorts but I've been getting a headache trying to play back via the MP3Player_PlayBuffer() function. Everything seems to work but I get no sound. I cannot find a sound control function so is there another way to do this ... here's my code. the "net" class is a class I've written to retrieve data from the network. There is a whole bunch of other junk in there but this is the code to get the data from the network (tested and works) and attempt to play it back. Also when I've added debug code the MP3Player_IsPlaying() does tell me that it's playing but as I said I get no sound :(

       MP3Player_Init();

		
        if (connected)
        {
            len = net->client_recv(buffer,512);
            if(len > 0)
            {
              if (!MP3Player_IsPlaying())  MP3Player_PlayBuffer(buffer,(s32)len,NULL);
            }
        }
Re: Playing MP3 from buffer not file
January 27, 2009 10:49AM
Hi,

call ASND_Init prior to MP3Player_Init. It's using libASND now to allow multiple voices.

regards
shagkur
Re: Playing MP3 from buffer not file
January 27, 2009 09:12PM
Hi shagkur,

Thanks for the quick response. I tried adding the ASND_Init before the MP3Player_Init but I still get no sound. I guess firstly I'm unsure why I need to do this in the first place. I thought that the mp3 functions were under the mad library and nothing to do with the asnd libs. Thanks in advance for any explanation, I'm sure you have a lot more knowledge on the sound libraries than I do.
Re: Playing MP3 from buffer not file
January 28, 2009 01:23AM
Here is some code that I have that is adapted from the mp3 player example on the wiki:

#include   // standard includes mp3player.h, fat.h, etc won't format correctly in the code block

FILE* pFile;
long lSize;
char * buffer;
long result;

char * filename;

int main()
{
        fatInitDefault();
	
	MP3Player_Init();
	
	
	if(fatInitDefault())
	{
		filename = (char *)"sd:/music/song.mp3";
	
		OpenFile(filename);
	}
	while(1)
            {
                if(buffer!=NULL)
		{
			MP3Player_PlayBuffer(buffer,lSize,NULL);
		}
           }
        
        if(buffer!=NULL)
	{
		if(MP3Player_IsPlaying())
		{
			MP3Player_Stop();
			fclose (pFile);
			free (buffer);
		}
	}
        return 0;
}

void OpenFile(char path[50])
{
	pFile = fopen (path, "rb");
	 
	//Check that pFile exist
	if (pFile!=NULL) 
	{   
		// obtain file size:
		fseek (pFile , 0 , SEEK_END);
		lSize = ftell (pFile);
		rewind (pFile);

		// allocate memory to contain the whole file:
		buffer = (char*) malloc (sizeof(char)*lSize);
		if (buffer == NULL) {fputs ("   Memory error",stderr); exit (2);}
		// copy the file into the buffer:
		result = fread (buffer,1,lSize,pFile);
		if (result != lSize) {fputs ("   Reading error",stderr); exit (3);}
		
		fclose (pFile); 
		
	}
	else if (pFile==NULL) 
	{

	fclose (pFile);
	//exit (0);
		
	}
}

I know that you are trying to play an mp3 from a buffer and not a file, but this code loads a file into a buffer to play back the contents. I'm not sure if this will help, but hopefully it does.



Edited 1 time(s). Last edit at 01/28/2009 01:24AM by pcmantinker.
Re: Playing MP3 from buffer not file
January 28, 2009 03:46AM
try taking MP3Player_PlayBuffer(buffer,lSize,NULL); out of the while statement.

place it somewhere else., use this as a example(only one I found) MiniMP3 Player



Edited 1 time(s). Last edit at 01/28/2009 03:47AM by Matando.
Re: Playing MP3 from buffer not file
January 28, 2009 04:45AM
Ah, yeah. lol oops I see how that would be an infinite loop. :P I originally had it in a while loop that terminated when I pressed the home button on a wiimote. Also, the code from the wiki example is a bit outdated and doesn't load the file I select from the list. I just used the code as an example and adapted it to fit my needs. I was experimenting with getting mp3 playback in my wiimote input tester application. I did notice that it takes a while to load a file from the SD card, thus hanging the program a little bit until the file is loaded into memory. I wonder if I put it in a header file, would it load quicker?



Edited 1 time(s). Last edit at 01/28/2009 04:49AM by pcmantinker.
Re: Playing MP3 from buffer not file
January 28, 2009 08:40AM
Awesome thanks for all your help. I've figured it out. It seems my buffer was too small for the mp3 buffer function. I've got a workaround that I'm bashing out. I'll post my project when I have it in a decent state. The project is a Shoutcast client and browser, so you'll be able to browse a play internet radio.

Thanks again.
Sorry, only registered users may post in this forum.

Click here to login