Created
November 9, 2013 19:35
-
-
Save igbedecarats/7388960 to your computer and use it in GitHub Desktop.
AudioPlayer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "StdAfx.h" | |
#include "AudioPlayer.h" | |
#include "Log.h" | |
AudioPlayer::AudioPlayer(void) | |
{ | |
music = NULL; | |
} | |
AudioPlayer::~AudioPlayer(void) | |
{ | |
if(music != NULL) { | |
Mix_FreeMusic(music); | |
} | |
std::map<std::string, Mix_Chunk*>::iterator it = effects.begin(); | |
for(it; it != effects.end(); it++) { | |
if(it->second != NULL) | |
Mix_FreeChunk(it->second); | |
} | |
} | |
void AudioPlayer::load(const std::string& path) { | |
if(music != NULL) { | |
Mix_FreeMusic(music); | |
} | |
music = Mix_LoadMUS( path.c_str()); | |
if(music == NULL) | |
Log::getInstancia()->error("Audio", "Error cargando archivo de musica"); | |
} | |
bool AudioPlayer::isPlaying() { | |
return (Mix_PlayingMusic() != 0); | |
} | |
void AudioPlayer::play() { | |
if( music != NULL ) | |
{ | |
//If there is no music playing | |
if( !isPlaying() ) | |
{ | |
Mix_PlayMusic( music, -1 ); | |
} | |
} | |
} | |
bool AudioPlayer::isPaused() { | |
return Mix_PausedMusic() == 1; | |
} | |
void AudioPlayer::playEffect(const std::string& path) { | |
if(effects.find(path) == effects.end() ) { // no lo tengo | |
effects[path] = Mix_LoadWAV(path.c_str()); | |
} | |
if(effects[path] != NULL) { | |
// se pudo cargar el audio | |
Mix_PlayChannel(-1,effects[path],0); | |
} | |
} | |
void AudioPlayer::pause() { | |
Mix_PauseMusic(); //Pause the music | |
} | |
void AudioPlayer::resume() { | |
//Mix_PlayMusic( music, -1 ); | |
Mix_ResumeMusic(); //Resume the music | |
} | |
void AudioPlayer::setVolumen(int volumen) { | |
Mix_VolumeMusic(volumen); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <SDL\SDL_mixer.h> | |
#include <string> | |
#include <map> | |
class AudioPlayer | |
{ | |
private: | |
Mix_Music *music; | |
std::map<std::string, Mix_Chunk *> effects; | |
public: | |
AudioPlayer(void); | |
~AudioPlayer(void); | |
void load(const std::string& path); | |
bool isPlaying(); | |
bool isPaused(); | |
void play(); | |
void pause(); | |
void resume(); | |
void playEffect(const std::string& path); | |
/** | |
Volumen minimo: 0 | |
Volumen maximo: 128 | |
**/ | |
void setVolumen(int volumen); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int _tmain(int argc, _TCHAR* argv[]) | |
{ | |
//.. | |
this->audioPlayer->load("./sonidos/background_converted.wav"); | |
this->audioPlayer->play(); | |
this->audioPlayer->setVolumen(100); | |
//.. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment