我正在制作MP3播放器,但我的问题是我不能在旧歌完成后自动开始新歌。我尝试过使用isComplete()
然后我尝试找到歌曲的长度并使用thread.sleep()并将其设置为与我的歌曲相同的长度,所以当歌曲结束时,新的歌曲开始并且它起作用,唯一的问题是我不能点击任何按钮,当我尝试退出没有任何事情发生时,歌曲继续播放,一个新的将开始,但窗口冻结
如果你能提供帮助那就太好了(问题出在第312行)
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.logging.*;
import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.net.*;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.*;
import javazoom.jl.player.Player;
import javazoom.spi.mpeg.sampled.file.MpegAudioFileReader;
public class PPR extends JPanel{
private static long startTime;
private static long stopTime;
static int ms;
static int songnumber = 0;
static String song;
static Long duration;
//Above is the song number and the path
static File test = new File ("C:/Users/cmtc/Documents/WorkSpace/Mp3 Player/music/aa.mp3");
static File[] files = new File("music").listFiles();
static LinkedList songnames = new LinkedList();
//above is the array that gets all of the files and the linked list that gets all the names
private final static int NOTSTARTED = 0;
private final static int PLAYING = 1;
private final static int PAUSED = 2;
private final static int FINISHED = 3;
//above tell the player what is happening
private final Object playerLock = new Object();
//not sure if i need above
private static int playerStatus = NOTSTARTED;
//above starts off by saying the player hasnt started yet
static JFrame f = new JFrame();
static JPanel p = new JPanel();
static String filename = "this string doesnt matter";
//above is the jpanel and jframe
static FileInputStream input;
static MP3 player;
//above allows for file io and the mp3 player
static Runnable Graphic = new Runnable() {
public void run() {
gui();
}
};
// I had to make threads so that more than one button is visible
public static void invokeLater(Runnable doRun){
SwingUtilities.invokeLater(Graphic);
}
//above is what allows multiple buttons to be visible
public PPR(){
SwingUtilities.invokeLater(Graphic);
}
//above is similar to what is above this
public static void gui(){
f.setVisible(true);
f.setSize(1600,900);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(p);
PlayPauseRewind();
}
//above is baseically what runs the three buttons
public static void PlayPauseRewind(){
Object play = ppr(80,26,25,25,"pics/play.jpg");
((AbstractButton) play).addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
startTime = e.getWhen();
if (playerStatus == PLAYING){
pause();
playerStatus = PAUSED;
}else if (playerStatus == NOTSTARTED){
play();
playerStatus = PLAYING;
}else if (playerStatus == PAUSED){
resume();
playerStatus = PLAYING;
}
}
});
//above is play ***************************************
Object next = ppr(120,26,25,25,"pics/next.png");
((AbstractButton) next).addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if (songnumber == files.length-1){
System.out.println("No More songs");
}else if (playerStatus == NOTSTARTED){
play();
playerStatus = PLAYING;
pause();
playerStatus = PAUSED;
songnumber++;
play();
playerStatus = PLAYING;
System.out.println(songnumber);
}else if (playerStatus == PAUSED){
songnumber++;
play();
pause();
playerStatus = PAUSED;
System.out.println(songnumber);
}else{
stop();
songnumber++;
System.out.println(songnumber);
//above tells the next button what to do below is the try/catch
try {
input = new FileInputStream(allsongs(songnumber,song));
player = new MP3(input);
// start playing
player.play();
// after 5 secs, pause
} catch (final Exception e1) {
throw new RuntimeException(e1);
}
playerStatus = PLAYING;
}
}
});
Object back = ppr(40,26,25,25,"pics/back.png");
((AbstractButton) back).addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if (songnumber == 0){
System.out.println("No More songs");
}else if (playerStatus == NOTSTARTED){
play();
playerStatus = PLAYING;
pause();
playerStatus = PAUSED;
songnumber--;
play();
playerStatus = PLAYING;
System.out.println(songnumber);
}else if (playerStatus == PAUSED){
songnumber--;
play();
pause();
playerStatus = PAUSED;
System.out.println(songnumber);
}else{
stop();
songnumber--;
System.out.println(songnumber);
//above tells the back button what to do below is the try/catch
try {
input = new FileInputStream(allsongs(songnumber,song));
player = new MP3(input);
// start playing
player.play();
// after 5 secs, pause
} catch (final Exception e1) {
throw new RuntimeException(e1);
}
playerStatus = PLAYING;
}
}
});
}
public static Object ppr(int x, int y, int width, int height, String file){
p.setLayout(null);
Toolkit tool = Toolkit.getDefaultToolkit();
Image player = tool.getImage(file);
ImageIcon playbutton = new ImageIcon(player);
JButton play = new JButton(playbutton);
play.setBounds(x, y, width, height);
return p.add(play);
//above is a method that makes creating buttons very easy
}
public static void play(){
startTime = System.currentTimeMillis();
try {
input = new FileInputStream(allsongs(songnumber,song));
player = new MP3(input);
player.play();
} catch (final Exception e) {
throw new RuntimeException(e);
}
playerStatus = PLAYING;
nextsong();
}
//above is the play method
public static void pause(){
try {
// start playing
player.pause();
// after 5 secs, pause
} catch (final Exception e) {
throw new RuntimeException(e);
}
playerStatus = PAUSED;
}
//above is the pause method
public static void resume(){
try {
// start playing
player.resume();
// after 5 secs, pause
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
public static void length(){
long size = files[songnumber].length();
AudioFileFormat baseFileFormat;
int ms = 0;
try {
baseFileFormat = new MpegAudioFileReader().getAudioFileFormat(files[songnumber]);
Map properties = baseFileFormat.properties();
duration = (Long) properties.get("duration");
duration = duration/1000;
ms = (int) (duration/10);
System.out.println(ms);
//System.out.println(size);
} catch (UnsupportedAudioFileException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//ABOVE AND BELOW IS WHERE I AM TRYING TO FIX IT;
public static void nextsong(){
length();
try {
Thread.sleep(duration);
stop();
songnumber++;
play();
playerStatus=PLAYING;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void stop(){
try {
player.stop();
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
//above is the resume method
public static void temp(){
JOptionPane.showMessageDialog(null, "Action Listener is working");
}
//above is what I use to make sure the actionlistener is working I wont need it eventually
public static String allsongs(int songnumber,String song){
for (int i = 0; i<files.length;i++){
songnames.add("music/" + files[i].getName());
song = (String) songnames.get(songnumber);
}
return song;
}
//above is the loop that goes through the folder and names everysong while assigning it a song number
}
答案 0 :(得分:0)
你正在使用某种MP3播放器,由于找不到匹配的导入语句,我无法跟踪它是什么:
player = new MP3(input);
player.play();
最常见的事情是向播放器添加一个监听器(如果你的MP3类支持),如
player.addXXXListener(new XXXListener(){});
XXXlistener会有一个在完成播放后被调用的方法,在该方法中你可以开始下一首歌。
所以这取决于你的MP3类支持的内容。