-
Notifications
You must be signed in to change notification settings - Fork 68
/
MediaPlayer.scala
386 lines (331 loc) · 11.8 KB
/
MediaPlayer.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
* Copyright (c) 2011-2018, ScalaFX Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the ScalaFX Project nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE SCALAFX PROJECT OR ITS CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package scalafx.scene.media
import javafx.scene.{media => jfxsm}
import javafx.{event => jfxe, util => jfxu}
import scalafx.Includes._
import scalafx.beans.property._
import scalafx.delegate.{SFXDelegate, SFXEnumDelegate, SFXEnumDelegateCompanion}
import scalafx.util.Duration
import scala.language.implicitConversions
object MediaPlayer {
implicit def sfxMediaPlayer2jfx(mp: MediaPlayer): jfxsm.MediaPlayer = if (mp != null) mp.delegate else null
object Status
extends SFXEnumDelegateCompanion[jfxsm.MediaPlayer.Status, Status] {
/**
* State of the player after dispose() method is invoked.
*
* This state indicates player is disposed, all resources are free and player SHOULD NOT be used again.
* Media and MediaView objects associated with disposed player can be reused.
*/
case object Disposed extends Status(jfxsm.MediaPlayer.Status.DISPOSED)
@deprecated ("Use Disposed; DISPOSED will be removed in a future release", "8.0.60-R10")
val DISPOSED = Disposed
/**
* State of the player when a critical error has occurred.
*/
case object Halted extends Status(jfxsm.MediaPlayer.Status.HALTED)
@deprecated ("Use Halted; HALTED will be removed in a future release", "8.0.60-R10")
val HALTED = Halted
/**
* State of the player when playback is paused.
*/
case object Paused extends Status(jfxsm.MediaPlayer.Status.PAUSED)
@deprecated ("Use Paused; PAUSED will be removed in a future release", "8.0.60-R10")
val PAUSED = Paused
/**
* State of the player when it is currently playing.
*/
case object Playing extends Status(jfxsm.MediaPlayer.Status.PLAYING)
@deprecated ("Use Playing; PLAYING will be removed in a future release", "8.0.60-R10")
val PLAYING = Playing
/**
* State of the player once it is prepared to play.
*/
case object Ready extends Status(jfxsm.MediaPlayer.Status.READY)
@deprecated ("Use Ready; READY will be removed in a future release", "8.0.60-R10")
val READY = Ready
/**
* State of the player when data coming into the buffer has slowed or stopped and the playback buffer does not
* have enough data to continue playing.
*/
case object Stalled extends Status(jfxsm.MediaPlayer.Status.STALLED)
@deprecated ("Use Stalled; STALLED will be removed in a future release", "8.0.60-R10")
val STALLED = Stalled
/**
* State of the player when playback has stopped.
*/
case object Stopped extends Status(jfxsm.MediaPlayer.Status.STOPPED)
@deprecated ("Use Stopped; STOPPED will be removed in a future release", "8.0.60-R10")
val STOPPED = Stopped
/**
* State of the player immediately after creation.
*/
case object Unknown extends Status(jfxsm.MediaPlayer.Status.UNKNOWN)
@deprecated ("Use Unknown; UNKNOWN will be removed in a future release", "8.0.60-R10")
val UNKNOWN = Unknown
protected override def unsortedValues: Array[Status] = Array(Disposed, Halted, Paused, Playing,
Ready, Stalled, Stopped, Unknown)
}
/**
* Wraps [[http://docs.oracle.com/javase/8/javafx/api/javafx/scene/input/MediaPlayer.Status.html]]
*/
sealed abstract class Status(override val delegate: jfxsm.MediaPlayer.Status)
extends SFXEnumDelegate[jfxsm.MediaPlayer.Status]
/**
* A value representing an effectively infinite number of playback cycles.
*/
val Indefinite: Int = jfxsm.MediaPlayer.INDEFINITE
private def runnable(op: => Unit): Runnable = new Runnable {
def run() {
op
}
}
}
class MediaPlayer(override val delegate: jfxsm.MediaPlayer) extends SFXDelegate[jfxsm.MediaPlayer] {
/**
* Create a player for a specific media.
*/
def this(media: Media) = this(new jfxsm.MediaPlayer(media))
/**
* The interval between spectrum updates in seconds.
*/
def audioSpectrumInterval: DoubleProperty = delegate.audioSpectrumIntervalProperty
def audioSpectrumInterval_=(v: Double) {
audioSpectrumInterval() = v
}
/**
* A listener for audio spectrum updates.
*/
def audioSpectrumListener: ObjectProperty[jfxsm.AudioSpectrumListener] = delegate.audioSpectrumListenerProperty
def audioSpectrumListener_=(v: jfxsm.AudioSpectrumListener) {
audioSpectrumListener() = v
}
/**
* The number of bands in the audio spectrum.
*/
def audioSpectrumNumBands: IntegerProperty = delegate.audioSpectrumNumBandsProperty
def audioSpectrumNumBands_=(v: Int) {
audioSpectrumNumBands() = v
}
/**
* The sensitivity threshold in decibels; must be non-positive.
*/
def audioSpectrumThreshold: IntegerProperty = delegate.audioSpectrumThresholdProperty
def audioSpectrumThreshold_=(v: Int) {
audioSpectrumThreshold() = v
}
/**
* Whether playing should start as soon as possible.
*/
def autoPlay: BooleanProperty = delegate.autoPlayProperty
def autoPlay_=(v: Boolean) {
autoPlay() = v
}
/**
* The balance, or left-right setting, of the audio output.
*/
def balance: DoubleProperty = delegate.balanceProperty
def balance_=(v: Double) {
balance() = v
}
/**
* The current buffer position indicating how much media can be played without stalling the MediaPlayer.
*/
def bufferProgressTime: ReadOnlyObjectProperty[jfxu.Duration] = delegate.bufferProgressTimeProperty
/**
* The number of completed playback cycles.
*/
def currentCount: ReadOnlyIntegerProperty = delegate.currentCountProperty
/**
* The current rate of playback regardless of settings.
*/
def currentRate: ReadOnlyDoubleProperty = delegate.currentRateProperty
/**
* The current media playback time.
*/
def currentTime: ReadOnlyObjectProperty[jfxu.Duration] = delegate.currentTimeProperty
/**
* The number of times the media will be played.
*/
def cycleCount: IntegerProperty = delegate.cycleCountProperty
def cycleCount_=(v: Int) {
cycleCount() = v
}
/**
* The amount of time between the startTime and stopTime of this player.
*/
def cycleDuration: ReadOnlyObjectProperty[jfxu.Duration] = delegate.cycleDurationProperty
/**
* Observable property set to a MediaException if an error occurs.
*/
def error: ReadOnlyObjectProperty[jfxsm.MediaException] = delegate.errorProperty
/**
* Retrieves the Media instance being played.
*/
def media = delegate.getMedia
/**
* Whether the player audio is muted.
*/
def mute: BooleanProperty = delegate.muteProperty
def mute_=(v: Boolean) {
mute() = v
}
/**
* Event handler invoked when the player currentTime reaches stopTime and is not repeating.
*/
def onEndOfMedia: ObjectProperty[Runnable] = delegate.onEndOfMediaProperty
def onEndOfMedia_=(v: Runnable) {
onEndOfMedia() = v
}
def onEndOfMedia_=(op: => Unit) {
onEndOfMedia() = MediaPlayer.runnable(op)
}
/**
* Event handler invoked when an error occurs.
*/
def onError: ObjectProperty[Runnable] = delegate.onErrorProperty
def onError_=(v: Runnable) {
onError() = v
}
def onError_=(op: => Unit) {
onError() = MediaPlayer.runnable(op)
}
/**
* Event handler invoked when the status changes to HALTED.
*/
def onHalted: ObjectProperty[Runnable] = delegate.onHaltedProperty
def onHalted_=(v: Runnable) {
onHalted() = v
}
def onHalted_=(op: => Unit) {
onHalted() = MediaPlayer.runnable(op)
}
/**
* Event handler invoked when the player currentTime reaches a media marker.
*/
def onMarker = delegate.onMarkerProperty
def onMarker_=(v: jfxe.EventHandler[jfxsm.MediaMarkerEvent]) {
onMarker() = v
}
/**
* Event handler invoked when the status changes to PAUSED.
*/
def onPaused: ObjectProperty[Runnable] = delegate.onPausedProperty
def onPaused_=(v: Runnable) {
onPaused() = v
}
def onPaused_=(op: => Unit) {
onPaused() = MediaPlayer.runnable(op)
}
/**
* Event handler invoked when the status changes to PLAYING.
*/
def onPlaying: ObjectProperty[Runnable] = delegate.onPlayingProperty
def onPlaying_=(v: Runnable) {
onPlaying() = v
}
def onPlaying_=(op: => Unit) {
onPlaying() = MediaPlayer.runnable(op)
}
/**
* Event handler invoked when the status changes to READY.
*/
def onReady: ObjectProperty[Runnable] = delegate.onReadyProperty
def onReady_=(v: Runnable) {
onReady() = v
}
def onReady_=(op: => Unit) {
onReady() = MediaPlayer.runnable(op)
}
/**
* Event handler invoked when the player currentTime reaches stopTime and will be repeating.
*/
def onRepeat: ObjectProperty[Runnable] = delegate.onRepeatProperty
def onRepeat_=(v: Runnable) {
onRepeat() = v
}
def onRepeat_=(op: => Unit) {
onRepeat() = MediaPlayer.runnable(op)
}
/**
* Event handler invoked when the status changes to STALLED.
*/
def onStalled: ObjectProperty[Runnable] = delegate.onStalledProperty
def onStalled_=(v: Runnable) {
onStalled() = v
}
def onStalled_=(op: => Unit) {
onStalled() = MediaPlayer.runnable(op)
}
/**
* Event handler invoked when the status changes to STOPPED.
*/
def onStopped: ObjectProperty[Runnable] = delegate.onStoppedProperty
def onStopped_=(v: Runnable) {
onStopped() = v
}
def onStopped_=(op: => Unit) {
onStopped() = MediaPlayer.runnable(op)
}
/**
* The rate at which the media should be played.
*/
def rate: DoubleProperty = delegate.rateProperty
def rate_=(v: Double) {
rate() = v
}
/**
* The time offset where media should start playing, or restart from when repeating.
*/
def startTime: ObjectProperty[jfxu.Duration] = delegate.startTimeProperty
def startTime_=(v: Duration) {
startTime() = v
}
/**
* The current state of the MediaPlayer.
*/
def status: ReadOnlyObjectProperty[jfxsm.MediaPlayer.Status] = delegate.statusProperty
/**
* The time offset where media should stop playing or restart when repeating.
*/
def stopTime: ObjectProperty[jfxu.Duration] = delegate.stopTimeProperty
def stopTime_=(v: Duration) {
stopTime() = v
}
/**
* The total amount of play time if allowed to play until finished.
*/
def totalDuration: ReadOnlyObjectProperty[jfxu.Duration] = delegate.totalDurationProperty
/**
* The volume at which the media should be played.
*/
def volume: DoubleProperty = delegate.volumeProperty
def volume_=(v: Double) {
volume() = v
}
}