Welcome! Log In Create A New Profile

Advanced

Getting an MP3 File to play on a loop

Posted by Arikado 
Getting an MP3 File to play on a loop
January 31, 2009 06:08PM
Basically, when my MP3 file finishes playing I'd like it to restart itself. I've tried a ton of ways to do this, but nothing appears to be working so far. My latest way (posted below) just freezes the program when the MP3 file finishes.

if(!MP3Player_IsPlaying()){
rewind(bgmusic1);
MP3Player_Playfile(bgmusic1, my_reader, NULL);
}

I've thought about reworking the if statement to use fgetpos() or ftell() to see if I've reached the end of the FILE (bgmusic1). But I'm not really 100% sure how to do that.

Thanks in advance to anyone who replies,
Arikado

EDIT: For anyone who didnt know, bgmusic1 is a FILE pointer.



Edited 2 time(s). Last edit at 01/31/2009 09:27PM by Arikado.
Re: Getting an MP3 File to play on a loop
January 31, 2009 10:26PM
Arikado,

What else are you doing with bgmusic1 before that code. It could be a problem there. Also, you don't need to rewind(bgmusic1); if your not reading it before the mp3 function. And don't forget to fclose before a new fopen. Here try this ( not compiled so check for typos).

FILE* bgmusic1 = 0;

s32  my_reader(void *fp,void *dat, s32 size){
	
	return fread(dat, 1, size, fp);
}
void play_mp3()
{

   if(!MP3Player_IsPlaying()) {

      if (bgmusic1) {
          fclose(bgmusic1); // also don't forget to clean up if exiting main
          bgmusic1 = 0;
     }

      bgmusic1 = fopen(path,"rb");
      if (!bgmusic1) return;

      MP3Player_Playfile(bgmusic1, my_reader, NULL);
   }

}



Edited 1 time(s). Last edit at 01/31/2009 10:27PM by scanff.
Re: Getting an MP3 File to play on a loop
February 01, 2009 01:08AM
Hi scanff,

The code you showed me does work. If I call MP3Player_Stop() your code will restart the mp3 file. However, if the mp3 file finishes the app still freezes. I'm really not sure why. However, I'm thinking of writing an if statement that uses ftell() or fgetpos() to determine wether or not the file has finished playing and then call MP3Player_stop() if it has. Like I said in my original post, I've never used fgetpos() or ftell() before so any help with them (from anyone) would be truly apreciated. If you have any other ideas though, I'd love to hear them.



Edited 1 time(s). Last edit at 02/01/2009 01:10AM by Arikado.
Re: Getting an MP3 File to play on a loop
February 01, 2009 06:16AM
Arikado,

I've not had a problem with the buffer function but I've actually never used the MP3Player_Playfile() function. The MP3Player_Stop() does not crash for me when using MP3Player_PlayBuffer(). So here's some code to do what you want using the MP3Player_PlayBuffer(). Basically very little difference.

This also show's you the use of the ftell file function.


char* mp3_buffer = 0;
long mp3_buffer_size = 0;

void  load_mp3(const char* fn)
{
	
	if(mp3_buffer) {
		delete [] mp3_buffer ;
		mp3_buffer  = 0;
	}

	FILE* bgmusic1 = fopen(fn,"rb");
	if (!bgmusic1) return;

	int pos = ftell(bgmusic1); // don't really need this as we probably always want this to be 0 (start of file)
	fseek(bgmusic1, 0, SEEK_END);
	mp3_buffer_size = ftell(bgmusic1);

	mp3_buffer = new char[mp3_buffer_size];

	fread(mp3_buffer,mp3_buffer_size,1,bgmusic1);
	
	fclose(bgmusic1);

}

void play_mp3()
{

	if(!MP3Player_IsPlaying() && mp3_buffer) {
		MP3Player_PlayBuffer(mp3_buffer,mp3_buffer_size,NULL);
   }

}
// EXAMPLE MAIN
int main()
{
	load_mp3("/apps/test/1.mp3");
	while(1) {
	   play_mp3();
	} 

	if (mp3_buffer)
	{
		delete [] mp3_buffer;
		mp3_buffer = 0;
	}
}



Edited 2 time(s). Last edit at 02/01/2009 06:18AM by scanff.
Re: Getting an MP3 File to play on a loop
February 01, 2009 04:28PM
It appears as though calling any FILE related functions (rewind(), fclose(), even fseek() and ftell()) after the mp3 file has finished playing will freeze the app. Not calling any of these functions and simply choosing to call MP3Player_Stop() and then MP3Player_PlayFile() doesnt do anything and the music remains stopped.

scanff, I will take you suggestion and play from a buffer instead of a file on my SD card. Hopefully that will fix my problem. Also, I was wondering what you believe would be the easiest way to convert my mp3 file into a buffer.

EDIT: I switched to using buffers and now everything works perfectly. Thanks scanff :)



Edited 3 time(s). Last edit at 02/01/2009 09:05PM by Arikado.
Re: Getting an MP3 File to play on a loop
February 01, 2009 10:19PM
You should really figure out this issue. It could be a memory leak somewhere else in the program that will cause random crashes elsewhere.

I'm glad you've got it working with the buffer function. Also If you ever wanted to include a buffer without using the file functions you could probably create it as a hex array. I've seen this done with images on a couple of projects here so I'm guessing there is some utility that converts a binary file to a hex array. You'd end up with something like so :-

>> mymp3.h - file

const char my_mp3[] = {0x34,0x56,0xff, 0x66 ........... };

You could just include mymp3.h and use my_mp3[] as the buffer.
Re: Getting an MP3 File to play on a loop
February 01, 2009 10:31PM
Actually scanff, thats exactly how I made my buffer. I converted my mp3 files into a hex array wth bin2o by adding this to my makefile:

#---------------------------------------------------------------------------------
# This rule links in binary data with the .mp3 extension
#---------------------------------------------------------------------------------
%.mp3.o	:	%.mp3
#---------------------------------------------------------------------------------
	@echo $(notdir $<)
	$(bin2o)

-include $(DEPENDS)

Then, like you said, I just #include "mymp3_mp3.h" and called the playbuffer function like so:

MP3Player_PlayBuffer(mymp3_mp3, mymp3_mp3_size);

So I'm no longer using anything related to FILE at all.

Once again, thanks for all your help :)



Edited 1 time(s). Last edit at 02/01/2009 10:32PM by Arikado.
Sorry, only registered users may post in this forum.

Click here to login