Advertisement
Guest User

UE4 play ogg music file from hard disk

a guest
Jun 19th, 2014
815
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.18 KB | None | 0 0
  1. #include "FooActor42.h"
  2. #include "MyFooActor.h"
  3.  
  4. AMyFooActor::AMyFooActor(const class FPostConstructInitializeProperties& PCIP)
  5. : Super(PCIP)
  6. {
  7.     Box = PCIP.CreateDefaultSubobject<UBoxComponent>(this, TEXT("box"));
  8.  
  9.     sw = (USoundWave*)StaticConstructObject(USoundWave::StaticClass(), this, TEXT("MyTestSoundWave"));
  10.     ac = PCIP.CreateDefaultSubobject<UAudioComponent>(this, TEXT("audio component"));
  11.  
  12.     sw->SoundGroup = ESoundGroup::SOUNDGROUP_Music;
  13.     ac->bIsMusic = true;
  14.     ac->bAutoActivate = false;
  15.    
  16.     loaded = FFileHelper::LoadFileToArray(rawFile, TEXT("D:\\my song.ogg"));
  17.  
  18.     Box->bGenerateOverlapEvents = true;
  19.     Box->SetRelativeScale3D(FVector(5, 5, 5));
  20.  
  21.     if (loaded){
  22.         Debug("loaded");
  23.  
  24.         FByteBulkData* bulkData = &sw->CompressedFormatData.GetFormat(TEXT("OGG"));
  25.         bulkData->Lock(LOCK_READ_WRITE);
  26.         FMemory::Memcpy(bulkData->Realloc(rawFile.Num()), rawFile.GetTypedData(), rawFile.Num());
  27.         bulkData->Unlock();
  28.  
  29.         Debug("size: " + FString::FromInt(bulkData->GetBulkDataSize()));
  30.         Debug("sw volume: " + FString::SanitizeFloat(sw->Volume) + " duration: " + FString::SanitizeFloat(sw->GetDuration()));
  31.         Debug("sw MaxConcurrentPlayCount: " + FString::FromInt(sw->MaxConcurrentPlayCount) + " numchannels: " + FString::FromInt(sw->NumChannels));
  32.         //volume 1.0 - duration 0.0 - NumChannels 0
  33.  
  34.         //FIXME need to find a way to set these parameters
  35.         //sw->Duration = 10.0f;
  36.         sw->NumChannels = 2;
  37.         //Debug("sw detailed info: " + sw->GetDetailedInfo());
  38.  
  39.         ac->SetSound(sw);
  40.         //Debug("ac detailed info: " + ac->GetDetailedInfo());
  41.     }
  42.  
  43.     RootComponent = Box;
  44.  
  45.     Box->OnComponentBeginOverlap.AddDynamic(this, &AMyFooActor::TriggerEnter);
  46.     Box->OnComponentEndOverlap.AddDynamic(this, &AMyFooActor::TriggerExit);
  47. }
  48.  
  49. void AMyFooActor::TriggerEnter(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
  50. {
  51.     Debug("trigger enter");
  52.     if (loaded){
  53.         ac->Play();
  54.     }
  55.  
  56. }
  57. void AMyFooActor::TriggerExit(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
  58. {
  59.     ac->Stop();
  60.     Debug("trigger exit");
  61. }
  62.  
  63. void AMyFooActor::Debug(FString msg){
  64.     if (GEngine){
  65.         GEngine->AddOnScreenDebugMessage(-1, 1, FColor::Red, msg);
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement