Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "CustomMusicPlayer.h"
- #include "MusicPlayerActor.h"
- //#define DEBUG_MUSIC_PLAYER_ACTOR 1
- AMusicPlayerActor::AMusicPlayerActor(const class FPostConstructInitializeProperties& PCIP)
- : Super(PCIP)
- {
- FileName = "My Song.ogg";
- Box_play = PCIP.CreateDefaultSubobject<UBoxComponent>(this, TEXT("box_play"));
- light_play = PCIP.CreateDefaultSubobject<UPointLightComponent>(this, TEXT("light_play"));
- Box_pause = PCIP.CreateDefaultSubobject<UBoxComponent>(this, TEXT("box_pause"));
- light_stop = PCIP.CreateDefaultSubobject<UPointLightComponent>(this, TEXT("light_stop"));
- Box_stop = PCIP.CreateDefaultSubobject<UBoxComponent>(this, TEXT("box_stop"));
- light_pause = PCIP.CreateDefaultSubobject<UPointLightComponent>(this, TEXT("light_pause"));
- RootComponent = Box_play;
- Box_pause->AttachTo(RootComponent);
- Box_stop->AttachTo(RootComponent);
- light_play->Intensity = 10000;
- light_play->SetLightColor(FColor::Green);
- light_play->AttachTo(RootComponent);
- light_stop->Intensity = 10000;
- light_stop->SetLightColor(FColor::Red);
- light_stop->AttachTo(Box_stop);
- light_pause->Intensity = 10000;
- light_pause->SetLightColor(FColor::Yellow);
- light_pause->AttachTo(Box_pause);
- Box_play->bGenerateOverlapEvents = true;
- Box_pause->bGenerateOverlapEvents = true;
- Box_stop->bGenerateOverlapEvents = true;
- Box_stop->AddRelativeLocation(FVector(75, 0, 0));
- Box_pause->AddRelativeLocation(FVector(-75, 0, 0));
- Box_play->SetRelativeScale3D(FVector(5, 5, 5));
- Box_play->OnComponentBeginOverlap.AddDynamic(this, &AMusicPlayerActor::TriggerEnter);
- Box_play->OnComponentEndOverlap.AddDynamic(this, &AMusicPlayerActor::TriggerExit);
- Box_pause->OnComponentBeginOverlap.AddDynamic(this, &AMusicPlayerActor::TriggerEnterPause);
- Box_pause->OnComponentEndOverlap.AddDynamic(this, &AMusicPlayerActor::TriggerExitPause);
- Box_stop->OnComponentBeginOverlap.AddDynamic(this, &AMusicPlayerActor::TriggerEnterStop);
- Box_stop->OnComponentEndOverlap.AddDynamic(this, &AMusicPlayerActor::TriggerExitStop);
- sw = (USoundWave*)StaticConstructObject(USoundWave::StaticClass(), this, TEXT("MyTestSoundWave"));
- ac = PCIP.CreateDefaultSubobject<UAudioComponent>(this, TEXT("audio_component"));
- device = GEngine ? GEngine->GetAudioDevice() : NULL; //gently ask for the audio device
- sw->SoundGroup = ESoundGroup::SOUNDGROUP_Music;
- ac->bIsUISound = true;
- ac->bIsMusic = true;
- ac->bAutoActivate = false;
- loaded = false;
- }
- inline void AMusicPlayerActor::Load(FString NewFileName = "")
- {
- if (NewFileName != "")
- {
- FileName = NewFileName;
- Stop();
- }
- loaded = FFileHelper::LoadFileToArray(rawFile, FileName.GetCharArray().GetTypedData());
- if (loaded){
- Debug("loaded");
- FByteBulkData* bulkData = &sw->CompressedFormatData.GetFormat(TEXT("OGG"));
- bulkData->Lock(LOCK_READ_WRITE);
- FMemory::Memcpy(bulkData->Realloc(rawFile.Num()), rawFile.GetTypedData(), rawFile.Num());
- bulkData->Unlock();
- fillSoundWaveInfo(sw, &rawFile);
- ac->SetSound(sw);
- }
- }
- void AMusicPlayerActor::Play()
- {
- if (!loaded){
- Load();
- }
- if (!loaded){
- return;
- }
- if (isPaused){
- audioSource->Play();
- isPaused = false;
- }
- else
- {
- ac->Play();
- }
- }
- void AMusicPlayerActor::Pause()
- {
- int status = findSource(sw);
- //Debug("Surce location returned " + FString::FromInt(status));
- if (audioSource)
- {
- audioSource->Pause();
- isPaused = true;
- }
- }
- void AMusicPlayerActor::Stop()
- {
- ac->Stop();
- isPaused = false;
- if (audioSource)
- {
- audioSource->Stop();
- }
- }
- void AMusicPlayerActor::TriggerEnter(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
- {
- //Debug("trigger play enter");
- Play();
- }
- void AMusicPlayerActor::TriggerExit(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
- {
- //Debug("trigger play exit");
- }
- void AMusicPlayerActor::TriggerEnterPause(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
- {
- //Debug("trigger pause enter");
- Pause();
- }
- void AMusicPlayerActor::TriggerExitPause(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
- {
- //Debug("trigger pause exit");
- }
- void AMusicPlayerActor::TriggerEnterStop(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
- {
- //Debug("trigger stop enter");
- Stop();
- }
- void AMusicPlayerActor::TriggerExitStop(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
- {
- //Debug("trigger stop exit");
- }
- int AMusicPlayerActor::fillSoundWaveInfo(USoundWave* sw, TArray<uint8>* rawFile)
- {
- FSoundQualityInfo info;
- FVorbisAudioInfo vorbis_obj = FVorbisAudioInfo();
- if (!vorbis_obj.ReadCompressedInfo(rawFile->GetTypedData(), rawFile->Num(), &info))
- {
- //Debug("Can't load header");
- return 1;
- }
- sw->NumChannels = info.NumChannels;
- sw->Duration = info.Duration;
- sw->RawPCMDataSize = info.SampleDataSize;
- sw->SampleRate = info.SampleRate;
- return 0;
- }
- int AMusicPlayerActor::findSource(USoundWave* sw)
- {
- if (!device)
- {
- activeSound = NULL;
- audioSource = NULL;
- return -1;
- }
- TArray<FActiveSound*> tmpActualSounds = device->GetActiveSounds();
- if (tmpActualSounds.Num()){
- for (auto activeSoundIt(tmpActualSounds.CreateIterator()); activeSoundIt; ++activeSoundIt){
- activeSound = *activeSoundIt;
- for (auto WaveInstanceIt(activeSound->WaveInstances.CreateIterator()); WaveInstanceIt; ++WaveInstanceIt){
- sw_instance = WaveInstanceIt.Value();
- if (sw_instance->WaveData->CompressedDataGuid == sw->CompressedDataGuid){
- audioSource = device->WaveInstanceSourceMap.FindRef(sw_instance);
- return 0;
- }
- }
- }
- }
- audioSource = NULL;
- activeSound = NULL;
- return -2;
- }
- void AMusicPlayerActor::Debug(FString msg){
- #ifdef DEBUG_MUSIC_PLAYER_ACTOR
- if (GEngine){
- GEngine->AddOnScreenDebugMessage(-1, 1, FColor::Red, msg);
- }
- #endif
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement