如何在Windows 8应用程序中加载mp3声音文件并播放?我找不到一个能够帮助我理解它必须做什么的教程?
我迄今为止所做的就是:
Sound.h
#pragma once
#include <xaudio2.h>
class Sound
{
Sound( );
void Initialize();
void Play( wchar_t fileName );
private:
interface IXAudio2* audioEngine;
interface IXAudio2MasteringVoice* masteringVoice;
IXAudio2SourceVoice* sourceVoice;
WAVEFORMATEX* format;
};
Sound.cpp
#include "pch.h"
#include "Sound.h"
Sound::Sound()
{}
void Sound::Initialize()
{
// Create the XAudio2 Engine
UINT32 flags = 0;
XAudio2Create( &audioEngine, flags );
// Create the mastering voice
audioEngine->CreateMasteringVoice(
&masteringVoice,
XAUDIO2_DEFAULT_CHANNELS,
48000
);
//
// Create the source voice
//
audioEngine->CreateSourceVoice(
&sourceVoice,
format,
0,
XAUDIO2_DEFAULT_FREQ_RATIO,
nullptr,
nullptr,
nullptr
);
}
void Sound::Play( wchar_t fileName )
{
// To do:
// Load sound file and play it
}
我甚至不知道我所做的事情是否正确......
答案 0 :(得分:0)
SoundEffect.h
#pragma once
ref class SoundEffect
{
internal:
SoundEffect();
void Initialize(
_In_ IXAudio2* masteringEngine,
_In_ WAVEFORMATEX* sourceFormat,
_In_ Platform::Array<byte>^ soundData
);
void PlaySound(_In_ float volume);
protected private:
bool m_audioAvailable;
IXAudio2SourceVoice* m_sourceVoice;
Platform::Array<byte>^ m_soundData;
};
SoundEffect.cpp
#include "pch.h"
#include "SoundEffect.h"
SoundEffect::SoundEffect():
m_audioAvailable(false)
{
}
//----------------------------------------------------------------------
void SoundEffect::Initialize(
_In_ IXAudio2 *masteringEngine,
_In_ WAVEFORMATEX *sourceFormat,
_In_ Platform::Array<byte>^ soundData)
{
m_soundData = soundData;
if (masteringEngine == nullptr)
{
// Audio is not available so just return.
m_audioAvailable = false;
return;
}
// Create a source voice for this sound effect.
DX::ThrowIfFailed(
masteringEngine->CreateSourceVoice(
&m_sourceVoice,
sourceFormat
)
);
m_audioAvailable = true;
}
//----------------------------------------------------------------------
void SoundEffect::PlaySound(_In_ float volume)
{
XAUDIO2_BUFFER buffer = {0};
if (!m_audioAvailable)
{
// Audio is not available so just return.
return;
}
// Interrupt sound effect if it is currently playing.
DX::ThrowIfFailed(
m_sourceVoice->Stop()
);
DX::ThrowIfFailed(
m_sourceVoice->FlushSourceBuffers()
);
// Queue the memory buffer for playback and start the voice.
buffer.AudioBytes = m_soundData->Length;
buffer.pAudioData = m_soundData->Data;
buffer.Flags = XAUDIO2_END_OF_STREAM;
DX::ThrowIfFailed(
m_sourceVoice->SetVolume(volume)
);
DX::ThrowIfFailed(
m_sourceVoice->SubmitSourceBuffer(&buffer)
);
DX::ThrowIfFailed(
m_sourceVoice->Start()
);
}