RealtimeStyleTransferRuntime/Plugins/GameSubtitles/Source/Private/Players/MediaSubtitlesPlayer.cpp

78 lines
1.7 KiB
C++
Raw Normal View History

2022-05-23 18:41:30 +00:00
// Copyright Epic Games, Inc. All Rights Reserved.
#include "Players/MediaSubtitlesPlayer.h"
2022-09-13 07:18:28 +00:00
#include "Containers/Array.h"
#include "Containers/UnrealString.h"
#include "MediaPlayer.h"
#include "Misc/Timespan.h"
2022-05-23 18:41:30 +00:00
#include "Overlays.h"
2022-09-13 07:18:28 +00:00
#include "Stats/Stats.h"
2022-05-23 18:41:30 +00:00
#include "SubtitleManager.h"
2022-09-13 07:18:28 +00:00
#include "UObject/WeakObjectPtr.h"
2022-05-23 18:41:30 +00:00
UMediaSubtitlesPlayer::UMediaSubtitlesPlayer(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
, MediaPlayer(nullptr)
, bEnabled(false)
{
}
void UMediaSubtitlesPlayer::BeginDestroy()
{
Stop();
Super::BeginDestroy();
}
void UMediaSubtitlesPlayer::Play()
{
bEnabled = true;
}
void UMediaSubtitlesPlayer::Stop()
{
bEnabled = false;
// Clear the movie subtitle for this object
FSubtitleManager::GetSubtitleManager()->SetMovieSubtitle(this, TArray<FString>());
}
void UMediaSubtitlesPlayer::SetSubtitles(UOverlays* Subtitles)
{
SourceSubtitles = Subtitles;
}
void UMediaSubtitlesPlayer::BindToMediaPlayer(UMediaPlayer* InMediaPlayer)
{
MediaPlayer = InMediaPlayer;
}
void UMediaSubtitlesPlayer::Tick(float DeltaSeconds)
{
QUICK_SCOPE_CYCLE_COUNTER(STAT_UMediaSubtitlesPlayer_Tick);
if (bEnabled && SourceSubtitles)
{
UMediaPlayer* MediaPlayerPtr = MediaPlayer.Get();
if (MediaPlayerPtr)
{
FTimespan CurrentTime = MediaPlayerPtr->GetTime();
TArray<FOverlayItem> CurrentSubtitles;
SourceSubtitles->GetOverlaysForTime(CurrentTime, CurrentSubtitles);
TArray<FString> SubtitlesText;
for (const FOverlayItem& Subtitle : CurrentSubtitles)
{
SubtitlesText.Add(Subtitle.Text);
}
FSubtitleManager::GetSubtitleManager()->SetMovieSubtitle(this, SubtitlesText);
}
else
{
Stop();
}
}
}