JAVA音楽プレーヤー私のJAVA宿題
28731 ワード
これは私达のJAVA课程の宿题で、笔者はやはり多くの时间をかけて完成して、まだ少し腐っていますが...
直接コードを貼る~
直接コードを貼る~
package MyMusicPlayer;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.media.*;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.filechooser.FileFilter;
public class MPlayer implements ActionListener, ControllerListener, Runnable {
//
private JFrame frame = new JFrame(" ");
private JButton open = new JButton(" ");
private JButton stop = new JButton(" ");
private JButton music = new JButton(" ");
private JButton next = new JButton(" ");
private JButton last = new JButton(" ");
private JPanel progress = new JPanel();
private JPanel progress2 = new JPanel();
private JPanel buttonPanel = new JPanel();
private JPanel buttonPanel2 = new JPanel();
private JPanel buttonPanel3 = new JPanel();
private JButton timeInformation = new JButton();
private JSlider timeSlider = new JSlider(SwingConstants.HORIZONTAL, 0, 100, 0);
//
int index;
int count;
int countSecond;
int newtime = 0;
int ischanging = 0;
int ispressing = 0;
private Player player = null;
private MP3FileChooser fileChooser = new MP3FileChooser();
File currentFile = null;
List<File> currentFiles = null;
private File currentDirectory = null;
/**
* ActionListener override
*/
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if (cmd.equals(" ")) {
if (player == null) {
if (fileChooser.showOpenDialog(frame) == MP3FileChooser.APPROVE_OPTION) {
this.currentFile = fileChooser.getSelectedFile();
File cd = fileChooser.getCurrentDirectory();
if (cd != this.currentDirectory
|| this.currentDirectory == null) {
FileFilter[] fileFilters = fileChooser
.getChoosableFileFilters();
File files[] = cd.listFiles();
this.currentFiles = new ArrayList<File>();
for (File file : files) {
for (FileFilter filter : fileFilters) {
if (!file.isDirectory() && filter.accept(file)) {
this.currentFiles.add(file);
}
}
}
}
index = currentFiles.indexOf(currentFile);
count = currentFiles.size();
PlayFile();
}
} else {
player.start();
}
} else if (cmd.equals(" ")) {
if (player != null) {
player.close();
player = null;
open.setText(" ");
next.setText(" ");
last.setText(" ");
music.setText(" ");
timeInformation.setText(" :00:00 / :00:00");
}
} else if (cmd.equals(" ")) {
open.setText(" ");
player.stop();
} else if (cmd.equals(" ")) {
open.setText(" ");
player.start();
} else if (cmd.equals(" ") || cmd.startsWith(" ")) {
if (player != null) {
nextMP3();
}
} else if (cmd.equals(" ") || cmd.startsWith(" ")) {
if (player != null) {
lastMP3();
}
} else {
JOptionPane.showMessageDialog(null, " ");
}
}
/**
* ControllerListener override
*/
public void controllerUpdate(ControllerEvent e) {
if (e instanceof EndOfMediaEvent) {
nextMP3();
}
}
/**
* MP3
*/
public void PlayFile() {
try {
player = Manager.createRealizedPlayer(currentFiles.get(index).toURI().toURL());
player.prefetch();
player.setMediaTime(new Time(0.0));//
player.addControllerListener(this);
player.start();
} catch (Exception e1) {
dealError();
return;
}
this.setFrame();
}
/**
* MP3
*
* @MP3
* @
*/
public String getMP3Name(String str) {
int i;
for (i = str.length() - 1; i > 0; i--) {
if (str.charAt(i) == '\\')
break;
}
str = str.substring(i + 1, str.length() - 4);
return str;
}
/**
*
*/
public void nextMP3() {
if (this.currentFiles != null && !this.currentFiles.isEmpty()) {
if ( ++index == this.currentFiles.size()) {
index = 0;
}
player.stop();
PlayFile();
}
}
/**
*
*/
public void lastMP3() {
if (this.currentFiles != null && !this.currentFiles.isEmpty()) {
if ( index-- == 0) {
index = this.currentFiles.size() - 1;
}
player.stop();
PlayFile();
}
}
/**
*
*/
public void setFrame()
{
open.setText(" ");
next.setText(" "
+ getMP3Name(this.currentFiles.get(
index == count - 1 ? 0 : index + 1).toString()));
last.setText(" "
+ getMP3Name(this.currentFiles.get(
index == 0 ? count - 1 : index - 1).toString()));
music.setText(" :"+getMP3Name(this.currentFiles.get(index).toString()));
countSecond = (int)player.getDuration().getSeconds();
timeSlider.setMaximum(countSecond);
timeSlider.setValue(0);
newtime = 0;
};
/**
*
* ,
*/
public void dealError()
{
currentFiles.remove(index);
if( --count == index )
index = 0;
if( count == 0)
player = null;
else
PlayFile();
}
/**
* MP3
*
* @author BlackStar
*
*/
@SuppressWarnings("serial")
class MP3FileChooser extends JFileChooser {
public MP3FileChooser() {
super();
setAcceptAllFileFilterUsed(false);
addFilter();
}
public MP3FileChooser(String currentDirectoryPath) {
super(currentDirectoryPath);
setAcceptAllFileFilterUsed(false);
addFilter();
}
private void addFilter() {
this.addChoosableFileFilter(new MyFileFilter(
new String[] { ".MP3" }, "MP3 "));
}
}
/**
* MP3
*
* @author BlackStar
*
*/
class MyFileFilter extends FileFilter {
String[] suffarr;
String decription;
public MyFileFilter() {
super();
}
public MyFileFilter(String[] suffarr, String decription) {
super();
this.suffarr = suffarr;
this.decription = decription;
}
public boolean accept(File f) {
for (String s : suffarr) {
if (f.getName().toUpperCase().endsWith(s)) {
return true;
}
}
return f.isDirectory();
}
public String getDescription() {
return this.decription;
}
}
/**
* MPlayer
*/
public MPlayer() {
open.addActionListener(this);
stop.addActionListener(this);
next.addActionListener(this);
last.addActionListener(this);
music.addActionListener(this);
timeInformation.addActionListener(this);
frame.setLayout(new FlowLayout());
buttonPanel.add(open);
buttonPanel.add(stop);
frame.add(buttonPanel);
timeSlider.setMajorTickSpacing(1);
timeSlider.setPaintTicks(true);
timeSlider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent arg0) {
if (player != null && ispressing == 1) {
newtime = (int)((JSlider)arg0.getSource()).getValue();
timeInformation.setText(" :"+newtime/60+":"+newtime%60+" : "+countSecond/60+":"+countSecond%60);
ischanging = 1;
}
}
});
timeSlider.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent arg0) {
ispressing = 1;
}
public void mouseReleased(MouseEvent arg0) {
ispressing = 0;
}
});
timeInformation.setText(" :00:00 / :00:00");
progress.add(timeInformation,BorderLayout.NORTH);
progress2.add(timeSlider,BorderLayout.SOUTH);
frame.add(progress);
frame.add(music);
frame.add(progress2);
buttonPanel2.add(last);
buttonPanel3.add(next);
frame.add(buttonPanel2);
frame.add(buttonPanel3);
frame.setSize(220, 280);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
/**
*
*/
public void run() {
while(true) {
sleep();
if(player != null) {
if(ispressing == 0) {
if(ischanging == 1) {
newtime = timeSlider.getValue();
player.setMediaTime(new Time(((long)newtime)*1000000000));
ischanging = 0;
} else {
newtime = (int)player.getMediaTime().getSeconds();
timeSlider.setValue(newtime);
timeInformation.setText(" :"+newtime/60+":"+newtime%60+" : "+countSecond/60+":"+countSecond%60);
}
}
}
}
}
/**
* while
*/
public void sleep()
{
try {
Thread.sleep(10);
} catch (InterruptedException e) {
}
}
/**
*
*/
public static void main(String[] args) {
MPlayer play = new MPlayer();
Thread timeRun = new Thread(play);
timeRun.start();
}
}