how to open an mp3 file with c

11,767

I think we cannot open an mp3 file this way , like any regular text file.

fp=fopen("Hello.mp3","rb")

We have to use a codec , to actually play the mp3 file , or atleast use a library which has one.

Do take a look at these places.

How to play MP3 files in C?

SDL Sound Mix Tutorial

Share:
11,767
falkon21
Author by

falkon21

C# Developer

Updated on June 04, 2022

Comments

  • falkon21
    falkon21 almost 2 years

    I'm having some troubles with this "simple" program that im trying to build.

    My goal is to create a C program which executes an mp3 sound file saying "Hello" and runs at Windows startup.

    #include<stdio.h>
    
    main(int argc, char *argv[]){
    
    FILE *fp;
    char s[100];
    int i;
    
    
        if((fp=fopen("Hello.mp3","rb"))==NULL) //Open file and read on binary mode
           { 
             printf("Could not open the file\n");
             exit(1);
        }
    
    fclose(fp);
    
    
     }
    

    I think that to interpret the MP3 encoded data i must use an library but i realy need an help.

    Best regards,

    Ricardo

  • falkon21
    falkon21 about 11 years
    Hum, what if we try to execute a command from the windows shell? Maybe using the ShellExecute(); command? I can't make it work.. I realy just wanna execute the C program and make it open the music.mp3 file..
  • Barath Ravikumar
    Barath Ravikumar about 11 years
    @RicardoCosta although i don't know why you want to do it :), yes you can open the mp3 file through a shell using a command line interface given by any audio player. On my Linux system i have mplayer installed so i have a program like this int main() { system("mplayer mp3file.mp3") } , now after that putting the mp3file.mp3 in the same directory of the program , and running it sure will open the mp3 file and play it
  • falkon21
    falkon21 about 11 years
    ok, but do you use any library for that? i mean, we cant just write: main(){ system("mplayer mp3file.mp3") } and what it run, isn't it? and i just wanna understand how to run any exe from the shell using a C program :P
  • falkon21
    falkon21 about 11 years
    #include<stdlib.h> #include<stdio.h> int main( int argc, const char * argv[] ) { char command[] = "C:\\Users\\Ricardo\\Desktop\\Hello_ricardo.mp3"; int status = system( command ); system("pause"); } Now it's working, thanks! :)