2014-02-13 106 views
1

我如何着手加载mp3声音文件并从Windows 8应用程序中播放声音文件?我找不到能够帮助我理解我必须做什么的教程?在DirectX中播放声音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 
} 

我甚至不知道我是什么所做的是对的...

回答

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

砂金我的朋友。你能帮我把文件放入soundData变量吗?之后,我认为这是完成 – Jimmyt1988

+1

检查(http://code.msdn.microsoft.com/Basic-Audio-Sample-9a5bb0b7)它比我想象的更复杂。 – Faisal

+1

我得到了这个工作buddos!哟哟。现在我必须播放MP3文件 Jimmyt1988

相关问题