2017-03-03 13 views
0

以下はコードサンプルですが、その他の機能は省略されています。以下のコードはメディアプレーヤーのみを網羅しています。私が取り組んでいるプロジェクトのメニュー画面で使用されています。私の問題は、正しく動作するように、トグルボタンのOn/OffであるmusicButtonを取得することです。次のコードを使用して、音楽トグルボタンとやり取りすると、再生中の音楽が停止します。再生を再開するためにもう一度クリックすると、再開しません。それは一度停止し、完全に停止します。JavaFX - 音楽オン/オフトグルボタン(動作していません)

私は、2つのifステートメントのトグルボタンのブール値を使ってみました。オフで押されている場合は、音楽を一時停止します。オンになっている場合は、音楽を再開します。問題は、前述したように、音楽を一時停止することですが、再開することはできません。私はループでいくつかの組み合わせを試しましたが、何もうまくいきませんでした。

これについては文が単純すぎると思います。私はJavaDocsと様々なオンライン記事を精査しましたが、何か決定的なものは見つけられません。私はリスナーについて少しは読んだことがありますが、オン/オフスイッチではあまりにも複雑です。

私の質問: ユーザーがクリックすると、音楽を一時停止/再生する方法を教えてください。

ご協力いただきありがとうございます。

-Bagger

/* A simple game, the mechanics not yet implemented. 

This is simply working on the title screen. */ 


import java.io.File; 
import javafx.application.Application; 
import javafx.geometry.Pos; 
import javafx.scene.Scene; 
import javafx.scene.control.ToggleButton; 
import javafx.scene.layout.VBox; 
import javafx.scene.media.Media; 
import javafx.scene.media.MediaPlayer; 
import javafx.stage.Stage; 

public class MenuFX extends Application { 

@Override 

public void start (Stage primaryStage) { 

    // Make the window a set size... 
    primaryStage.setResizable(false); 

    // Create media player 
    // Rather than inputting the entire absolute URI, which would confine the program 
    // to the creator's device, we create a new file, grab the URI on whatever machine 
    // the program is running on and convert it to a string... portability. 
    Media menuMusic = new Media(new File("music/menu.mp3").toURI().toString()); 
    MediaPlayer menuPlayer = new MediaPlayer(menuMusic); 

    // Want to see the absolute URI? Uncomment the next line 
    //System.out.println(new File("music/menu.mp3").toURI().toString()); 

    // Adjust the cycles and volume then start playing menu music 
    // Lazy, but it will suffice 
    menuPlayer.setCycleCount(999999999); 
    menuPlayer.setVolume(0.1); 
    menuPlayer.setAutoPlay(true); 




    /* 
    Need assistance here 
    */ 


    // Create music toggle button 
    ToggleButton musicButton = new ToggleButton("Music On/Off"); 

    if (musicButton.isSelected() == false) { 

     musicButton.setOnAction(e -> menuPlayer.pause()); 
    } 

    if (musicButton.isSelected() == true) { 

     musicButton.setOnAction(e -> menuPlayer.play()); 
    } 




    // Add all nodes to the vbox pane and center it all 
    // Must be in order from top to bottom 
    menuVBox.getChildren().add(musicButton); 
    menuVBox.setAlignment(Pos.CENTER); 

    // New scene, place pane in it 
    Scene scene = new Scene(menuVBox, 630, 730); 

    // Place scene in stage 
    primaryStage.setTitle("-tiles-"); 
    primaryStage.setScene(scene); 
    primaryStage.show(); 
} 

// Needed to run JavaFX w/o the use of the command line 
public static void main(String[] args) { 

    launch(args); 
} 

} 

答えて

0

私はあなたがそれを考える上だと思います。 ToggleButtonに音楽を一時停止して再生するには、EventListenerが必要です。

musicButton.setOnAction(event -> { 
    if (musicButton.isSelected()) { 
     menuPlayer.pause(); 
    }else { 
     menuPlayer.play(); 
    } 
}); 

これはあなたに望ましい効果を与えるはずです。

ToggleButtonがデフォルトで選択されていないため、EventListenerは唯一menuPlayer.pause();です。それをクリックすると、一時停止します。あなたのコードをEventListenerに移動し、if-elseという適切なコードを使用しました。

+0

非常に簡単な解決策...ありがとうございました。それだけで同じハンドラブロックに入る必要があった...私は今参照してください。ありがとうございました! – Psychobagger

+0

いつでも。これで問題が解決した場合は、答えとして選択してください。 –

関連する問題