SDL播放音频。。。
c4droid吧
全部回复
仅看楼主
level 9
有点愤青0 楼主
#include "SDL2/SDL.h"
#include "SDL2/SDL_image.h"
#include "SDL2/SDL_mixer.h"
#include "SDL2/SDL_ttf.h"
#include <string>
#include <sstream>
const int WINDOW_W=900;
const int WINDOW_H=600;
SDL_Window *Win=NULL;
SDL_Renderer *Render=NULL;
SDL_Texture *Background=NULL;
SDL_Texture *Start=NULL;
SDL_Texture *Stop=NULL;
SDL_Texture *Pause=NULL;
SDL_Texture *Last=NULL;
SDL_Texture *Next=NULL;
SDL_Surface *Message=NULL;
SDL_Event event;
TTF_Font *Font=NULL;
Mix_Music *Music1=NULL;
Mix_Music *Music2=NULL;
Mix_Music *Music3=NULL;
/*Mix_Chunk *Chunk1=NULL;
Mix_Chunk *Chunk2=NULL;
Mix_Chunk *Chunk3=NULL;*/
SDL_Color Textcolor={0,0,255};
struct Mouse
{
int x;
int y;
};
class Timer
{
private:
int startTicks;
int pauseTicks;
bool started;
bool paused;
public:
Timer();
void start();
void stop();
void pause();
void unpause();
int get_Ticks();
bool is_started();
bool is_paused();
};
Timer::Timer()
{
startTicks=0;
pauseTicks=0;
started=false;
paused=false;
}
void Timer::start()
{
started=true;
paused=false;
startTicks=SDL_GetTicks();
}
void Timer::stop()
{
started=false;
paused=false;
}
void Timer::pause()
{
if((started==true)&&(paused==false))
{
paused=true;
pauseTicks=SDL_GetTicks()-startTicks;
}
}
void Timer::unpause()
{
if((started==true)&&(paused==true))
{
paused=false;
startTicks=SDL_GetTicks()-pauseTicks;
pauseTicks=0;
}
}
int Timer::get_Ticks()
{
if(started==true)
{
if(paused==true)
return pauseTicks;
else
return SDL_GetTicks()-startTicks;
}
}
bool Timer::is_started()
{
return started;
}
bool Timer::is_paused()
{
return paused;
}
bool Init()
{
SDL_Init(SDL_INIT_EVERYTHING);
if(TTF_Init()==-1)
return false;
if(Mix_OpenAudio(22050,MIX_DEFAULT_FORMAT,2,4096)==-1)
return false;
return true;
}
SDL_Texture *loadimage(std::string filename)
{
SDL_Surface *load=NULL;
SDL_Texture *Tex=NULL;
load=IMG_Load(filename.c_str());
SDL_SetColorKey(load,true,SDL_MapRGB(load->format,0,255,255));
Tex=SDL_CreateTextureFromSurface(Render,load);
SDL_FreeSurface(load);
SDL_RenderClear(Render);
return Tex;
}
void apply_surface(int x, int y,SDL_Texture *source, SDL_Renderer *destilation)
{
SDL_Rect offset;
offset.x=x;
offset.y=y;
SDL_RenderCopy(destilation,source,NULL,&offset);
}
bool load_files()
{
Background=loadimage("bg.png");
Start=loadimage("Start.png");
Pause=loadimage("Pause.png");
Stop=loadimage("Stop.png");
Last=loadimage("Last.png");
Next=loadimage("Next.png");
Font=TTF_OpenFont("lazy.ttf",30);
Music1=Mix_LoadMUS("1.ogg");
Music2=Mix_LoadMUS("2.ogg");
Music3=Mix_LoadMUS("3.ogg");
if(Background==NULL||Font==NULL||Music1==NULL||Music2==NULL||Music3==NULL||Start==NULL||Stop==NULL||Pause==NULL||Last==NULL||Next==NULL)
return false;
return true;
}
void clean_up()
{
SDL_DestroyTexture(Background);
SDL_DestroyRenderer(Render);
SDL_DestroyTexture(Start);
SDL_DestroyTexture(Stop);
SDL_DestroyTexture(Pause);
SDL_DestroyTexture(Last);
SDL_DestroyTexture(Next);
SDL_DestroyWindow(Win);
Mix_FreeMusic(Music1);
Mix_FreeMusic(Music2);
Mix_FreeMusic(Music3);
/*Mix_FreeChunk(Chunk1);
Mix_FreeChunk(Chunk2);
Mix_FreeChunk(Chunk3);*/
TTF_CloseFont(Font);
Mix_CloseAudio();
}
void Fontmessage(std::string font)
{
SDL_Rect rect;
rect.x=(WINDOW_W-Message->w)/2;
rect.y=130;
Message=TTF_RenderUTF8_Solid(Font,font.c_str(),Textcolor);
SDL_Texture *T=NULL;
T=SDL_CreateTextureFromSurface(Render,Message);
SDL_RenderCopy(Render,T,NULL,&rect);
SDL_FreeSurface(Message);
SDL_DestroyTexture(T);
}
int main(int argc, char *argv[])
{
int started=1;//判定是否是暂停或者是播放
int MUSIC=1;
bool quit=false;
if(Init()==false)
return 1;
Win=SDL_CreateWindow("Musicer",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,WINDOW_W,WINDOW_H,SDL_WINDOW_ALLOW_HIGHDPI);
Render=SDL_CreateRenderer(Win,-1,SDL_RENDERER_ACCELERATED);
if(load_files()==false)
printf("%s",SDL_GetError());
return 2;
Mouse mouse;
Timer fps;
Fontmessage("第一次用SDL2写音乐播放器!");
fps.start();
while(quit==false)
{
while(SDL_PollEvent(&event))
{
if(event.type==SDL_MOUSEBUTTONDOWN)
{
if(event.button.button==SDL_BUTTON_LEFT)
{
mouse.x=event.button.x;
mouse.y=event.button.y;
if(mouse.x>400 && mouse.x<500 && mouse.y>300 && mouse.y<400)
{
if(Mix_PlayingMusic()==1)
Mix_HaltMusic();
else if(Mix_PlayingMusic()==0)
{
if(MUSIC==1)
{
Mix_PlayMusic(Music1,-1);
}
else if(MUSIC==2)
{
Mix_PlayMusic(Music2,-1);
}
else if(MUSIC==3)
{
Mix_PlayMusic(Music3,-1);
}
}
}
else if(mouse.x>400 && mouse.x<500 && mouse.y>500 && mouse.y<600)
{
if(Mix_PlayingMusic()==1)
{
if(Mix_PausedMusic()==1)
{
Mix_ResumeMusic();
started=0;
}
else if(Mix_PausedMusic()==0)
{
Mix_PauseMusic();
started=1;
}
}
}
else if(mouse.x>200 && mouse.x<300 && mouse.y>500 && mouse.y<600)
{
MUSIC--;
if(MUSIC<0)
{
MUSIC=3;
}
if(MUSIC==1)
Mix_PlayMusic(Music1,-1);
else if(MUSIC==2)
Mix_PlayMusic(Music2,-1);
else
Mix_PlayMusic(Music3,-1);
}
else if(mouse.x>600 && mouse.x<700 && mouse.y>500 && mouse.y<600)
{
MUSIC++;
if(MUSIC>3)
{
MUSIC=1;
}
if(MUSIC==1)
Mix_PlayMusic(Music1,-1);
else if(MUSIC==2)
Mix_PlayMusic(Music2,-1);
else
Mix_PlayMusic(Music3,-1);
}
}
}
}
if(event.type==SDL_QUIT)
{
quit=true;
}
SDL_Rect R;
R.x=0;
R.y=0;
Background=loadimage("Background.png");
Start=loadimage("Start.png");
Stop=loadimage("Stop.png");
Pause=loadimage("Pause.png");
Last=loadimage("Last.png");
Next=loadimage("Next.png");
apply_surface(R.x,R.y,Background,Render);
apply_surface(400,300,Stop,Render);
apply_surface(200,500,Last,Render);
apply_surface(600,500,Next,Render);
if(started==0)
{
apply_surface(400,500,Pause,Render);
}
else
{
apply_surface(400,500,Start,Render);
}
SDL_RenderPresent(Render);
SDL_RenderClear(Render);
if(fps.get_Ticks()<1000/20)
SDL_Delay(1000/20-fps.get_Ticks());
}
clean_up();
TTF_Quit();
SDL_Quit();
return 0;
}
这个是loadimage()函数错了,但是我找不到原因。。。申明一下,我在电脑上弄的,之前不用绝对路径也能运行,所以不会是路径问题,图片什么都有。。。
2014年07月15日 16点07分 1
level 9
有点愤青0 楼主
2014年07月15日 16点07分 2
level 9
我运行了一下代码,没有问题
2014年07月16日 01点07分 3
我的没有图像,闪退。。。
2014年07月16日 03点07分
level 9
有点愤青0 楼主
自顶。。。。
2014年07月16日 03点07分 4
level 9
有点愤青0 楼主
自顶。。。。
2014年07月16日 03点07分 5
level 9
有点愤青0 楼主
自顶。。。。
2014年07月16日 03点07分 6
level 9
那么长的代码你也好意思。
2014年07月16日 04点07分 7
额。。。。我也不好意思,就是用SDL_GetError给的错误信息是Unknown WAV format [委屈]
2014年07月16日 04点07分
是WAVE,说错了
2014年07月16日 04点07分
level 12
和你说,我播放不了ogg,wav多大你是知道的。。。。。。。。。。。。。。。。。。。。。。。
2014年07月16日 06点07分 8
可是我换了其他十几K的WAV也不能播放。。。
2014年07月16日 08点07分
回复 有点愤青0 :不是大小的问题,是声道多少的问题,去windows里面搜几个提示音来试试。
2014年07月16日 08点07分
我刚才调试了一下,十几K的WAV,电脑上运行窗口时白色的,之后就停止运行了。。。[乖]
2014年07月16日 08点07分
回复 有点愤青0 :用ogg试试[太开心]
2014年07月16日 13点07分
level 8
好长的代码
2014年07月16日 13点07分 9
level 3
c++ 怎么播放音乐?? 那个会实例??
2016年01月02日 21点01分 10
1