无法让 .mp3 停止使用 JavaFX 媒体播放器
Can't get .mp3 to stop using JavaFX media player
所以我正在尝试使用 javafx 和 scenebuilder 制作点唱机应用程序。它有一个键盘,我在其中将歌曲的编号键入标签,标签上的文本被转换为 int 并传递给 track() 方法,该方法从数组中选择一个曲目(数组是某物的占位符未来更帅)
play() 方法播放曲目就好了......
控制器代码class:
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
public class MainController implements Initializable {
@Override
public void initialize(URL location, ResourceBundle resources) {
// TODO Auto-generated method stub
}
@FXML
private Label number;
public void zero(ActionEvent event){
number.setText(number.getText() + "0");
}
public void one(ActionEvent event){
number.setText(number.getText() + "1");
}
public void two(ActionEvent event){
number.setText(number.getText() + "2");
}
public void three(ActionEvent event){
number.setText(number.getText() + "3");
}
public void four(ActionEvent event){
number.setText(number.getText() + "4");
}
public void five(ActionEvent event){
number.setText(number.getText() + "5");
}
public void six(ActionEvent event){
number.setText(number.getText() + "6");
}
public void seven(ActionEvent event){
number.setText(number.getText() + "7");
}
public void eight(ActionEvent event){
number.setText(number.getText() + "8");
}
public void nine(ActionEvent event){
number.setText(number.getText() + "9");
}
public void clear(){
number.setText("");
}
public MediaPlayer track(){
URL[] songs = new URL[]{
getClass().getResource("/res/03. Eyes On Fire.mp3"),
getClass().getResource("/res/05,Graveyard-Endless Night.mp3"),
getClass().getResource("/res/10 Nas - It Ain't Hard To Tell.mp3")
};
final Media media = new Media(songs[Integer.parseInt(number.getText())].toString());
final MediaPlayer mediaplayer = new MediaPlayer(media);
return mediaplayer;
}
public void play(){
track().play();
}
public void stop(){
track().stop();
}
}
提前致谢。
您的 track()
方法在您每次调用它时都会创建一个新的 MediaPlayer
。因此,当您调用 stop()
时,您是在与您调用 play()
的实例不同的实例上调用它,而您调用 play()
的实例永远不会停止。
您需要保留对 "current" MediaPlayer
的引用。让 track()
(或者 selectTrack()
更新该引用,然后您可以在需要时对其调用 stop()
。类似于:
public class MainController {
private MediaPlayer player ;
// ...
public void selectTrack() {
URL[] songs = new URL[] { ... } ;
final Media media = new Media(songs[Integer.parseInt(number.getText())].toString());
this.player = new MediaPlayer(media);
}
public void play() {
// stop current player:
if (player != null) {
player.stop();
}
selectTrack();
player.play();
}
public void stop() {
player.stop();
}
}
所以我正在尝试使用 javafx 和 scenebuilder 制作点唱机应用程序。它有一个键盘,我在其中将歌曲的编号键入标签,标签上的文本被转换为 int 并传递给 track() 方法,该方法从数组中选择一个曲目(数组是某物的占位符未来更帅) play() 方法播放曲目就好了...... 控制器代码class:
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
public class MainController implements Initializable {
@Override
public void initialize(URL location, ResourceBundle resources) {
// TODO Auto-generated method stub
}
@FXML
private Label number;
public void zero(ActionEvent event){
number.setText(number.getText() + "0");
}
public void one(ActionEvent event){
number.setText(number.getText() + "1");
}
public void two(ActionEvent event){
number.setText(number.getText() + "2");
}
public void three(ActionEvent event){
number.setText(number.getText() + "3");
}
public void four(ActionEvent event){
number.setText(number.getText() + "4");
}
public void five(ActionEvent event){
number.setText(number.getText() + "5");
}
public void six(ActionEvent event){
number.setText(number.getText() + "6");
}
public void seven(ActionEvent event){
number.setText(number.getText() + "7");
}
public void eight(ActionEvent event){
number.setText(number.getText() + "8");
}
public void nine(ActionEvent event){
number.setText(number.getText() + "9");
}
public void clear(){
number.setText("");
}
public MediaPlayer track(){
URL[] songs = new URL[]{
getClass().getResource("/res/03. Eyes On Fire.mp3"),
getClass().getResource("/res/05,Graveyard-Endless Night.mp3"),
getClass().getResource("/res/10 Nas - It Ain't Hard To Tell.mp3")
};
final Media media = new Media(songs[Integer.parseInt(number.getText())].toString());
final MediaPlayer mediaplayer = new MediaPlayer(media);
return mediaplayer;
}
public void play(){
track().play();
}
public void stop(){
track().stop();
}
}
提前致谢。
您的 track()
方法在您每次调用它时都会创建一个新的 MediaPlayer
。因此,当您调用 stop()
时,您是在与您调用 play()
的实例不同的实例上调用它,而您调用 play()
的实例永远不会停止。
您需要保留对 "current" MediaPlayer
的引用。让 track()
(或者 selectTrack()
更新该引用,然后您可以在需要时对其调用 stop()
。类似于:
public class MainController {
private MediaPlayer player ;
// ...
public void selectTrack() {
URL[] songs = new URL[] { ... } ;
final Media media = new Media(songs[Integer.parseInt(number.getText())].toString());
this.player = new MediaPlayer(media);
}
public void play() {
// stop current player:
if (player != null) {
player.stop();
}
selectTrack();
player.play();
}
public void stop() {
player.stop();
}
}