[Feature request] IVolumeGettable
Hi!
For some stuff I'm developing, I added this interface. I was wondering if you can add it to the official version (if you consider that can be useful).
Thanks in advance.
New interface:
namespace Ami.BroAudio
{
public interface IVolumeGettable
{
internal float GetVolume();
}
}
Implemented in IAudioPlayer:
public interface IAudioPlayer : IEffectDecoratable, IVolumeGettable, IVolumeSettable, IMusicDecoratable, IAudioStoppable, ISchedulable
{
...
}
In Audio Player:
float IVolumeGettable.GetVolume()
{
return _trackVolume.Current;
}
In EmptyInstance:
float IVolumeGettable.GetVolume() => 0f;
In AudioPlayerInstanceWrapper
float IVolumeGettable.GetVolume() => IsAvailable() ? Instance.GetVolume() : 0f;
And finally in BroAudioChainingMethod (I couldn't find another extension class for Audio Player, I know is not the right place):
/// <inheritdoc cref="GetVolume(IAudioPlayer)"/>
public static float GetVolume(this IAudioPlayer player)
=> player == null ? 0f : player.GetVolume();
Thanks!
If possible, could you describe a bit more about the use case for this feature?
Since the volume is dynamic, accessing this value at different moments may return different results. Plus, aside from trackVolume, there are other volume factors that also affect the final output. My initial thought was that, for most users, this might not provide particularly useful information, and I also wanted to keep the API as simple as possible. That’s why I didn’t include this feature. Hope that makes sense.😊
If you really need to add this method but don’t want it to get overwritten in a future BroAudio update, I’d suggest the following approach:
- In AudioPlayer.Volume.cs, add:
public float CurrentTrackVolume => _trackVolume.Current; - When needed, you can cast
IAudioPlayertoAudioPlayerto accessCurrentTrackVolume. (Note: In the current version, only an implicit conversion exists, which might cause an error. However, I’ll be releasing an update within a day to change it to a safer explicit conversion that won’t throw an error. In the meantime, you can check out AudioPlayerInstanceWrapper.cs in this commit for reference.)
This adjustment only adds a single line to AudioPlayer.Volume.cs, so it should be easier to reapply after an upgrade!
Thanks!
I understand your point.
My use case:
- I play ambiance audio based on the level
- Sometimes the ambiance audio is a combination of two tracks, based on the type of map/level
- Some levels can use the same ambiance audio track (not player), with different volume levels
- Ambiance tracks have long fade-in and fade-out times.
The old logic:
- When the level changes, I call the Stop method for tracks I no longer need.
- For tracks that persist across levels, I adjust their volume using SetVolume (with fade).
The problem:
- If the player moves quickly between levels (e.g., near the map border), sometimes the ambiance sound never starts.
- I suspect that calling .Stop(fadeOutTime) followed by .Play is causing issues when the audio player is in the middle of stopping.
The solution:
- Stop all tracks
- Start only the tracks I need
The new problem:
- That feels unnatural/cheap in the game
The "Improved Solution" Using GetVolume (Not a Fix, But a Smoother Transition):
- Stop all tracks
- Start only the tracks I need, but:
- If the track is new, just play it normally
- If the track is the same but with a different volume:
- If the existing (stopping) audio player's volume is higher than the new volume:
- Play the new audio fast (almost 0 fade-in time).
- If the existing (stopping) audio player's volume is lower than the new volume:
- Play the new audio normally (with its intended fade-in).
- If the existing (stopping) audio player's volume is higher than the new volume:
This approach feels more natural because it smooths the transition when reducing volume.
That’s the clearest and most well-structured description I’ve seen👍really appreciate it!
I understand your use case now. However, for this and the other issues you brought up, I’ll need some time to dive deeper. Hope that doesn’t slow down your project too much.🥺
btw, I just released a new update. You can try the approach I mentioned earlier:
- In AudioPlayer.Volume.cs, add:
public float CurrentTrackVolume => _trackVolume.Current;- When needed, you can cast IAudioPlayer to AudioPlayer to access CurrentTrackVolume.
With this update, you can now safely cast IAudioPlayer using explicit conversion, like this (or any other method you prefer):
AudioPlayer player = BroAudio.Play(_sound) as AudioPlayer;
Debug.Log(player.CurrentTrackVolume);
Hope this helps! Thanks
That’s the clearest and most well-structured description I’ve seen👍really appreciate it!
I understand your use case now. However, for this and the other issues you brought up, I’ll need some time to dive deeper. Hope that doesn’t slow down your project too much.🥺
btw, I just released a new update. You can try the approach I mentioned earlier:
- In AudioPlayer.Volume.cs, add:
public float CurrentTrackVolume => _trackVolume.Current;- When needed, you can cast IAudioPlayer to AudioPlayer to access CurrentTrackVolume.
With this update, you can now safely cast
IAudioPlayerusing explicit conversion, like this (or any other method you prefer):AudioPlayer player = BroAudio.Play(_sound) as AudioPlayer; Debug.Log(player.CurrentTrackVolume);Hope this helps! Thanks
Hi!
Thank you so much for all your help! The solution you suggested looks much simpler and more straightforward than mine.
Also, I don’t have a strict timeline for my project—there’s so much to do that if I get stuck on something, I just move on to the next task. 😆
Really appreciate your support!