Last active
August 29, 2015 14:06
-
-
Save takamin/864ad484be958277306c to your computer and use it in GitHub Desktop.
PlaySound
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 "Sound.h" | |
int _tmain(int argc, _TCHAR* argv[]) | |
{ | |
Sound chimes("C:\\Windows\\Media\\chimes.wav"); | |
for(int i = 0; i < 5; i++) { | |
chimes.Play(); | |
Sleep(1000); | |
} | |
return 0; | |
} | |
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 "windows.h" | |
#include <string> | |
class Sound | |
{ | |
public: | |
Sound(); | |
Sound(const std::string& filename); | |
~Sound(); | |
bool Load(const std::string& filename); | |
void Play(); | |
private: | |
std::string filename; | |
LPCTSTR sound_buffer; | |
}; |
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 <iostream> | |
#include "Sound.h" | |
Sound::Sound() | |
: sound_buffer(0) | |
{ | |
} | |
Sound::Sound(const std::string& filename) | |
: sound_buffer(0) | |
{ | |
Load(filename); | |
} | |
Sound::~Sound() | |
{ | |
if (sound_buffer != 0) { | |
HeapFree(GetProcessHeap(), 0, (LPVOID)sound_buffer); | |
} | |
} | |
bool Sound::Load(const std::string& filename) | |
{ | |
bool load_success = false; | |
this->filename = filename; | |
HANDLE hfile = CreateFile(filename.c_str(), GENERIC_READ, | |
0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); | |
if (hfile != INVALID_HANDLE_VALUE) { | |
DWORD file_size = GetFileSize(hfile, NULL); | |
if (file_size != INVALID_FILE_SIZE) { | |
sound_buffer = (LPCTSTR)HeapAlloc( | |
GetProcessHeap(), HEAP_ZERO_MEMORY, | |
file_size); | |
if (sound_buffer != NULL) { | |
DWORD read_bytes; | |
BOOL read_success = ReadFile( | |
hfile, (LPVOID)sound_buffer, | |
file_size, &read_bytes, NULL); | |
if (read_success) { | |
load_success = true; | |
} | |
else { | |
HeapFree(GetProcessHeap(), 0, | |
(LPVOID)sound_buffer); | |
sound_buffer = NULL; | |
} | |
} | |
} | |
CloseHandle(hfile); | |
} | |
if (!load_success) { | |
std::cerr << "サウンドファイル読み込み失敗:" | |
<< filename << std::endl; | |
} | |
return load_success; | |
} | |
void Sound::Play() | |
{ | |
if (sound_buffer) { | |
PlaySound(sound_buffer, NULL, | |
SND_ASYNC | SND_MEMORY); | |
} | |
else { | |
std::cerr << filename << "を再生できません。" | |
<< std::endl; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment