Untitled

mail@pastecode.io avatar
unknown
c_cpp
a year ago
2.5 kB
6
Indexable
Never
// All Rights Reserved

#include "MediaPlayerComponent.h"

#include "MediaPlayer.h"
#include "MediaSoundComponent.h"
#include "MediaSource.h"
#include "StreamMediaSource.h"
#include "vSide/vSide.h"
#include "Sound/SoundClass.h"


UMediaPlayerComponent::UMediaPlayerComponent()
{
	PrimaryComponentTick.bCanEverTick = true;

	MediaSoundComponent = CreateDefaultSubobject<UMediaSoundComponent>(TEXT("MediaSound"));
	MediaPlayer = CreateDefaultSubobject<UMediaPlayer>(TEXT("MediaPlayer"));
	MediaPlayer->OnMediaOpened.AddDynamic(this, &UMediaPlayerComponent::OnStreamOpened);
	MediaPlayer->OnMediaOpenFailed.AddDynamic(this, &UMediaPlayerComponent::OnStreamOpenFailed);
	MediaPlayer->OnMediaClosed.AddDynamic(this, &UMediaPlayerComponent::OnStreamClosed);
	MediaPlayer->OnPlaybackSuspended.AddDynamic(this, &UMediaPlayerComponent::OnStreamPaused);
	MediaPlayer->OnPlaybackResumed.AddDynamic(this, &UMediaPlayerComponent::OnStreamResumed);

#if WITH_EDITOR
	MediaSoundComponent->SetDefaultMediaPlayer(MediaPlayer);
#else
	MediaSoundComponent->SetMediaPlayer(MediaPlayer);
#endif
}

void UMediaPlayerComponent::FadeIn()
{
}

void UMediaPlayerComponent::FadeOut()
{
}

void UMediaPlayerComponent::PlayURL(const FString& URL)
{
	if (!IsValid(MediaSoundComponent->GetMediaPlayer()))
	{
		UE_LOG(LogGame, Error, TEXT("MediaPlayer is invalid"));
		return;
	}

	UStreamMediaSource* MediaSource = NewObject<UStreamMediaSource>();
	MediaSource->StreamUrl = URL;
	if (!MediaSoundComponent->GetMediaPlayer()->OpenSource(MediaSource))
	{
		MediaSource->StreamUrl = "";
		return;
	}
}

void UMediaPlayerComponent::Stop()
{
	if (!IsValid(MediaSoundComponent->GetMediaPlayer()))
	{
		UE_LOG(LogGame, Error, TEXT("MediaPlayer is invalid"));
		return;
	}
	MediaSoundComponent->GetMediaPlayer()->Close();
}


void UMediaPlayerComponent::OnStreamOpened(FString OpenedUrl)
{
	UE_LOG(LogGame, Display, TEXT("OnStreamOpened %s"), *OpenedUrl);
}

void UMediaPlayerComponent::OnStreamOpenFailed(FString FailedUrl)
{
	UE_LOG(LogGame, Error, TEXT("OnStreamOpenFailed %s"), *FailedUrl);
}

void UMediaPlayerComponent::OnStreamClosed()
{
	UE_LOG(LogGame, Display, TEXT("OnStreamClosed"));
}

void UMediaPlayerComponent::OnStreamPaused()
{
	UE_LOG(LogGame, Display, TEXT("OnStreamPaused"));
}

void UMediaPlayerComponent::OnStreamResumed()
{
	UE_LOG(LogGame, Display, TEXT("OnStreamResumed"));
}