如何在 scalafx gui 场景中添加 mp3?
How to add a mp3 in scalafx gui scene?
我尝试使用 scalafx 将 mp3 添加到我的 scala gui 中,但我无法添加到场景中
这就是我所拥有的,但它不起作用...
val gameStage = new PrimaryStage {
title = "Game Graphics"
scene = new Scene(windowWidth, windowHeight) {
var audio = new Media(url)
var mediaPlayer = new MediaPlayer(audio)
mediaPlayer.volume = 100
mediaPlayer.play()
}
}
似乎有一个问题是您没有使用 MediaView
实例将 MediaPlayer
添加到场景中。另外,最好在场景显示之前不开始播放媒体。
我认为您需要这样的东西(作为一个完整的应用程序):
import scalafx.application.JFXApp
import scalafx.application.JFXApp.PrimaryStage
import scalafx.scene.{Group, Scene}
import scalafx.scene.media.{Media, MediaPlayer, MediaView}
object GameGraphics
extends JFXApp {
// Required info. Populate as necessary...
val url = ???
val windowWidth = ???
val windowHeight = ???
// Initialize the media and media player elements.
val audio = new Media(url)
val mediaPlayer = new MediaPlayer(audio)
mediaPlayer.volume = 100
// The primary stage is best defined as the stage member of the application.
stage = new PrimaryStage {
title = "Game Graphics"
width = windowWidth
height = windowHeight
scene = new Scene {
// Create a MediaView instance of the media player, and add it to the scene. (It needs
// to be the child of a Group, or the child of a subclass of Group).
val mediaView = new MediaView(mediaPlayer)
root = new Group(mediaView)
}
// Now play the media.
mediaPlayer.play()
}
}
此外,您应该更喜欢 val
而不是 var
,尤其是在关联变量定义后不需要修改的情况下。
顺便说一句,无法测试您的代码,所以请考虑下次发布 minimal, complete and verifiable example。
我尝试使用 scalafx 将 mp3 添加到我的 scala gui 中,但我无法添加到场景中
这就是我所拥有的,但它不起作用...
val gameStage = new PrimaryStage {
title = "Game Graphics"
scene = new Scene(windowWidth, windowHeight) {
var audio = new Media(url)
var mediaPlayer = new MediaPlayer(audio)
mediaPlayer.volume = 100
mediaPlayer.play()
}
}
似乎有一个问题是您没有使用 MediaView
实例将 MediaPlayer
添加到场景中。另外,最好在场景显示之前不开始播放媒体。
我认为您需要这样的东西(作为一个完整的应用程序):
import scalafx.application.JFXApp
import scalafx.application.JFXApp.PrimaryStage
import scalafx.scene.{Group, Scene}
import scalafx.scene.media.{Media, MediaPlayer, MediaView}
object GameGraphics
extends JFXApp {
// Required info. Populate as necessary...
val url = ???
val windowWidth = ???
val windowHeight = ???
// Initialize the media and media player elements.
val audio = new Media(url)
val mediaPlayer = new MediaPlayer(audio)
mediaPlayer.volume = 100
// The primary stage is best defined as the stage member of the application.
stage = new PrimaryStage {
title = "Game Graphics"
width = windowWidth
height = windowHeight
scene = new Scene {
// Create a MediaView instance of the media player, and add it to the scene. (It needs
// to be the child of a Group, or the child of a subclass of Group).
val mediaView = new MediaView(mediaPlayer)
root = new Group(mediaView)
}
// Now play the media.
mediaPlayer.play()
}
}
此外,您应该更喜欢 val
而不是 var
,尤其是在关联变量定义后不需要修改的情况下。
顺便说一句,无法测试您的代码,所以请考虑下次发布 minimal, complete and verifiable example。