目前,我正在尝试使用 FMOD 设计一个声音播放器。我想实现的目标只使用一个声音指针,但在多次调用 createStream 时最终内存使用量不断增加。无论如何在不使用多个声音指针的情况下改进它吗?
声音类.h
#ifndef _SOUNDCLASS_H_
#define _SOUNDCLASS_H_
//////////////
// INCLUDES //
//////////////
#include "FMOD/fmod.hpp"
using namespace FMOD;
////////////////////////////////////////////////////////////////////////////////
// Class name: SoundClass
////////////////////////////////////////////////////////////////////////////////
class SoundClass
{
public:
SoundClass();
~SoundClass();
bool Initialize();
float Load( const int& trackNo );
void Play();
void Stop();
void Pause();
void Resume();
void Next( int& trackNo, float& totalTrackLength );
void Previous( int& trackNo, float& totalTrackLength );
private:
bool m_isFirstRun;
bool m_isLoad;
bool m_isPlay;
int m_currentTrackNo;
unsigned int* m_trackLength;
FMOD_RESULT result;
System* fmodSystem;
Channel* fmodChannel;
Sound* track;
};
#endif
声音类.cpp
#include "SoundClass.h"
SoundClass::SoundClass()
{
fmodSystem = 0;
track = 0;
fmodChannel = 0;
m_isFirstRun = true;
m_isLoad = true;
m_isPlay = true;
m_currentTrackNo = 0;
m_trackLength = new unsigned int;
}
SoundClass::~SoundClass()
{
/*
Cleaning up...
*/
track->release();
fmodSystem->release();
}
bool SoundClass::Initialize()
{
/*
Setup FMOD for your application.
*/
// Create a FMOD system.
result = System_Create( &fmodSystem );
// Similar to HRESULT, FMOD use FMOD_RESULT to keep track of the execution result.
if ( result != FMOD_OK )
{
return false;
}
// Alternately, you can check for the availability of the system.
if ( !fmodSystem )
{
return false;
}
// Initialize FMOD.
result = fmodSystem->init( 100, FMOD_INIT_NORMAL, 0 );
if ( result != FMOD_OK )
{
return false;
}
return true;
}
float SoundClass::Load( const int& trackNo )
{
/*
Load a sound file.
*/
// FMOD sound source.
switch ( trackNo )
{
case 0:
fmodSystem->createStream( "resources/audio/01.ogg", FMOD_DEFAULT, 0, &track );
break;
case 1:
fmodSystem->createStream( "resources/audio/02.ogg", FMOD_DEFAULT, 0, &track );
break;
case 2:
fmodSystem->createStream( "resources/audio/03.ogg", FMOD_DEFAULT, 0, &track );
break;
case 3:
fmodSystem->createStream( "resources/audio/04.ogg", FMOD_DEFAULT, 0, &track );
break;
case 4:
fmodSystem->createStream( "resources/audio/05.ogg", FMOD_DEFAULT, 0, &track );
break;
case 5:
fmodSystem->createStream( "resources/audio/06.ogg", FMOD_DEFAULT, 0, &track );
break;
case 6:
fmodSystem->createStream( "resources/audio/07.ogg", FMOD_DEFAULT, 0, &track );
break;
case 7:
fmodSystem->createStream( "resources/audio/08.ogg", FMOD_DEFAULT, 0, &track );
break;
case 8:
fmodSystem->createStream( "resources/audio/09.ogg", FMOD_DEFAULT, 0, &track );
break;
case 9:
fmodSystem->createStream( "resources/audio/10.ogg", FMOD_DEFAULT, 0, &track );
break;
}
m_currentTrackNo = trackNo;
track->getLength( m_trackLength, FMOD_TIMEUNIT_MS );
return (float)*m_trackLength / 1000;
}
void SoundClass::Play( )
{
// Play sound on a channel
fmodSystem->playSound( FMOD_CHANNEL_REUSE, track, false, &fmodChannel );
}
void SoundClass::Stop()
{
fmodChannel->stop();
}
void SoundClass::Pause()
{
fmodChannel->setPaused( true );
}
void SoundClass::Resume()
{
fmodChannel->setPaused( false );
}
void SoundClass::Next( int& trackNo, float& totalTrackLength )
{
if ( m_currentTrackNo != 9 )
{
totalTrackLength = Load( m_currentTrackNo + 1 );
trackNo = m_currentTrackNo;
Play();
}
}
void SoundClass::Previous( int& trackNo, float& totalTrackLength )
{
if ( m_currentTrackNo != 0 )
{
totalTrackLength = Load( m_currentTrackNo - 1 );
trackNo = m_currentTrackNo;
Play();
}
}