Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(linux): play sound, when initialized #1332

Merged
merged 1 commit into from Nov 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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