Is it possible to load mp3 files?

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
Diablo vt
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 26
Joined: Wed Oct 29, 2008 6:09 pm
Location: London

Is it possible to load mp3 files?

Post 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.
There are 10 types of people in this world; those who understand binary and those who don't.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Is it possible to load mp3 files?

Post 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)
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
Amarant
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 34
Joined: Wed Nov 05, 2008 9:52 am

Re: Is it possible to load mp3 files?

Post 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.
177
User avatar
Diablo vt
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 26
Joined: Wed Oct 29, 2008 6:09 pm
Location: London

Re: Is it possible to load mp3 files?

Post 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:
There are 10 types of people in this world; those who understand binary and those who don't.
User avatar
programmerinprogress
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Wed Oct 29, 2008 7:31 am
Current Project: some crazy stuff, i'll tell soon :-)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++!
Location: The UK
Contact:

Re: Is it possible to load mp3 files?

Post by programmerinprogress »

just out of curiosity, what are you trying to do?

(with the loaded .mp3?)
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D

I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
User avatar
Diablo vt
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 26
Joined: Wed Oct 29, 2008 6:09 pm
Location: London

Re: Is it possible to load mp3 files?

Post 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.
There are 10 types of people in this world; those who understand binary and those who don't.
User avatar
programmerinprogress
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Wed Oct 29, 2008 7:31 am
Current Project: some crazy stuff, i'll tell soon :-)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++!
Location: The UK
Contact:

Re: Is it possible to load mp3 files?

Post 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.
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D

I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
User avatar
Diablo vt
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 26
Joined: Wed Oct 29, 2008 6:09 pm
Location: London

Re: Is it possible to load mp3 files?

Post 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. ;)
There are 10 types of people in this world; those who understand binary and those who don't.
noob
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 10
Joined: Wed Oct 29, 2008 1:41 pm

Re: Is it possible to load mp3 files?

Post by noob »

Its possible to load an mp3 from a CLI based program. Command line mp3 players exist.
User avatar
programmerinprogress
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Wed Oct 29, 2008 7:31 am
Current Project: some crazy stuff, i'll tell soon :-)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++!
Location: The UK
Contact:

Re: Is it possible to load mp3 files?

Post 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.
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D

I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
Post Reply