基于SDL的SDL_mixer库的音乐播放器
来源:互联网 发布:茶叶网络推广 编辑:程序博客网 时间:2024/04/30 10:27
SDL(Simple DirectMedia Layer)是一个自由的跨平台的多媒体开发包,适用于游戏、游戏SDK、演示软件、模拟器、MPEG播放器和其他应用软件。 用途广泛
SDL音频的扩展是通过SDL_mixer来扩展的,从文档可以支持wav,MP3,ogg,fac,midi几种最常见的音频格式,但是SDL_mixer库只提供了wav解码播放函数,所以其他格式需要其他的库来支持。
MP3在SDL_mixer播放可以采用两种库来进行解码工作,分别是smpeg(用c++语言写成),libmad(用c语言写成),两种库在用时安装即可,在此不再累述。
下面是用SDL_mixer自带的wav解码函数写成的音乐播放器,实现功能为:上一曲,下一曲,暂停,继续播放,播放列表,播放的哪一首,退出程序,此程序全部用鼠标点击来完成相应的操作,其中用到的SDL包分别是SDL_image,SDL_ttf,SDL_mixer,在运行此程序时必须安装这些包。
/*此程序名为guo.c*/
运行命令为:gcc guo.c -lSDL_image -lSDL -lSDL_ttf -lSDL_mixer -o guo
第二步:./guo 加上相应的播放音乐的名字
源码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_mixer.h"
#include "SDL/SDL_ttf.h"
int xx,yy,ww,hh;
/*加载播放界面*/
const int SCREEN_WIDTH = 1000;
const int SCREEN_HEIGHT = 651;
const int SCREEN_BPP = 32;
SDL_Surface *background = NULL;
SDL_Surface *screen = NULL;
SDL_Surface *load_image( char filename[20] )
{
SDL_Surface* loadedImage = NULL;
SDL_Surface* optimizedImage = NULL;
loadedImage = SDL_LoadBMP(filename);
if( loadedImage != NULL )
{
optimizedImage = SDL_DisplayFormat( loadedImage );
SDL_FreeSurface( loadedImage );
}
return optimizedImage;
}
void apply_surface( int x, int y, SDL_Surface* picture,SDL_Surface* Screen)
{
SDL_Rect offset;
offset.x = x;
offset.y = y;
SDL_BlitSurface( picture, NULL,Screen, &offset );
}
/*加载播放界面*/
/*字体设置*/
TTF_Font *font = NULL;
SDL_Color textColor = { 0, 0, 0 };
SDL_Surface *message = NULL;
/*字体设置*/
/*鼠标点击*/
SDL_Event event;
SDL_Rect box;
int handle_events();
int handle_events(int x,int y,int w,int h)
{
box.x=x;
box.y=y;
box.w=w;
box.h=h;
int x1 = 0, y1 = 0;
if( event.type == SDL_MOUSEBUTTONDOWN )
{
if( event.button.button == SDL_BUTTON_LEFT )
{
x1 = event.button.x;
y1 = event.button.y;
if( ( x1 > box.x ) && ( x1 < box.x + box.w ) && ( y1 > box.y ) && ( y1 < box.y + box.h ) )
{
printf("按下\n");
return 5;
}
}
}
}
/*鼠标点击*/
/*音乐加载*/
char songlist[50][50];
Mix_Music *music = NULL;
int sum ,playing;
int quit;
int init()
{
if(TTF_Init()==-1)
{
exit(1);
}
font = TTF_OpenFont( "simfang.ttf", 30 );
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
{
exit(1);
}
if( SDL_Init(SDL_INIT_AUDIO)<0)
{
exit(1);
}
screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
if( screen == NULL )
{
exit (1);
}
SDL_WM_SetCaption( "音乐播放器", NULL );
if(font==NULL)
{
exit(1);
}
return 1;
}
int Music_Set()
{
int audio_rate = 44100;//声音频率
Uint16 audio_format = MIX_DEFAULT_FORMAT;//声音格式
int audio_channels = 6;//声道个数
int audio_buffers = 1024;//采样大小
int audio_volume = MIX_MAX_VOLUME;
if(Mix_OpenAudio(audio_rate, audio_format, audio_channels,audio_buffers)<0)//初始化音频函数
{
printf("Couldn't Open Audio: %s\n",SDL_GetError());
return 0;
}
return 1;
}
void PlayMusic(int track)
{
if(track<0 || track >=sum)
{
printf("Input Error\n");
exit(1);
}
if(music != NULL)
Mix_FreeMusic(music);
music = NULL;
music = Mix_LoadMUS(songlist[track]);//加载音乐
if(music == NULL)
{
printf("Couldn't load %s: %s",songlist[track],SDL_GetError());
return ;
}
if(Mix_FadeInMusic(music,-1,2000) == -1)
{
printf("Couldn't Play %s: %s",songlist[track],SDL_GetError());
return ;
}
printf("Now Playing : %s\n",songlist[track]);
playing = track;
}
void PlayNext()//下一首
{
PlayMusic((playing+1)%sum);
}
void PlayPrevious()//上一首
{
PlayMusic((playing-1+sum)%sum);
}
void PauseMusic()
{
if(Mix_PlayingMusic())
{
Mix_PauseMusic();
}
}
void ResumeMusic()
{
if(Mix_PausedMusic())
{
Mix_ResumeMusic();
}
else if(!Mix_PlayingMusic())
{
PlayMusic(playing);
}
}
void ListMusic()
{
int i;
printf("***********SONGLIST*************\n");
for(i=0;i<sum;i++)
{
message = TTF_RenderText_Solid( font, songlist[i], textColor );
apply_surface((SCREEN_WIDTH - message->h )/2,i*30, message, screen );
printf("%d: %s\n",i+1,songlist[i]);
}
printf("********************************\n");
}
void Menu()
{
printf("*************OPERATION***************\n");
printf(" p(暂停) c(继续)\n");
printf(" f(上一首) n(下一首)\n");
printf(" l(列表) s(停止)\n");
printf(" h(帮助) q(退出)\n");
printf("********************************\n");
}
void Control()
{
quit = 0;
PlayMusic(playing);
char cmd[20];
int i;
while(!quit)
{
while(SDL_PollEvent(&event))
{
if(handle_events(755,496, 88,80)==5)
{
PlayNext();//下一首
}
if(handle_events(431,316,89,88)==5)
{
ListMusic();//播放列表
}
if(handle_events(587,323,88,75)==5)
{
PauseMusic();//暂停
}
if(handle_events(761,321,88,75)==5)
{
Mix_HaltMusic();
quit = 1;
//SDL_Quit();//退出
}
if(handle_events(432,498,78,74)==5)
{
PlayPrevious();//上一首
}
if(handle_events(602,515,58,60)==5)
{
ResumeMusic();//播放
}
if(event.type==SDL_QUIT)
{
quit=1;
}
SDL_Flip(screen);
}
}
}
/*音乐加载*/
void FreeSDL()
{
if( music != NULL)
{
Mix_FreeMusic(music);
music = NULL;
}
Mix_CloseAudio();
SDL_FreeSurface( background );
TTF_CloseFont( font );
TTF_Quit();
SDL_Quit();
}
int main(int argc, char *argv[])
{
int i=1;
if(!init() )
{
return 1;
}
background=load_image("background.bmp");
apply_surface(0,0,background,screen);
if(SDL_Flip(screen)==-1)
{
return 1;
}
if(!Music_Set())
{
return 1;
}
for(sum=0;i<argc;i++,sum++)
{
strcpy(songlist[sum],argv[i]);
}
playing=0;
Menu();
Control();
FreeSDL();
return 0;
}
其中还需要用到字体库,字体库任何一种都行,根据自己爱好
还有图片,图片见上传文件,图片是bmp格式,播放界面按钮是自己ps加上的,推荐使用精灵按钮。
如果大家在看本贴时发现什么错误,希望大家提出《有交流、才有进步》
==世界上最可怕的不是比你优秀的人,而是比你优秀的人还在努力==
come from :guoqianqian5812
- 基于SDL的SDL_mixer音乐播放器
- 基于SDL的SDL_mixer库的音乐播放器
- 基于SDL的mp3播放器
- SDL播放wav格式的音乐
- sdl2.0使用SDL_Mixer播放音乐
- 基于QT的音乐播放器
- 基于Android的音乐播放器项目
- 基于Swing的MP3 音乐播放器
- 基于MCI的音乐播放器
- 基于android的音乐播放器
- 基于Service的音乐播放器
- 基于MFC对话框的音乐播放器
- 基于QT的音乐播放器
- 基于Android的音乐播放器
- 基于汇编语言的音乐播放器
- 基于arm的音乐播放器制作
- 基于Service的简易音乐播放器
- 基于WiFidirect的音乐播放器
- 【c++】计时函数
- 关于启动mysql服务1067错误
- 《编程珠玑(第2版)》笔记——将一个n元一维向量向左旋转i个位置(第2章)
- Android平台下驱动的开发及测试框架概述(二)
- toj 2857. Digit Sorting
- 基于SDL的SDL_mixer库的音乐播放器
- error: ‘for’ loop initial declarations are only allowed in
- hdu-1261
- JAVA编译过程
- 回复是对付对付对付多个发个
- POJ 3071 Football(简单 概率DP)
- c++11 Lambda函数与表示式
- spark 读取hadoop 格式的文件
- 拖动条SeekBar的简单使用