Воспроизведение звука в приложении DirectX для Windows 8

Как мне загрузить звуковой файл mp3 и воспроизвести его из моего приложения для Windows 8? Я не могу найти учебник, который поможет мне понять, что я должен делать?

Все, что мне удалось сделать, это:

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
}

Я даже не знаю, правильно ли то, что я сделал …

1

Решение

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()
);
}
0

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector