diff options
Diffstat (limited to 'scene/2d')
-rw-r--r-- | scene/2d/audio_stream_player_2d.cpp | 17 | ||||
-rw-r--r-- | scene/2d/audio_stream_player_2d.h | 2 |
2 files changed, 14 insertions, 5 deletions
diff --git a/scene/2d/audio_stream_player_2d.cpp b/scene/2d/audio_stream_player_2d.cpp index 7b681eac7a..8c23d33402 100644 --- a/scene/2d/audio_stream_player_2d.cpp +++ b/scene/2d/audio_stream_player_2d.cpp @@ -72,12 +72,10 @@ void AudioStreamPlayer2D::_notification(int p_what) { _update_panning(); } - if (setplay.get() >= 0 && stream.is_valid()) { + if (setplayback.is_valid() && setplay.get() >= 0) { active.set(); - Ref<AudioStreamPlayback> new_playback = stream->instantiate_playback(); - ERR_FAIL_COND_MSG(new_playback.is_null(), "Failed to instantiate playback."); - AudioServer::get_singleton()->start_playback_stream(new_playback, _get_actual_bus(), volume_vector, setplay.get(), pitch_scale); - stream_playbacks.push_back(new_playback); + AudioServer::get_singleton()->start_playback_stream(setplayback, _get_actual_bus(), volume_vector, setplay.get(), pitch_scale); + setplayback.unref(); setplay.set(-1); } @@ -255,7 +253,11 @@ void AudioStreamPlayer2D::play(float p_from_pos) { if (stream->is_monophonic() && is_playing()) { stop(); } + Ref<AudioStreamPlayback> stream_playback = stream->instantiate_playback(); + ERR_FAIL_COND_MSG(stream_playback.is_null(), "Failed to instantiate playback."); + stream_playbacks.push_back(stream_playback); + setplayback = stream_playback; setplay.set(p_from_pos); active.set(); set_physics_process_internal(true); @@ -390,6 +392,10 @@ bool AudioStreamPlayer2D::get_stream_paused() const { return false; } +bool AudioStreamPlayer2D::has_stream_playback() { + return !stream_playbacks.is_empty(); +} + Ref<AudioStreamPlayback> AudioStreamPlayer2D::get_stream_playback() { ERR_FAIL_COND_V_MSG(stream_playbacks.is_empty(), Ref<AudioStreamPlayback>(), "Player is inactive. Call play() before requesting get_stream_playback()."); return stream_playbacks[stream_playbacks.size() - 1]; @@ -458,6 +464,7 @@ void AudioStreamPlayer2D::_bind_methods() { ClassDB::bind_method(D_METHOD("set_panning_strength", "panning_strength"), &AudioStreamPlayer2D::set_panning_strength); ClassDB::bind_method(D_METHOD("get_panning_strength"), &AudioStreamPlayer2D::get_panning_strength); + ClassDB::bind_method(D_METHOD("has_stream_playback"), &AudioStreamPlayer2D::has_stream_playback); ClassDB::bind_method(D_METHOD("get_stream_playback"), &AudioStreamPlayer2D::get_stream_playback); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "stream", PROPERTY_HINT_RESOURCE_TYPE, "AudioStream"), "set_stream", "get_stream"); diff --git a/scene/2d/audio_stream_player_2d.h b/scene/2d/audio_stream_player_2d.h index a5fd584513..79a026fed2 100644 --- a/scene/2d/audio_stream_player_2d.h +++ b/scene/2d/audio_stream_player_2d.h @@ -56,6 +56,7 @@ private: SafeFlag active{ false }; SafeNumeric<float> setplay{ -1.0 }; + Ref<AudioStreamPlayback> setplayback; Vector<AudioFrame> volume_vector; @@ -129,6 +130,7 @@ public: void set_panning_strength(float p_panning_strength); float get_panning_strength() const; + bool has_stream_playback(); Ref<AudioStreamPlayback> get_stream_playback(); AudioStreamPlayer2D(); |