Skip to content

Commit

Permalink
fix(linux): play sound, when initialized (#1332)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustl22 committed Nov 23, 2022
1 parent 782fc9d commit 2ed91fe
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
20 changes: 12 additions & 8 deletions packages/audioplayers_linux/linux/audio_player.cc
Expand Up @@ -60,13 +60,14 @@ void AudioPlayer::SetSourceUrl(std::string url) {
if (_url != url) {
_url = url;
gst_element_set_state(playbin, GST_STATE_NULL);
_isInitialized = false;
_isPlaying = false;
if (!_url.empty()) {
g_object_set(playbin, "uri", _url.c_str(), NULL);
if (playbin->current_state != GST_STATE_READY) {
gst_element_set_state(playbin, GST_STATE_READY);
}
}
_isInitialized = false;
}
}

Expand Down Expand Up @@ -146,7 +147,11 @@ void AudioPlayer::OnMediaStateChange(GstObject *src, GstState *old_state,
if (*new_state >= GST_STATE_READY) {
if (!this->_isInitialized) {
this->_isInitialized = true;
Pause(); // Need to set to pause state, in order to get duration
if (this->_isPlaying) {
Resume();
} else {
Pause(); // Need to set to pause state, in order to get duration
}
}
} else if (this->_isInitialized) {
this->_isInitialized = false;
Expand Down Expand Up @@ -211,8 +216,8 @@ void AudioPlayer::OnPlaybackEnded() {

void AudioPlayer::SetBalance(float balance) {
if (!panorama) {
Logger::Error(std::string("Audiopanorama was not initialized"));
return;
Logger::Error(std::string("Audiopanorama was not initialized"));
return;
}

if (balance > 1.0f) {
Expand Down Expand Up @@ -327,14 +332,12 @@ int64_t AudioPlayer::GetDuration() {
}

void AudioPlayer::Play() {
if (!_isInitialized) {
return;
}
SetPosition(0);
Resume();
}

void AudioPlayer::Pause() {
_isPlaying = false;
GstStateChangeReturn ret = gst_element_set_state(playbin, GST_STATE_PAUSED);
if (ret == GST_STATE_CHANGE_FAILURE) {
Logger::Error(
Expand All @@ -345,6 +348,7 @@ void AudioPlayer::Pause() {
}

void AudioPlayer::Resume() {
_isPlaying = true;
if (!_isInitialized) {
return;
}
Expand All @@ -356,7 +360,7 @@ void AudioPlayer::Resume() {
return;
}
// Update position and duration when start playing, as no event is emitted elsewhere
OnPositionUpdate();
OnPositionUpdate();
OnDurationUpdate();
}

Expand Down
1 change: 1 addition & 0 deletions packages/audioplayers_linux/linux/audio_player.h
Expand Up @@ -59,6 +59,7 @@ class AudioPlayer {
GstBus *bus;

bool _isInitialized = false;
bool _isPlaying = false;
bool _isLooping = false;
bool _isSeekCompleted = true;
double _playbackRate = 1.0;
Expand Down

0 comments on commit 2ed91fe

Please sign in to comment.