Page 1 of 1

Is it possible to load mp3 files?

Posted: Mon May 25, 2009 1:31 am
by Diablo vt
Hi guys. I have a question. Can you load .mp3 files using the fopen() function? I tried a very simple program out but, the file doesn't seem to load. Anyway, here is my code:

Code: Select all

#include <stdio.h>

int main()
{
    FILE *file1;
    
    file1 = fopen("Death_to_analog.mp3", "r");
    
    if (file1 != NULL) {
              printf ("Loaded Death to Analog\n");
    }
    
    fclose(file1);
    
    getchar();
    return 0;

}
I might be 1. Mad for trying such a thing or 2. Missing something out. Thanks.

Re: Is it possible to load mp3 files?

Posted: Mon May 25, 2009 2:29 am
by MarauderIIC
Should be able to open anything, if its not open in another program already. Check that the file is in the working directory (the dir w/ all your source if running from VS, with the exe otherwise)

Re: Is it possible to load mp3 files?

Posted: Mon May 25, 2009 7:46 am
by Amarant
If MarauderIIC's suggestion doesn't fix your problem and your running the code on an operating system that has case sensitive filenames, make sure that the case of the filename is correct.

Also make sure that you're using the correct mode string.
As stated here (http://www.cplusplus.com/reference/clib ... dio/fopen/) you should add the 'b' character to your mode string to read in binary files.

so

Code: Select all

file1 = fopen("Death_to_analog.mp3", "r");
should be

Code: Select all

file1 = fopen("Death_to_analog.mp3", "rb");
*Note that this most likely isn't causing your problem, but it could cause some unexpected problems when trying to read the file.

Re: Is it possible to load mp3 files?

Posted: Mon May 25, 2009 12:53 pm
by Diablo vt
Nothing works. :( I made sure the .exe file was in the same directory, the program outputs which song was played so, it doesn't return NULL. Read binary mode doesn't work either, as Amarant basically assumed. By the way, I use Dev-C++ as my IDE and I use Windows Vista, please don't bash me lol. Help a noob? :lol:

Re: Is it possible to load mp3 files?

Posted: Mon May 25, 2009 1:11 pm
by programmerinprogress
just out of curiosity, what are you trying to do?

(with the loaded .mp3?)

Re: Is it possible to load mp3 files?

Posted: Mon May 25, 2009 1:58 pm
by Diablo vt
programmerinprogress wrote:just out of curiosity, what are you trying to do?

(with the loaded .mp3?)
I want to load in an .mp3 file, so I can play a song. It's only an experiment, to see if I can get it to do something. I know it's pretty pointless but as I said, it's only an experiment.

Re: Is it possible to load mp3 files?

Posted: Mon May 25, 2009 2:14 pm
by programmerinprogress
well, you know, the C/C++ IO systems treats files as streams, irrespective of their application or type, the idea is that you can get data in and out of your program, but if i'm understanding you correctly, you expect the program to randomly interpret the files as an audio file, and play it accordingly.

AFAIK, that's not how it works, to actually play an mp3 file, you need to use a library that can interpret (understand in other words) the file you're trying to access, and use the data from the streams to output the sounds, simply trying to load in the sound is not enough.

Now, if i've misread the situation, I don't mean to insult your knowledge of the subject, but if you want to play mp3, perhaps you could use one of the many audio libraries out there to accomplish this task (I can't remember if SDL_mixer supports mp3, but SDL_mixer has it's own audio loading functions, and allows you to stop, pause and play chunks of audio)

EDIT: when the term 'open' is used in C/C++, it refers to opening a file for reading or writing in/out streams of data, not actually 'opening' the file as you would an .exe file when you click it, again, if i've misread the situation, I totally apologise.

Re: Is it possible to load mp3 files?

Posted: Tue May 26, 2009 9:49 pm
by Diablo vt
programmerinprogress wrote:well, you know, the C/C++ IO systems treats files as streams, irrespective of their application or type, the idea is that you can get data in and out of your program, but if i'm understanding you correctly, you expect the program to randomly interpret the files as an audio file, and play it accordingly.

AFAIK, that's not how it works, to actually play an mp3 file, you need to use a library that can interpret (understand in other words) the file you're trying to access, and use the data from the streams to output the sounds, simply trying to load in the sound is not enough.

Now, if i've misread the situation, I don't mean to insult your knowledge of the subject, but if you want to play mp3, perhaps you could use one of the many audio libraries out there to accomplish this task (I can't remember if SDL_mixer supports mp3, but SDL_mixer has it's own audio loading functions, and allows you to stop, pause and play chunks of audio)

EDIT: when the term 'open' is used in C/C++, it refers to opening a file for reading or writing in/out streams of data, not actually 'opening' the file as you would an .exe file when you click it, again, if i've misread the situation, I totally apologise.
Cheers man. I guess loading an .mp3 from a CLI based program isn't possible. I'll look more into SDL once I'm feeling more confident, using C. Btw, SDL_mixer does have support for .mp3 files. ;)

Re: Is it possible to load mp3 files?

Posted: Wed May 27, 2009 11:12 pm
by noob
Its possible to load an mp3 from a CLI based program. Command line mp3 players exist.

Re: Is it possible to load mp3 files?

Posted: Thu May 28, 2009 6:18 am
by programmerinprogress
well, you know, it isn't actually the Command-line that opens the file, it's the application you invoke from the command-line that plays the file.

so yeah, if you found and application which takes an mp3 filepath as a command-line argument, you could open the file from the command-line, but to simply use C++ file streams to open the file and just expect it work work is impossible, you need a library that can interpret the data you're trying to stream in, and then toy need to write a program that uses that library.