我尝试使用JMF
在Java中创建一个简单的mp3播放器。
我尝试了一些.mp3
和.wav
文件,但它不起作用。 BlueJ
不会向我显示任何错误,只会将toURL()
作为警告弃用。
我读过有关mp3编解码器的内容,我也尝试将其放在主函数中,但没有任何改变。
大部分代码都是按钮管理,但无论如何我都会留下它。
完整代码在这里:
import java.io.File;
import java.io.IOException;
import java.net.URL;
import javax.media.*;
import javax.media.format.AudioFormat;
public class AudioPlayerTest extends javax.swing.JFrame {
Player audioPlayer = null;
String audioPath = "";
public AudioPlayerTest() {
initComponents();
audioPath = "C:\\test.mp3";
initAudioPlayer(audioPath);
}
private void initAudioPlayer(String pathname) {
try {
URL url = new File(pathname).toURL();
audioPlayer = Manager.createRealizedPlayer(url);
} catch (IOException ex) {
ex.printStackTrace();
} catch (NoPlayerException ex) {
ex.printStackTrace();
} catch (CannotRealizeException ex) {
ex.printStackTrace();
}
}
@SuppressWarnings("unchecked")
private void initComponents() {
mainPanel = new javax.swing.JPanel();
PlayButton = new javax.swing.JButton();
PauseButton = new javax.swing.JButton();
StopButton = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
PlayButton.setText("Play");
PlayButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
PlayButtonActionPerformed(evt);
}
});
PauseButton.setText("Pause");
PauseButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
PauseButtonActionPerformed(evt);
}
});
StopButton.setText("Stop");
StopButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
StopButtonActionPerformed(evt);
}
});
javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
mainPanel.setLayout(mainPanelLayout);
mainPanelLayout.setHorizontalGroup(
mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(mainPanelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(PlayButton)
.addGap(27, 27, 27)
.addComponent(PauseButton)
.addGap(18, 18, 18)
.addComponent(StopButton)
.addContainerGap(36, Short.MAX_VALUE))
);
mainPanelLayout.setVerticalGroup(
mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(mainPanelLayout.createSequentialGroup()
.addContainerGap()
.addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(PlayButton)
.addComponent(PauseButton)
.addComponent(StopButton))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(mainPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(69, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(mainPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}
private void PlayButtonActionPerformed(java.awt.event.ActionEvent evt) {
audioPlayer.start();
}
private void PauseButtonActionPerformed(java.awt.event.ActionEvent evt) {
audioPlayer.stop();
}
private void StopButtonActionPerformed(java.awt.event.ActionEvent evt) {
audioPlayer.stop();
audioPlayer.setMediaTime(new Time(0.0));
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new AudioPlayerTest().setVisible(true);
}
});
}
private javax.swing.JButton PauseButton;
private javax.swing.JButton PlayButton;
private javax.swing.JButton StopButton;
private javax.swing.JPanel mainPanel;
}