2016-11-17 55 views
0

我的歌曲表現有問題。我有一個從filechooser加載的音軌列表,放置在TableView中並存儲在列表(播放器)中。通過getSelectionModel採取選定的音頻文件,我運行它,迄今如此好。如果您不停下來並選擇另一個音頻文件併發送給您播放,則會疊加聲音。我怎樣才能停止執行前一個文件,從而避免多重播放?如何防止使用mediaplayer javafx進行多重播放?

addFileAudio代碼:

Media media; 
final List<MediaPlayer> players = new ArrayList<>(); 
String fileName; 
MediaPlayer mediaPlayer; 
public void addFileAudio(ActionEvent event) { 
      BasicFileAttributes attr = null; 
     FileChooser fileChooser = new FileChooser(); 
     fileChooser.setTitle("Seleziona il file da caricare"); 
     fileChooser.getExtensionFilters().addAll(
       new ExtensionFilter("Audio Files","*.wav","*.mp3")); 
     File selectedFile = fileChooser.showOpenDialog(new Stage()); 
     if (selectedFile != null) { 

      media = new Media(new File(selectedFile.getAbsolutePath()).toURI().toString()); 
      mediaPlayer = new MediaPlayer(media); 
      players.add(mediaPlayer); 

      MediaView mediaView = new MediaView(mediaPlayer); 
      mediaView.setMediaPlayer(mediaPlayer); 

      Path pathFile = Paths.get(selectedFile.toURI()); 
      try { 
       attr = Files.readAttributes(pathFile, BasicFileAttributes.class); 
      } catch (IOException e) { 
       Exception.showError(1); 
      } 
     }else{ 
       Exception.showWarning(0); 
     } 

     String fileName = selectedFile.getName(); 
     String data = attr.creationTime().toString().substring(0,10); 
     Table1.getItems().add(new PopulateTable(Table1.getItems().size()+1, fileName, data)); 

     } 

代碼播放按鈕:

void play(ActionEvent event) { 
MediaPlayer player = players.get(Table1.getSelectionModel().getSelectedIndex()); Status status = player.getStatus(); 
if (status!= Status.PLAYING) { 
player.play(); 
} else { 
MediaPlayer playerNext = players.get(Table1.getSelectionModel().getSelectedIndex()); player.stop(); playerNext.play(); } 

+0

狀態狀態不應該在您的play()方法中。它應該是靜態的和全球的。然後一旦你按播放()你播放歌曲,然後設置狀態播放 – Sedrick

回答

0

也許是這樣的: UPDATE

@FXML private ListView lvPlaylist; 


MediaPlayer player; 
Media pick; 

Status status = Status.STOPPED; 

String pathToMP3One = "song1.mp3"; 
String pathToMP3Two = "song2.mp3"; 

HashMap<String, String> hashmap = new HashMap(); 

ObservableList<String> items =FXCollections.observableArrayList(); 


@FXML 
private void handleButtonAction(ActionEvent event) { 

    switch(status) 
    { 
     case STOPPED: 

      String fileToPlay = hashmap.get(lvPlaylist.getSelectionModel().getSelectedItem().toString()); 
      System.out.println(fileToPlay); 
      pick = new Media(fileToPlay); 
      player = new MediaPlayer(pick); 
      player.play(); 
      status = Status.PLAYING; 
      break; 
     case PLAYING: 
      player.stop(); 
      fileToPlay = hashmap.get(lvPlaylist.getSelectionModel().getSelectedItem().toString()); 
      pick = new Media(fileToPlay); 
      player = new MediaPlayer(pick); 
      player.play(); 
      status = Status.PLAYING; 
      break;     
    } 
} 

@FXML 
public void addFileAudio(ActionEvent event) { 
    BasicFileAttributes attr = null; 
    FileChooser fileChooser = new FileChooser(); 
    fileChooser.setTitle("Seleziona il file da caricare"); 
    fileChooser.getExtensionFilters().addAll(new ExtensionFilter("Audio Files","*.wav","*.mp3")); 
    File selectedFile = fileChooser.showOpenDialog(new Stage()); 
    if (selectedFile != null) { 
     Path pathFile = Paths.get(selectedFile.toURI()); 
     try { 
      attr = Files.readAttributes(pathFile, BasicFileAttributes.class); 
     } catch (IOException e) { 
      System.out.println(e.toString()); 
     } 
    }else{ 
     System.out.println("hello"); 
    } 

    hashmap.put(selectedFile.getName(), selectedFile.toURI().toString());  
    System.out.println("File name: " + selectedFile.getName()); 
    lvPlaylist.getItems().add(selectedFile.getName()); 

    } 
@Override 
public void initialize(URL url, ResourceBundle rb) { 
    // TODO 
    lvPlaylist.setItems(items); 

} 

這對我有效!

+0

謝謝,但方法setStatus給我一個錯誤: 方法setStatus從類型MediaPlayer不可見 你建議我什麼? – Margherita

+0

我猜你有一個方法setStatus()。剛剛擺脫player.setStatus();部分。你有player.getStatus();所以我認爲你必須有player.setStatus();抱歉。如果你沒有player.setStatus(),你怎麼改變玩家的狀態。爲什麼使用player.getStatus,如果它始終是相同的。 – Sedrick

+0

事實上,我不能僅僅爲了接受而改變價值。我有一個statusProperty()方法,但我無法弄清楚它是否屬於我的情況。您也可以只使用STATUS。 (停止/暫停/播放...) – Margherita

相關問題