Package: JavaFxMediaPlayer
JavaFxMediaPlayer
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
JavaFxMediaPlayer() |
|
|
|
|
|
||||||||||||||||||||
checkNotPlaying() |
|
|
|
|
|
||||||||||||||||||||
fastForward() |
|
|
|
|
|
||||||||||||||||||||
lambda$new$0() |
|
|
|
|
|
||||||||||||||||||||
lambda$play$1(ObservableValue, Duration, Duration) |
|
|
|
|
|
||||||||||||||||||||
pause() |
|
|
|
|
|
||||||||||||||||||||
play() |
|
|
|
|
|
||||||||||||||||||||
rewind() |
|
|
|
|
|
||||||||||||||||||||
setMediaItem(MediaItem) |
|
|
|
|
|
||||||||||||||||||||
static {...} |
|
|
|
|
|
||||||||||||||||||||
stop() |
|
|
|
|
|
Coverage
1: /*
2: * *********************************************************************************************************************
3: *
4: * blueMarine II: Semantic Media Centre
5: * http://tidalwave.it/projects/bluemarine2
6: *
7: * Copyright (C) 2015 - 2021 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *********************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
12: * the License. You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
17: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations under the License.
19: *
20: * *********************************************************************************************************************
21: *
22: * git clone https://bitbucket.org/tidalwave/bluemarine2-src
23: * git clone https://github.com/tidalwave-it/bluemarine2-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.bluemarine2.ui.audio.renderer.impl.javafx;
28:
29: import javax.annotation.Nonnull;
30: import javax.annotation.Nullable;
31: import javax.inject.Inject;
32: import java.time.Duration;
33: import java.nio.file.Path;
34: import javafx.beans.value.ObservableValue;
35: import javafx.scene.media.Media;
36: import it.tidalwave.bluemarine2.model.MediaFileSystem;
37: import it.tidalwave.bluemarine2.model.MediaItem;
38: import it.tidalwave.bluemarine2.ui.audio.renderer.spi.MediaPlayerSupport;
39: import lombok.extern.slf4j.Slf4j;
40:
41: /***********************************************************************************************************************
42: *
43: * @author Fabrizio Giudici
44: *
45: **********************************************************************************************************************/
46: @Slf4j
47: public class JavaFxMediaPlayer extends MediaPlayerSupport
48: {
49: private static final javafx.util.Duration SKIP_DURATION = javafx.util.Duration.seconds(1);
50:
51: @Nullable
52: private Media media;
53:
54: @Nullable
55: private javafx.scene.media.MediaPlayer mediaPlayer;
56:
57: @Inject
58: private MediaFileSystem fileSystem;
59:
60: /*******************************************************************************************************************
61: *
62: *
63: *
64: ******************************************************************************************************************/
65: private final Runnable cleanup = () ->
66: {
67: log.debug(">>>> media reproduction finished");
68: // FIXME: remove listener from currentTimeProperty
69: mediaPlayer = null;
70: statusProperty.setValue(Status.STOPPED);
71: };
72:
73: /*******************************************************************************************************************
74: *
75: * {@inheritDoc}
76: *
77: ******************************************************************************************************************/
78: @Override
79: public void setMediaItem (@Nonnull final MediaItem mediaItem)
80: throws Exception
81: {
82: log.info("setMediaItem({})", mediaItem);
83: checkNotPlaying();
84: this.mediaItem = mediaItem;
85: final Path path = fileSystem.getRootPath().resolve(mediaItem.getPath()).toAbsolutePath();
86: log.debug("path: {}", path);
87: log.debug("metadata: {}", mediaItem.getMetadata());
88: media = new Media(path.toUri().toString());
89: statusProperty.set(Status.STOPPED);
90: playTimeProperty.set(Duration.ZERO);
91: }
92:
93: /*******************************************************************************************************************
94: *
95: * {@inheritDoc}
96: *
97: ******************************************************************************************************************/
98: @Override
99: public synchronized void play()
100: throws Exception
101: {
102: log.info("play()");
103: checkNotPlaying();
104:
105:• if ((mediaPlayer != null) && mediaPlayer.getStatus().equals(javafx.scene.media.MediaPlayer.Status.PAUSED))
106: {
107: mediaPlayer.play();
108: }
109: else
110: {
111:• if (mediaPlayer != null)
112: {
113: mediaPlayer.dispose();
114: }
115:
116: mediaPlayer = new javafx.scene.media.MediaPlayer(media);
117: // FIXME: bidirectional bind to an expression?
118: mediaPlayer.currentTimeProperty().addListener(
119: (ObservableValue<? extends javafx.util.Duration> observable,
120: javafx.util.Duration oldValue,
121: javafx.util.Duration newValue) ->
122: playTimeProperty.setValue(Duration.ofMillis((long)newValue.toMillis())));
123:
124: mediaPlayer.play();
125: mediaPlayer.setOnEndOfMedia(cleanup);
126: mediaPlayer.setOnError(cleanup);
127: mediaPlayer.setOnHalted(cleanup);
128: }
129:
130: statusProperty.setValue(Status.PLAYING);
131: }
132:
133: /*******************************************************************************************************************
134: *
135: * {@inheritDoc}
136: *
137: ******************************************************************************************************************/
138: @Override
139: public void stop()
140: {
141: log.info("stop()");
142:
143:• if (mediaPlayer != null)
144: {
145: mediaPlayer.stop();
146: statusProperty.setValue(Status.STOPPED);
147: }
148: }
149:
150: /*******************************************************************************************************************
151: *
152: * {@inheritDoc}
153: *
154: ******************************************************************************************************************/
155: @Override
156: public void pause()
157: {
158: log.info("pause()");
159:
160:• if (mediaPlayer != null)
161: {
162: mediaPlayer.pause();
163: statusProperty.setValue(Status.PAUSED);
164: }
165: }
166:
167: /*******************************************************************************************************************
168: *
169: * {@inheritDoc}
170: *
171: ******************************************************************************************************************/
172: @Override
173: public void rewind()
174: {
175: log.info("rewind()");
176:
177:• if (mediaPlayer != null)
178: {
179: mediaPlayer.seek(mediaPlayer.getCurrentTime().subtract(SKIP_DURATION));
180: }
181: }
182:
183: /*******************************************************************************************************************
184: *
185: * {@inheritDoc}
186: *
187: ******************************************************************************************************************/
188: @Override
189: public void fastForward()
190: {
191: log.info("fastForward()");
192:
193:• if (mediaPlayer != null)
194: {
195: mediaPlayer.seek(mediaPlayer.getCurrentTime().add(SKIP_DURATION));
196: }
197: }
198:
199: /*******************************************************************************************************************
200: *
201: *
202: *
203: ******************************************************************************************************************/
204: private void checkNotPlaying()
205: throws Exception
206: {
207:• if ((mediaPlayer != null) && mediaPlayer.getStatus().equals(javafx.scene.media.MediaPlayer.Status.PLAYING))
208: {
209: throw new Exception("Already playing");
210: }
211: }
212: }