Skip to content

Commit

Permalink
[voice ]Only call getVolume when necessary (openhab#3280)
Browse files Browse the repository at this point in the history
Signed-off-by: Jimmy Tanagra <jcode@tanagra.id.au>
  • Loading branch information
jimtng committed Dec 29, 2022
1 parent a3dc577 commit ab96a6b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
Expand Up @@ -123,17 +123,18 @@ public void play(@Nullable AudioStream audioStream, @Nullable String sinkId, @Nu
AudioSink sink = getSink(sinkId);
if (sink != null) {
PercentType oldVolume = null;
try {
// get current volume
oldVolume = getVolume(sinkId);
} catch (IOException e) {
logger.debug("An exception occurred while getting the volume of sink '{}' : {}", sink.getId(),
e.getMessage(), e);
}
// set notification sound volume
if (volume != null) {
try {
setVolume(volume, sinkId);
// get current volume
oldVolume = sink.getVolume();
} catch (IOException e) {
logger.debug("An exception occurred while getting the volume of sink '{}' : {}", sink.getId(),
e.getMessage(), e);
}

try {
sink.setVolume(volume);
} catch (IOException e) {
logger.debug("An exception occurred while setting the volume of sink '{}' : {}", sink.getId(),
e.getMessage(), e);
Expand All @@ -147,7 +148,7 @@ public void play(@Nullable AudioStream audioStream, @Nullable String sinkId, @Nu
if (volume != null && oldVolume != null) {
// restore volume only if it was set before
try {
setVolume(oldVolume, sinkId);
sink.setVolume(oldVolume);
} catch (IOException e) {
logger.debug("An exception occurred while setting the volume of sink '{}' : {}", sink.getId(),
e.getMessage(), e);
Expand Down
Expand Up @@ -255,17 +255,18 @@ public void say(String text, @Nullable String voiceId, @Nullable String sinkId,
}

PercentType oldVolume = null;
try {
// get current volume
oldVolume = audioManager.getVolume(sinkId);
} catch (IOException e) {
logger.debug("An exception occurred while getting the volume of sink '{}' : {}", sink.getId(),
e.getMessage(), e);
}
// set notification sound volume
if (volume != null) {
try {
audioManager.setVolume(volume, sinkId);
// get current volume
oldVolume = sink.getVolume();
} catch (IOException e) {
logger.debug("An exception occurred while getting the volume of sink '{}' : {}", sink.getId(),
e.getMessage(), e);
}

try {
sink.setVolume(volume);
} catch (IOException e) {
logger.debug("An exception occurred while setting the volume of sink '{}' : {}", sink.getId(),
e.getMessage(), e);
Expand All @@ -277,7 +278,7 @@ public void say(String text, @Nullable String voiceId, @Nullable String sinkId,
if (volume != null && oldVolume != null) {
// restore volume only if it was set before
try {
audioManager.setVolume(oldVolume, sinkId);
sink.setVolume(oldVolume);
} catch (IOException e) {
logger.debug("An exception occurred while setting the volume of sink '{}' : {}", sink.getId(),
e.getMessage(), e);
Expand Down

0 comments on commit ab96a6b

Please sign in to comment.