本文是根据http://blog.csdn.net/pennyshe/article/details/6553888修改的,在此声明!
写一个简单的mp3播放器,去掉了界面。有个问题就是不能自动播放下一曲。
注:需要安装SDL2-2.0.3、SDL2_mixer-2.0.0http://www.libsdl.org/projects/SDL_mixer和smpeghttp://download.csdn.net/detail/zdf511391565/8025611(播放mp3用的)
具体安装方法详见:http://blog.csdn.net/gameboy12615/article/details/5723836
mp3编码器(LAME):http://sourceforge.net/projects/lame/files/lame
gcc -o mp3SDLplay mp3SDLplay.c -L/usr/lib -lSDL2 -lSDL2_mixer
./mp3SDLplay /home/armmlinux/Music/huomiao.mp3 or
./mp3SDLplay /home/armmlinux/Music/*.mp3
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <signal.h>
- #include "SDL2/SDL.h"
- #include "SDL2/SDL_mixer.h"
- char songlist[50][50];
- Mix_Music *music = NULL;
- int sum ,playing;
- int quit;
- int Sdl_init()
- {
- if( SDL_Init(SDL_INIT_AUDIO)<0){
- fprintf(stderr,"Couldn't Initialize SDL: %s/n",SDL_GetError());
- return 0;
- }
- // int audio_rate = 22050;
- int audio_rate = 44100;
- Uint16 audio_format = AUDIO_S16SYS;
- // Uint16 audio_format = AUDIO_S16;
- int audio_channels = 2;
- int audio_buffers = 4096;
- // int audio_buffers = 1024;
- int audio_volume = MIX_MAX_VOLUME;
- char channeltype[][10] = {"","mono","stereo","surround"};
- if(Mix_OpenAudio(audio_rate, audio_format, audio_channels,audio_buffers)<0){
- fprintf(stderr,"Couldn't Open Audio: %s/n",SDL_GetError());
- return 0;
- }
- Mix_QuerySpec(&audio_rate,&audio_format,&audio_channels);
- Mix_VolumeMusic(audio_volume);
- Mix_SetMusicCMD(getenv("MUSIC_CMD"));
- fprintf(stdout,"Open Audio at %d Hz,%d bit,%s (%s),%d bytes buffer/n",audio_rate,audio_format& 0xFF ,channeltype[audio_channels],(audio_format&0x1000)?"BE":"LE",audio_buffers);
- return 1;
- }
- void PlayMusic(int track)
- {
- if(track<0 || track >=sum){
- fprintf(stderr,"Input Error/n");
- return ;
- }
- if(Mix_PlayingMusic()){
- Mix_FadeOutMusic(1000);
- SDL_Delay(1000);
- }
- if(music != NULL)
- Mix_FreeMusic(music);
- music = NULL;
- music = Mix_LoadMUS(songlist[track]);
- if(music == NULL){
- fprintf(stderr,"Couldn't load %s: %s",songlist[track],SDL_GetError());
- return ;
- }
- if(Mix_FadeInMusic(music,-1,2000) == -1){
- fprintf(stderr,"Couldn't Play %s: %s",songlist[track],SDL_GetError());
- return ;
- }
- fprintf(stdout,"Now Playing ♪♫♫♪ %s/n",songlist[track]);
- playing = track;
- }
- void PlayNext()
- {
- PlayMusic((playing+1)%sum);
- }
- void PlayPrevious()
- {
- PlayMusic((playing-1+sum)%sum);
- }
- void SetVolume(int vol)
- {
- if(vol < 0 || vol > MIX_MAX_VOLUME){
- fprintf(stderr,"Volume Value Error.(0 - %d)/n", MIX_MAX_VOLUME); return ;
- }
- Mix_VolumeMusic(vol);
- }
- void PauseMusic()
- {
- if(Mix_PlayingMusic())
- Mix_PauseMusic();
- }
- void ResumeMusic()
- {
- if(Mix_PausedMusic())
- Mix_ResumeMusic();
- else if(!Mix_PlayingMusic())
- PlayMusic(playing);
- }
- void StopMusic()
- {
- if(Mix_PlayingMusic())
- Mix_HaltMusic();
- }
- void ListMusic()
- {
- int i;
- fprintf(stdout,"***********SONGLIST*************/n");
- for(i=0;i<sum;i++)
- fprintf(stdout,"%d: %s/n",i+1,songlist[i]);
- fprintf(stdout,"********************************/n");
- }
- void PrintCliInfo()
- {
- fprintf(stdout,"*************HELP***************\n");
fprintf(stdout," p(pause) c(continue)\n");
fprintf(stdout," f(previous) n(next)\n");
fprintf(stdout," l(list) t#(play#)\n");
fprintf(stdout," s(stop) v#(volume:0~%d)\n",MIX_MAX_VOLUME);
fprintf(stdout," h(help) d#(delete#))\n");
fprintf(stdout," q(quit)\n");
fprintf(stdout,"********************************\n"); - }
- void PrintHelpInfo()
- {
- fprintf(stdout,"Uage:easymp3 [option]... [file]...\n");
fprintf(stdout,"[option]\n");
fprintf(stdout,"--help\tprintf help and quit!\n"); - }
- void Control()
- {
- PlayMusic(playing);
- char cmd[20];
- int i;
- while(!quit){
- fprintf(stdout,"Enter :");
- fscanf(stdin,"%s",cmd);
- switch(*cmd){
- case 'q':
- Mix_HaltMusic();
- quit = 1;
- break;
- case 'n':
- PlayNext();
- break;
- case 'p':
- PauseMusic();
- break;
- case 'c':
- ResumeMusic();
- break;
- case 's':
- StopMusic();
- break;
- case 'f':
- PlayPrevious();
- break;
- case 'h':
- PrintCliInfo();
- break;
- case 'd':
- for(i=atoi(cmd+1);i<sum;i++)
- strcpy(songlist[i-1],songlist[i]);
- sum--;
- break;
- case 't':
- PlayMusic(atoi(cmd+1)-1);
- break;
- case 'l':
- ListMusic();
- break;
- case 'v':
- Mix_VolumeMusic(atoi(cmd+1));
- break;
- default:
- break;
- }
- }
- }
- void FreeSDL()
- {
- if(Mix_PlayingMusic())
- Mix_HaltMusic();
- if( music != NULL){
- Mix_FreeMusic(music);
- music = NULL;
- }
- Mix_CloseAudio();
- SDL_Quit();
- }
- int main(int argc, char *argv[])
- {
- quit = 0;
- int i;
- for(i=1; i<argc && *argv[i] == '-'; i++ ){
- if(!strcmp(argv[i],"--help")){
- PrintHelpInfo();
- return 0;
- }
- }
- if(i>=argc){
- PrintHelpInfo();
- return 0;
- }
- if(!Sdl_init()){
- return 0;
- }
- for(sum=0;i<argc;i++,sum++)
- strcpy(songlist[sum],argv[i]);
- playing = 0;
- PrintCliInfo();
- Control();
- FreeSDL();
- return 0;
- }