Added audio playback to the AssetBrowser

This commit is contained in:
penev92
2022-01-05 00:12:11 +02:00
committed by Matthias Mailänder
parent 7a9e0863d6
commit 8b944e9c82
7 changed files with 36 additions and 1 deletions

View File

@@ -27,6 +27,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
readonly string[] allowedExtensions;
readonly string[] allowedSpriteExtensions;
readonly string[] allowedModelExtensions;
readonly string[] allowedAudioExtensions;
readonly string[] allowedVideoExtensions;
readonly IEnumerable<IReadOnlyPackage> acceptablePackages;
readonly string[] palettes;
@@ -48,6 +49,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
IReadOnlyPackage currentPackage;
Sprite[] currentSprites;
IModel currentVoxel;
ISound currentSound;
VideoPlayerWidget player = null;
bool isVideoLoaded = false;
bool isLoadError = false;
@@ -77,7 +79,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
{
ticker.OnTick = () =>
{
if (animateFrames)
if (animateFrames && currentSprites != null)
SelectNextFrame();
};
}
@@ -310,9 +312,11 @@ namespace OpenRA.Mods.Common.Widgets.Logic
var assetBrowserModData = modData.Manifest.Get<AssetBrowser>();
allowedSpriteExtensions = assetBrowserModData.SpriteExtensions;
allowedModelExtensions = assetBrowserModData.ModelExtensions;
allowedAudioExtensions = assetBrowserModData.AudioExtensions;
allowedVideoExtensions = assetBrowserModData.VideoExtensions;
allowedExtensions = allowedSpriteExtensions
.Union(allowedModelExtensions)
.Union(allowedAudioExtensions)
.Union(allowedVideoExtensions)
.ToArray();
@@ -402,6 +406,14 @@ namespace OpenRA.Mods.Common.Widgets.Logic
bool LoadAsset(IReadOnlyPackage package, string filename)
{
if (currentSound != null)
Game.Sound.StopSound(currentSound);
currentSprites = null;
currentFrame = 0;
currentVoxel = null;
currentSound = null;
if (isVideoLoaded)
{
player.Stop();
@@ -456,6 +468,16 @@ namespace OpenRA.Mods.Common.Widgets.Logic
// Just in case we're switching away from a type of asset that forced the music to pause.
UnMuteSounds();
}
else if (allowedAudioExtensions.Contains(fileExtension))
{
// Mute music so it doesn't interfere with current asset.
MuteSounds();
using (var soundStream = Game.ModData.DefaultFileSystem.Open(prefix + filename))
foreach (var modDataSoundLoader in Game.ModData.SoundLoaders)
if (modDataSoundLoader.TryParseSound(soundStream, out var soundFormat))
currentSound = Game.Sound.Play(soundFormat, Game.Sound.SoundVolume);
}
else if (allowedVideoExtensions.Contains(fileExtension))
{
var video = VideoLoader.GetVideo(Game.ModData.DefaultFileSystem.Open(filename), Game.ModData.VideoLoaders);