Content of file DefaultAudioRendererPresentationControl.java.html
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../../jacoco-resources/report.gif" type="image/gif"/><title>DefaultAudioRendererPresentationControl.java</title><link rel="stylesheet" href="../../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../../index.html" class="el_report">blueMarine II :: UI :: JavaFX</a> > <a href="../index.html" class="el_bundle">it-tidalwave-bluemarine2-ui</a> > <a href="index.source.html" class="el_package">it.tidalwave.bluemarine2.ui.audio.renderer.impl</a> > <span class="el_source">DefaultAudioRendererPresentationControl.java</span></div><h1>DefaultAudioRendererPresentationControl.java</h1><pre class="source lang-java linenums"><span class="nc" id="L1">/*</span>
* *********************************************************************************************************************
*
* blueMarine II: Semantic Media Centre
* http://tidalwave.it/projects/bluemarine2
*
* Copyright (C) 2015 - 2021 by Tidalwave s.a.s. (http://tidalwave.it)
*
* *********************************************************************************************************************
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* *********************************************************************************************************************
*
* git clone https://bitbucket.org/tidalwave/bluemarine2-src
* git clone https://github.com/tidalwave-it/bluemarine2-src
*
* *********************************************************************************************************************
*/
package it.tidalwave.bluemarine2.ui.audio.renderer.impl;
import javax.annotation.Nonnull;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import java.time.Duration;
import java.util.stream.Collectors;
import javafx.beans.property.ObjectProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.application.Platform;
import it.tidalwave.util.annotation.VisibleForTesting;
import it.tidalwave.role.ui.UserAction;
import it.tidalwave.messagebus.annotation.ListensTo;
import it.tidalwave.messagebus.annotation.SimpleMessageSubscriber;
import it.tidalwave.bluemarine2.model.MediaItem.Metadata;
import it.tidalwave.bluemarine2.model.PlayList;
import it.tidalwave.bluemarine2.model.audio.AudioFile;
import it.tidalwave.bluemarine2.ui.commons.OnDeactivate;
import it.tidalwave.bluemarine2.ui.commons.RenderAudioFileRequest;
import it.tidalwave.bluemarine2.ui.audio.renderer.AudioRendererPresentation;
import it.tidalwave.bluemarine2.ui.audio.renderer.MediaPlayer;
import it.tidalwave.bluemarine2.ui.audio.renderer.MediaPlayer.Status;
import lombok.extern.slf4j.Slf4j;
import static it.tidalwave.util.PropertyWrapper.wrap;
import static it.tidalwave.role.ui.Displayable._Displayable_;
import static it.tidalwave.bluemarine2.util.Formatters.*;
import static it.tidalwave.bluemarine2.model.MediaItem.Metadata.*;
import static it.tidalwave.bluemarine2.ui.audio.renderer.MediaPlayer.Status.PLAYING;
/***********************************************************************************************************************
*
* The Control of the {@link AudioRendererPresentation}.
*
* @stereotype Control
*
* @author Fabrizio Giudici
*
**********************************************************************************************************************/
<span class="nc" id="L66">@SimpleMessageSubscriber @Slf4j</span>
<span class="nc" id="L67">public class DefaultAudioRendererPresentationControl</span>
{
@Inject
private AudioRendererPresentation presentation;
@Inject
private MediaPlayer mediaPlayer;
<span class="nc" id="L75"> private final AudioRendererPresentation.Properties properties = new AudioRendererPresentation.Properties();</span>
<span class="nc" id="L77"> private Duration duration = Duration.ZERO;</span>
<span class="nc" id="L79"> private PlayList<AudioFile> playList = PlayList.empty();</span>
// Discriminates a forced stop from media player just terminating
private boolean stopped;
<span class="nc" id="L84"> private final UserAction prevAction = UserAction.of(() -> changeTrack(playList.previous().get()));</span>
<span class="nc" id="L86"> private final UserAction nextAction = UserAction.of(() -> changeTrack(playList.next().get()));</span>
<span class="nc" id="L88"> private final UserAction rewindAction = UserAction.of(() -> mediaPlayer.rewind());</span>
<span class="nc" id="L90"> private final UserAction fastForwardAction = UserAction.of(() -> mediaPlayer.fastForward());</span>
<span class="nc" id="L92"> private final UserAction pauseAction = UserAction.of(() -> mediaPlayer.pause());</span>
<span class="nc" id="L94"> private final UserAction playAction = UserAction.of(this::play);</span>
<span class="nc" id="L96"> private final UserAction stopAction = UserAction.of(this::stop);</span>
// FIXME: use expression binding
// e.g. properties.progressProperty().bind(mediaPlayer.playTimeProperty().asDuration().dividedBy/duration));
// FIXME: weak, remove previous listeners
<span class="nc" id="L101"> private final ChangeListener<Duration> l =</span>
(ObservableValue<? extends Duration> observable,
Duration oldValue,
Duration newValue) ->
{
// FIXME: the control shouldn't mess with JavaFX stuff
<span class="nc" id="L107"> Platform.runLater(() -></span>
{
<span class="nc" id="L109"> properties.playTimeProperty().setValue(format(newValue));</span>
<span class="nc" id="L110"> properties.progressProperty().setValue((double)newValue.toMillis() / duration.toMillis());</span>
<span class="nc" id="L111"> });</span>
<span class="nc" id="L112"> };</span>
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@PostConstruct
@VisibleForTesting void initialize()
{
<span class="nc" id="L121"> presentation.bind(properties,</span>
prevAction, rewindAction, stopAction, pauseAction, playAction, fastForwardAction, nextAction);
<span class="nc" id="L123"> }</span>
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@VisibleForTesting void onRenderAudioFileRequest (@ListensTo @Nonnull final RenderAudioFileRequest request)
throws MediaPlayer.Exception
{
<span class="nc" id="L132"> log.info("onRenderAudioFileRequest({})", request);</span>
<span class="nc" id="L134"> playList = request.getPlayList();</span>
<span class="nc" id="L135"> setAudioFile(playList.getCurrentItem().get());</span>
<span class="nc" id="L136"> bindMediaPlayer();</span>
<span class="nc" id="L137"> presentation.showUp(this);</span>
<span class="nc" id="L138"> presentation.focusOnPlayButton();</span>
<span class="nc" id="L139"> }</span>
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@OnDeactivate
@VisibleForTesting OnDeactivate.Result onDeactivate()
throws MediaPlayer.Exception
{
<span class="nc" id="L149"> stop();</span>
<span class="nc" id="L150"> unbindMediaPlayer();</span>
<span class="nc" id="L151"> playList = PlayList.empty();</span>
<span class="nc" id="L152"> return OnDeactivate.Result.PROCEED;</span>
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
private void setAudioFile (@Nonnull final AudioFile audioFile)
throws MediaPlayer.Exception
{
<span class="nc" id="L162"> log.info("setAudioFile({})", audioFile);</span>
<span class="nc" id="L163"> final Metadata metadata = audioFile.getMetadata();</span>
<span class="nc" id="L164"> log.info(">>>> metadata: {}", metadata);</span>
// FIXME: the control shouldn't mess with JavaFX stuff
// FIXME: this performs some (short) queries that are executed in the JavaFX thread
<span class="nc" id="L168"> Platform.runLater(() -></span>
{
<span class="nc" id="L170"> properties.titleProperty().setValue(metadata.get(TITLE).orElse(""));</span>
<span class="nc" id="L171"> properties.artistProperty().setValue(audioFile.findMakers().stream()</span>
<span class="nc" id="L172"> .map(maker -> maker.as(_Displayable_).getDisplayName())</span>
<span class="nc" id="L173"> .collect(Collectors.joining(", ")));</span>
<span class="nc" id="L174"> properties.composerProperty().setValue(audioFile.findComposers().stream()</span>
<span class="nc" id="L175"> .map(composer -> composer.as(_Displayable_).getDisplayName())</span>
<span class="nc" id="L176"> .collect(Collectors.joining(", ")));</span>
<span class="nc" id="L177"> duration = metadata.get(DURATION).orElse(Duration.ZERO);</span>
<span class="nc" id="L178"> properties.durationProperty().setValue(format(duration));</span>
<span class="nc" id="L179"> properties.folderNameProperty().setValue(</span>
<span class="nc" id="L180"> audioFile.getRecord().map(record -> record.as(_Displayable_).getDisplayName()).orElse(""));</span>
<span class="nc" id="L181"> properties.nextTrackProperty().setValue(</span>
<span class="nc bnc" id="L182" title="All 2 branches missed."> ((playList.getSize() == 1) ? "" : String.format("%d / %d", playList.getIndex() + 1, playList.getSize()) +</span>
<span class="nc" id="L183"> playList.peekNext().map(t -> " - Next track: " + t.getMetadata().get(TITLE).orElse("")).orElse("")));</span>
<span class="nc" id="L184"> });</span>
<span class="nc" id="L186"> mediaPlayer.setMediaItem(audioFile);</span>
<span class="nc" id="L187"> }</span>
/*******************************************************************************************************************
*
*
*
******************************************************************************************************************/
private void onMediaPlayerStarted()
{
<span class="nc" id="L196"> log.info("onMediaPlayerStarted()");</span>
// presentation.focusOnStopButton();
<span class="nc" id="L198"> }</span>
/*******************************************************************************************************************
*
*
*
******************************************************************************************************************/
private void onMediaPlayerStopped()
{
<span class="nc" id="L207"> log.info("onMediaPlayerStopped()");</span>
<span class="nc bnc" id="L209" title="All 2 branches missed."> if (!stopped)</span>
{
<span class="nc" id="L211"> presentation.focusOnPlayButton();</span>
}
<span class="nc bnc" id="L214" title="All 4 branches missed."> if (!stopped && playList.hasNext())</span>
{
// FIXME: check whether the disk is not gapless, and eventually pause
| check whether the disk is not gapless, and eventually pause | |
try
{
<span class="nc" id="L219"> setAudioFile(playList.next().get());</span>
<span class="nc" id="L220"> play();</span>
}
<span class="nc" id="L222"> catch (MediaPlayer.Exception e)</span>
{
<span class="nc" id="L224"> log.error("", e);</span>
<span class="nc" id="L225"> }</span>
}
<span class="nc" id="L227"> }</span>
/*******************************************************************************************************************
*
*
*
******************************************************************************************************************/
private void play()
throws MediaPlayer.Exception
{
<span class="nc" id="L237"> stopped = false;</span>
<span class="nc" id="L238"> mediaPlayer.play();</span>
<span class="nc" id="L239"> }</span>
/*******************************************************************************************************************
*
*
*
******************************************************************************************************************/
private void stop()
throws MediaPlayer.Exception
{
<span class="nc" id="L249"> stopped = true;</span>
<span class="nc" id="L250"> mediaPlayer.stop();</span>
<span class="nc" id="L251"> }</span>
/*******************************************************************************************************************
*
*
*
******************************************************************************************************************/
private void changeTrack (@Nonnull final AudioFile audioFile)
throws MediaPlayer.Exception
{
<span class="nc" id="L261"> final boolean wasPlaying = mediaPlayer.statusProperty().get().equals(PLAYING);</span>
<span class="nc bnc" id="L263" title="All 2 branches missed."> if (wasPlaying)</span>
{
<span class="nc" id="L265"> stop();</span>
}
<span class="nc" id="L268"> setAudioFile(audioFile);</span>
<span class="nc bnc" id="L270" title="All 2 branches missed."> if (wasPlaying)</span>
{
<span class="nc" id="L272"> play();</span>
}
<span class="nc" id="L274"> }</span>
/*******************************************************************************************************************
*
* Binds to the {@link MediaPlayer}.
*
******************************************************************************************************************/
@VisibleForTesting void bindMediaPlayer()
{
<span class="nc" id="L283"> log.debug("bindMediaPlayer()");</span>
<span class="nc" id="L284"> final ObjectProperty<Status> status = mediaPlayer.statusProperty();</span>
<span class="nc" id="L285"> wrap(stopAction.enabled()).bind(status.isEqualTo(PLAYING));</span>
<span class="nc" id="L286"> wrap(pauseAction.enabled()).bind(status.isEqualTo(PLAYING));</span>
<span class="nc" id="L287"> wrap(playAction.enabled()).bind(status.isNotEqualTo(PLAYING));</span>
<span class="nc" id="L288"> wrap(prevAction.enabled()).bind(playList.hasPreviousProperty());</span>
<span class="nc" id="L289"> wrap(nextAction.enabled()).bind(playList.hasNextProperty());</span>
<span class="nc" id="L290"> mediaPlayer.playTimeProperty().addListener(l);</span>
<span class="nc" id="L292"> status.addListener((observable, oldValue, newValue) -></span>
{
<span class="nc bnc" id="L294" title="All 3 branches missed."> switch (newValue)</span>
{
case STOPPED:
<span class="nc" id="L297"> onMediaPlayerStopped();</span>
<span class="nc" id="L298"> break;</span>
case PLAYING:
<span class="nc" id="L301"> onMediaPlayerStarted();</span>
break;
}
<span class="nc" id="L304"> });</span>
<span class="nc" id="L305"> }</span>
/*******************************************************************************************************************
*
* Unbinds from the {@link MediaPlayer}.
*
******************************************************************************************************************/
@VisibleForTesting void unbindMediaPlayer()
{
<span class="nc" id="L314"> log.debug("unbindMediaPlayer()");</span>
<span class="nc" id="L315"> wrap(stopAction.enabled()).unbind();</span>
<span class="nc" id="L316"> wrap(pauseAction.enabled()).unbind();</span>
<span class="nc" id="L317"> wrap(playAction.enabled()).unbind();</span>
<span class="nc" id="L318"> wrap(prevAction.enabled()).unbind();</span>
<span class="nc" id="L319"> wrap(nextAction.enabled()).unbind();</span>
<span class="nc" id="L320"> mediaPlayer.playTimeProperty().removeListener(l);</span>
<span class="nc" id="L321"> }</span>
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.7.202105040129</span></div></body></html>