Content of file JavaFxMediaPlayer.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>JavaFxMediaPlayer.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 :: CEC :: Navigation Adapter</a> &gt; <a href="../index.html" class="el_bundle">it-tidalwave-bluemarine2-ui-javafx</a> &gt; <a href="index.source.html" class="el_package">it.tidalwave.bluemarine2.ui.audio.renderer.impl.javafx</a> &gt; <span class="el_source">JavaFxMediaPlayer.java</span></div><h1>JavaFxMediaPlayer.java</h1><pre class="source lang-java linenums">/*
 * *********************************************************************************************************************
 *
 * 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 &quot;License&quot;); 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 &quot;AS IS&quot; 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.javafx;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.inject.Inject;
import java.time.Duration;
import java.nio.file.Path;
import javafx.beans.value.ObservableValue;
import javafx.scene.media.Media;
import it.tidalwave.bluemarine2.model.MediaFileSystem;
import it.tidalwave.bluemarine2.model.MediaItem;
import it.tidalwave.bluemarine2.ui.audio.renderer.spi.MediaPlayerSupport;
import lombok.extern.slf4j.Slf4j;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 *
 **********************************************************************************************************************/
<span class="nc" id="L46">@Slf4j</span>
<span class="nc" id="L47">public class JavaFxMediaPlayer extends MediaPlayerSupport</span>
  {
<span class="nc" id="L49">    private static final javafx.util.Duration SKIP_DURATION = javafx.util.Duration.seconds(1);</span>

    @Nullable
    private Media media;

    @Nullable
    private javafx.scene.media.MediaPlayer mediaPlayer;

    @Inject
    private MediaFileSystem fileSystem;

    /*******************************************************************************************************************
     *
     *
     *
     ******************************************************************************************************************/
<span class="nc" id="L65">    private final Runnable cleanup = () -&gt;</span>
      {
<span class="nc" id="L67">        log.debug(&quot;&gt;&gt;&gt;&gt; media reproduction finished&quot;);</span>
        // FIXME: remove listener from currentTimeProperty
<span class="nc" id="L69">        mediaPlayer = null;</span>
<span class="nc" id="L70">        statusProperty.setValue(Status.STOPPED);</span>
<span class="nc" id="L71">      };</span>

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override
    public void setMediaItem (@Nonnull final MediaItem mediaItem)
      throws Exception
      {
<span class="nc" id="L82">        log.info(&quot;setMediaItem({})&quot;, mediaItem);</span>
<span class="nc" id="L83">        checkNotPlaying();</span>
<span class="nc" id="L84">        this.mediaItem = mediaItem;</span>
<span class="nc" id="L85">        final Path path = fileSystem.getRootPath().resolve(mediaItem.getPath()).toAbsolutePath();</span>
<span class="nc" id="L86">        log.debug(&quot;path:     {}&quot;, path);</span>
<span class="nc" id="L87">        log.debug(&quot;metadata: {}&quot;, mediaItem.getMetadata());</span>
<span class="nc" id="L88">        media = new Media(path.toUri().toString());</span>
<span class="nc" id="L89">        statusProperty.set(Status.STOPPED);</span>
<span class="nc" id="L90">        playTimeProperty.set(Duration.ZERO);</span>
<span class="nc" id="L91">      }</span>

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override
    public synchronized void play()
      throws Exception
      {
<span class="nc" id="L102">        log.info(&quot;play()&quot;);</span>
<span class="nc" id="L103">        checkNotPlaying();</span>

<span class="nc bnc" id="L105" title="All 4 branches missed.">        if ((mediaPlayer != null) &amp;&amp; mediaPlayer.getStatus().equals(javafx.scene.media.MediaPlayer.Status.PAUSED))</span>
          {
<span class="nc" id="L107">            mediaPlayer.play();</span>
          }
        else
          {
<span class="nc bnc" id="L111" title="All 2 branches missed.">            if (mediaPlayer != null)</span>
              {
<span class="nc" id="L113">                mediaPlayer.dispose();</span>
              }

<span class="nc" id="L116">            mediaPlayer = new javafx.scene.media.MediaPlayer(media);</span>
            // FIXME: bidirectional bind to an expression?
bidirectional bind to an expression?
<span class="nc" id="L118"> mediaPlayer.currentTimeProperty().addListener(</span> (ObservableValue&lt;? extends javafx.util.Duration&gt; observable, javafx.util.Duration oldValue, javafx.util.Duration newValue) -&gt; <span class="nc" id="L122"> playTimeProperty.setValue(Duration.ofMillis((long)newValue.toMillis())));</span> <span class="nc" id="L124"> mediaPlayer.play();</span> <span class="nc" id="L125"> mediaPlayer.setOnEndOfMedia(cleanup);</span> <span class="nc" id="L126"> mediaPlayer.setOnError(cleanup);</span> <span class="nc" id="L127"> mediaPlayer.setOnHalted(cleanup);</span> } <span class="nc" id="L130"> statusProperty.setValue(Status.PLAYING);</span> <span class="nc" id="L131"> }</span> /******************************************************************************************************************* * * {@inheritDoc} * ******************************************************************************************************************/ @Override public void stop() { <span class="nc" id="L141"> log.info(&quot;stop()&quot;);</span> <span class="nc bnc" id="L143" title="All 2 branches missed."> if (mediaPlayer != null)</span> { <span class="nc" id="L145"> mediaPlayer.stop();</span> <span class="nc" id="L146"> statusProperty.setValue(Status.STOPPED);</span> } <span class="nc" id="L148"> }</span> /******************************************************************************************************************* * * {@inheritDoc} * ******************************************************************************************************************/ @Override public void pause() { <span class="nc" id="L158"> log.info(&quot;pause()&quot;);</span> <span class="nc bnc" id="L160" title="All 2 branches missed."> if (mediaPlayer != null)</span> { <span class="nc" id="L162"> mediaPlayer.pause();</span> <span class="nc" id="L163"> statusProperty.setValue(Status.PAUSED);</span> } <span class="nc" id="L165"> }</span> /******************************************************************************************************************* * * {@inheritDoc} * ******************************************************************************************************************/ @Override public void rewind() { <span class="nc" id="L175"> log.info(&quot;rewind()&quot;);</span> <span class="nc bnc" id="L177" title="All 2 branches missed."> if (mediaPlayer != null)</span> { <span class="nc" id="L179"> mediaPlayer.seek(mediaPlayer.getCurrentTime().subtract(SKIP_DURATION));</span> } <span class="nc" id="L181"> }</span> /******************************************************************************************************************* * * {@inheritDoc} * ******************************************************************************************************************/ @Override public void fastForward() { <span class="nc" id="L191"> log.info(&quot;fastForward()&quot;);</span> <span class="nc bnc" id="L193" title="All 2 branches missed."> if (mediaPlayer != null)</span> { <span class="nc" id="L195"> mediaPlayer.seek(mediaPlayer.getCurrentTime().add(SKIP_DURATION));</span> } <span class="nc" id="L197"> }</span> /******************************************************************************************************************* * * * ******************************************************************************************************************/ private void checkNotPlaying() throws Exception { <span class="nc bnc" id="L207" title="All 4 branches missed."> if ((mediaPlayer != null) &amp;&amp; mediaPlayer.getStatus().equals(javafx.scene.media.MediaPlayer.Status.PLAYING))</span> { <span class="nc" id="L209"> throw new Exception(&quot;Already playing&quot;);</span> } <span class="nc" id="L211"> }</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>