1

如何加载 mp3 声音文件并在我的 Windows 8 应用程序中播放?我找不到可以帮助我理解我必须做什么的教程?

到目前为止,我所做的只是:

声音.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;
};

声音.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
}

我什至不知道我所做的是否正确......

4

1 回答 1

0

音效.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; 
};

音效.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() 
        ); 
} 
于 2014-02-22T07:55:20.283 回答