Add `stop_callable` to `AudioStreamPlayerInternal`

This commit is contained in:
Kusok 2024-08-11 20:27:15 +08:00
parent 88f3b5f9d5
commit 8c7dd3cb61
5 changed files with 16 additions and 14 deletions

View File

@ -242,7 +242,7 @@ void AudioStreamPlayer2D::seek(float p_seconds) {
void AudioStreamPlayer2D::stop() { void AudioStreamPlayer2D::stop() {
setplay.set(-1); setplay.set(-1);
internal->stop(); internal->stop_basic();
} }
bool AudioStreamPlayer2D::is_playing() const { bool AudioStreamPlayer2D::is_playing() const {
@ -430,7 +430,7 @@ void AudioStreamPlayer2D::_bind_methods() {
} }
AudioStreamPlayer2D::AudioStreamPlayer2D() { AudioStreamPlayer2D::AudioStreamPlayer2D() {
internal = memnew(AudioStreamPlayerInternal(this, callable_mp(this, &AudioStreamPlayer2D::play), true)); internal = memnew(AudioStreamPlayerInternal(this, callable_mp(this, &AudioStreamPlayer2D::play), callable_mp(this, &AudioStreamPlayer2D::stop), true));
cached_global_panning_strength = GLOBAL_GET("audio/general/2d_panning_strength"); cached_global_panning_strength = GLOBAL_GET("audio/general/2d_panning_strength");
set_hide_clip_children(true); set_hide_clip_children(true);
} }

View File

@ -562,7 +562,7 @@ void AudioStreamPlayer3D::seek(float p_seconds) {
void AudioStreamPlayer3D::stop() { void AudioStreamPlayer3D::stop() {
setplay.set(-1); setplay.set(-1);
internal->stop(); internal->stop_basic();
} }
bool AudioStreamPlayer3D::is_playing() const { bool AudioStreamPlayer3D::is_playing() const {
@ -862,7 +862,7 @@ void AudioStreamPlayer3D::_bind_methods() {
} }
AudioStreamPlayer3D::AudioStreamPlayer3D() { AudioStreamPlayer3D::AudioStreamPlayer3D() {
internal = memnew(AudioStreamPlayerInternal(this, callable_mp(this, &AudioStreamPlayer3D::play), true)); internal = memnew(AudioStreamPlayerInternal(this, callable_mp(this, &AudioStreamPlayer3D::play), callable_mp(this, &AudioStreamPlayer3D::stop), true));
velocity_tracker.instantiate(); velocity_tracker.instantiate();
set_disable_scale(true); set_disable_scale(true);
cached_global_panning_strength = GLOBAL_GET("audio/general/3d_panning_strength"); cached_global_panning_strength = GLOBAL_GET("audio/general/3d_panning_strength");

View File

@ -112,7 +112,7 @@ void AudioStreamPlayer::seek(float p_seconds) {
} }
void AudioStreamPlayer::stop() { void AudioStreamPlayer::stop() {
internal->stop(); internal->stop_basic();
} }
bool AudioStreamPlayer::is_playing() const { bool AudioStreamPlayer::is_playing() const {
@ -283,7 +283,7 @@ void AudioStreamPlayer::_bind_methods() {
} }
AudioStreamPlayer::AudioStreamPlayer() { AudioStreamPlayer::AudioStreamPlayer() {
internal = memnew(AudioStreamPlayerInternal(this, callable_mp(this, &AudioStreamPlayer::play), false)); internal = memnew(AudioStreamPlayerInternal(this, callable_mp(this, &AudioStreamPlayer::play), callable_mp(this, &AudioStreamPlayer::stop), false));
} }
AudioStreamPlayer::~AudioStreamPlayer() { AudioStreamPlayer::~AudioStreamPlayer() {

View File

@ -132,7 +132,7 @@ Ref<AudioStreamPlayback> AudioStreamPlayerInternal::play_basic() {
} }
ERR_FAIL_COND_V_MSG(!node->is_inside_tree(), stream_playback, "Playback can only happen when a node is inside the scene tree"); ERR_FAIL_COND_V_MSG(!node->is_inside_tree(), stream_playback, "Playback can only happen when a node is inside the scene tree");
if (stream->is_monophonic() && is_playing()) { if (stream->is_monophonic() && is_playing()) {
stop(); stop_callable.call();
} }
stream_playback = stream->instantiate_playback(); stream_playback = stream->instantiate_playback();
ERR_FAIL_COND_V_MSG(stream_playback.is_null(), stream_playback, "Failed to instantiate playback."); ERR_FAIL_COND_V_MSG(stream_playback.is_null(), stream_playback, "Failed to instantiate playback.");
@ -242,7 +242,7 @@ void AudioStreamPlayerInternal::set_stream(Ref<AudioStream> p_stream) {
if (stream.is_valid()) { if (stream.is_valid()) {
stream->disconnect(SNAME("parameter_list_changed"), callable_mp(this, &AudioStreamPlayerInternal::_update_stream_parameters)); stream->disconnect(SNAME("parameter_list_changed"), callable_mp(this, &AudioStreamPlayerInternal::_update_stream_parameters));
} }
stop(); stop_callable.call();
stream = p_stream; stream = p_stream;
_update_stream_parameters(); _update_stream_parameters();
if (stream.is_valid()) { if (stream.is_valid()) {
@ -253,12 +253,12 @@ void AudioStreamPlayerInternal::set_stream(Ref<AudioStream> p_stream) {
void AudioStreamPlayerInternal::seek(float p_seconds) { void AudioStreamPlayerInternal::seek(float p_seconds) {
if (is_playing()) { if (is_playing()) {
stop(); stop_callable.call();
play_callable.call(p_seconds); play_callable.call(p_seconds);
} }
} }
void AudioStreamPlayerInternal::stop() { void AudioStreamPlayerInternal::stop_basic() {
for (Ref<AudioStreamPlayback> &playback : stream_playbacks) { for (Ref<AudioStreamPlayback> &playback : stream_playbacks) {
AudioServer::get_singleton()->stop_playback_stream(playback); AudioServer::get_singleton()->stop_playback_stream(playback);
} }
@ -289,7 +289,7 @@ void AudioStreamPlayerInternal::set_playing(bool p_enable) {
if (p_enable) { if (p_enable) {
play_callable.call(0.0); play_callable.call(0.0);
} else { } else {
stop(); stop_callable.call();
} }
} }
@ -339,9 +339,10 @@ StringName AudioStreamPlayerInternal::get_bus() const {
return SceneStringName(Master); return SceneStringName(Master);
} }
AudioStreamPlayerInternal::AudioStreamPlayerInternal(Node *p_node, const Callable &p_play_callable, bool p_physical) { AudioStreamPlayerInternal::AudioStreamPlayerInternal(Node *p_node, const Callable &p_play_callable, const Callable &p_stop_callable, bool p_physical) {
node = p_node; node = p_node;
play_callable = p_play_callable; play_callable = p_play_callable;
stop_callable = p_stop_callable;
physical = p_physical; physical = p_physical;
bus = SceneStringName(Master); bus = SceneStringName(Master);

View File

@ -53,6 +53,7 @@ private:
Node *node = nullptr; Node *node = nullptr;
Callable play_callable; Callable play_callable;
Callable stop_callable;
bool physical = false; bool physical = false;
AudioServer::PlaybackType playback_type = AudioServer::PlaybackType::PLAYBACK_TYPE_DEFAULT; AudioServer::PlaybackType playback_type = AudioServer::PlaybackType::PLAYBACK_TYPE_DEFAULT;
@ -94,7 +95,7 @@ public:
Ref<AudioStreamPlayback> play_basic(); Ref<AudioStreamPlayback> play_basic();
void seek(float p_seconds); void seek(float p_seconds);
void stop(); void stop_basic();
bool is_playing() const; bool is_playing() const;
float get_playback_position(); float get_playback_position();
@ -110,7 +111,7 @@ public:
void set_playback_type(AudioServer::PlaybackType p_playback_type); void set_playback_type(AudioServer::PlaybackType p_playback_type);
AudioServer::PlaybackType get_playback_type() const; AudioServer::PlaybackType get_playback_type() const;
AudioStreamPlayerInternal(Node *p_node, const Callable &p_play_callable, bool p_physical); AudioStreamPlayerInternal(Node *p_node, const Callable &p_play_callable, const Callable &p_stop_callable, bool p_physical);
}; };
#endif // AUDIO_STREAM_PLAYER_INTERNAL_H #endif // AUDIO_STREAM_PLAYER_INTERNAL_H