为什么JavaFX不支持.wav格式?

时间:2013-01-11 18:44:20

标签: java swing format javafx wav

1)JavaFX是否支持.wav格式? (不清楚Oracle's page页面)

2)如果不是,为什么会这样?

Swing是AWT的重新放置,也是Swing的JavaFX 我们说在Java中使用.wav文件格式很容易,那么为什么MediaMediaPlayer类的JavaFX不支持.wav格式?有什么问题吗?

更新

当我尝试播放.wav文件时,它会给我错误

UPDATE2:

SSCCE:

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.List;
import javafx.application.Platform;
import javafx.beans.value.*;
import javafx.embed.swing.JFXPanel;
import javafx.event.*;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.media.*;
import javafx.util.Duration;
import javax.swing.*;

 /** Example of playing all mp3 audio files in a given directory 
 * using a JavaFX MediaView launched from Swing 
 */
public class NewFXMain {
private static void initAndShowGUI() {
// This method is invoked on Swing thread
JFrame frame = new JFrame("FX");
final JFXPanel fxPanel = new JFXPanel();
frame.add(fxPanel);
frame.setBounds(200, 100, 800, 250);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);

Platform.runLater(new Runnable() {
  @Override public void run() {
    initFX(fxPanel);        
  }
 });
 }

 private static void initFX(JFXPanel fxPanel) {
// This method is invoked on JavaFX thread
Scene scene = new SceneGenerator().createScene();
fxPanel.setScene(scene);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
  @Override public void run() {
    initAndShowGUI();
  }
 });
 }
 }

 class SceneGenerator {    
 final Label currentlyPlaying = new Label();
 final ProgressBar progress = new ProgressBar();
 private ChangeListener<Duration> progressChangeListener;

 public Scene createScene() {
final StackPane layout = new StackPane();

 // determine the source directory for the playlist
final File dir = new File("e:\\");
if (!dir.exists() || !dir.isDirectory()) {
  System.out.println("Cannot find video source directory: " + dir);
  Platform.exit();
  return null;
}

// create some media players.
final List<MediaPlayer> players = new ArrayList<MediaPlayer>();
for (String file : dir.list(new FilenameFilter() {
  @Override public boolean accept(File dir, String name) {
    return name.endsWith(".wav");
  }
})) players.add(createPlayer("file:///" + (dir + "\\" + file).replace("\\", "/").replaceAll(" ", "%20")));
 if (players.isEmpty()) {
  System.out.println("No audio found in " + dir);
  Platform.exit();
  return null;
}    

// create a view to show the mediaplayers.
final MediaView mediaView = new MediaView(players.get(0));
final Button skip = new Button("Skip");
final Button play = new Button("Pause");

// play each audio file in turn.
for (int i = 0; i < players.size(); i++) {
  final MediaPlayer player     = players.get(i);
  final MediaPlayer nextPlayer = players.get((i + 1) % players.size());
  player.setOnEndOfMedia(new Runnable() {
    @Override public void run() {
      player.currentTimeProperty().removeListener(progressChangeListener);
      mediaView.setMediaPlayer(nextPlayer);
      nextPlayer.play();
    }
  });
  }

// allow the user to skip a track.
skip.setOnAction(new EventHandler<ActionEvent>() {
  @Override public void handle(ActionEvent actionEvent) {
    final MediaPlayer curPlayer = mediaView.getMediaPlayer();
    MediaPlayer nextPlayer = players.get((players.indexOf(curPlayer) + 1) %       players.size());
    mediaView.setMediaPlayer(nextPlayer);
    curPlayer.currentTimeProperty().removeListener(progressChangeListener);
    curPlayer.stop();
    nextPlayer.play();
  }
   });

 // allow the user to play or pause a track.
 play.setOnAction(new EventHandler<ActionEvent>() {
  @Override public void handle(ActionEvent actionEvent) {
    if ("Pause".equals(play.getText())) {
      mediaView.getMediaPlayer().pause();
      play.setText("Play");
    } else {
      mediaView.getMediaPlayer().play();
      play.setText("Pause");
    }
  }
 });

 // display the name of the currently playing track.
 mediaView.mediaPlayerProperty().addListener(new ChangeListener<MediaPlayer>() {
  @Override public void changed(ObservableValue<? extends MediaPlayer> observableValue, MediaPlayer oldPlayer, MediaPlayer newPlayer) {
    setCurrentlyPlaying(newPlayer);
  }
 });

 // start playing the first track.
 mediaView.setMediaPlayer(players.get(0));
 mediaView.getMediaPlayer().play();
 setCurrentlyPlaying(mediaView.getMediaPlayer());


 Button invisiblePause = new Button("Pause");
 invisiblePause.setVisible(false);
 play.prefHeightProperty().bind(invisiblePause.heightProperty());
 play.prefWidthProperty().bind(invisiblePause.widthProperty());

 // layout the scene.
 layout.setStyle("-fx-background-color: cornsilk; -fx-font-size: 20; -fx-padding: 20; -fx-alignment: center;");
 layout.getChildren().addAll(
  invisiblePause,
  VBoxBuilder.create().spacing(10).alignment(Pos.CENTER).children(
    currentlyPlaying,
    mediaView,
    HBoxBuilder.create().spacing(10).alignment(Pos.CENTER).children(skip, play, progress).build()
  ).build()
  );
 progress.setMaxWidth(Double.MAX_VALUE);
 HBox.setHgrow(progress, Priority.ALWAYS);
 return new Scene(layout, 800, 600);
 }


 private void setCurrentlyPlaying(final MediaPlayer newPlayer) {
 progress.setProgress(0);
 progressChangeListener = new ChangeListener<Duration>() {
  @Override public void changed(ObservableValue<? extends Duration> observableValue, Duration oldValue, Duration newValue) {
    progress.setProgress(1.0 * newPlayer.getCurrentTime().toMillis() / newPlayer.getTotalDuration().toMillis());
  }
  };
 newPlayer.currentTimeProperty().addListener(progressChangeListener);

 String source = newPlayer.getMedia().getSource();
 source = source.substring(0, source.length() - ".mp4".length());
 source = source.substring(source.lastIndexOf("/") + 1).replaceAll("%20", " ");
 currentlyPlaying.setText("Now Playing: " + source);
 }

  /** @return a MediaPlayer for the given source which will report any errors it    encounters */
 private MediaPlayer createPlayer(String aMediaSrc) {
 System.out.println("Creating player for: " + aMediaSrc);
 final MediaPlayer player = new MediaPlayer(new Media(aMediaSrc));
 player.setOnError(new Runnable() {
  @Override public void run() {
    System.out.println("Media error occurred: " + player.getError());
  }
 });
return player;
}
}

例外:

     Exception in runnable
     MediaException: MEDIA_UNSUPPORTED : Compressed WAVE is not supported!
at javafx.scene.media.Media.<init>(Unknown Source)
at SceneGenerator.createPlayer(NewFXMain.java:176)
at SceneGenerator.createScene(NewFXMain.java:74)
at NewFXMain.initFX(NewFXMain.java:39)
at NewFXMain.access$000(NewFXMain.java:20)
at NewFXMain$1.run(NewFXMain.java:32)
at com.sun.javafx.application.PlatformImpl$3.run(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$100(Unknown Source)
at com.sun.glass.ui.win.WinApplication$2$1.run(Unknown Source)
at java.lang.Thread.run(Thread.java:722)

1 个答案:

答案 0 :(得分:8)

对我来说似乎很清楚 - Supported Media Types

...
MP4 MPEG-4 Part 14  H.264/AVC   AAC video/mp4, audio/x-m4a, video/x-m4v .mp4, .m4a, .m4v
WAV Waveform Audio Format   N/A PCM audio/x-wav .wav

编辑: 因此,明显的答案(基于您的编辑)是JavaFX不支持压缩波形格式。

  

WAV :大多数WAV文件都是未压缩的,但它们可以保持压缩状态   音频也是如此。 JavaFX不支持播放压缩格式   然而。因此,在合并WAV文件时,开发人员需要确保   文件格式是包含未压缩PCM的WAV。

     

source