写了一个简单的MP3,用一个线程完成从文件中读出歌词,无法显示是怎么回事?

RT,原来用一个函数完成歌词的读取,可以显示,现在我用一个线程来完成同样的功能,代码都是完全从那个函数中复制过去的,结果无法显示,控制台中也无法输出,求解
以下是代码:
package app;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Vector;

import javax.media.Manager;
import javax.media.NoPlayerException;
import javax.media.Player;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class PlayFrame extends JFrame implements Runnable {
Player player;
Vector playlist;
Thread readlyric;
BufferedReader br;
String filedirectory = "file:1.mp3";
int position = 0;

JLabel musicname;
JTextArea lyric;
JPanel panellyric, panelbutton;
JButton playmusic, pausemusic, next, last;

Font x = new Font("Serif", Font.ITALIC + Font.BOLD, 16);

public PlayFrame() {
    try {
        br = new BufferedReader(new InputStreamReader(new FileInputStream(
                "1.lrc"), "UTF-8"));
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    playlist = new Vector();
    playlist.add("file:1.mp3");
    playlist.add("file:2.mp3");

    this.setTitle("MP3");
    this.setBounds(400, 100, 350, 550);

    musicname = new JLabel("Music_Name", JLabel.CENTER);
    musicname.setFont(x);
    panellyric = new JPanel();
    panelbutton = new JPanel();

    lyric = new JTextArea(25, 25);
    lyric.setEditable(false);
    lyric.setBackground(Color.WHITE);
    lyric.setLineWrap(true);// 确定是否能在右边界自动换行

    last = new JButton("Last");
    playmusic = new JButton("Play");
    pausemusic = new JButton("Pause");
    next = new JButton("Next");

    playmusic.addActionListener(play_button);
    pausemusic.addActionListener(stop_button);
    next.addActionListener(next_button);
    last.addActionListener(last_button);

    panellyric.add(lyric);
    panelbutton.add(last);
    panelbutton.add(playmusic);
    panelbutton.add(pausemusic);
    panelbutton.add(next);

    this.add(musicname, BorderLayout.NORTH);
    this.add(panellyric, BorderLayout.CENTER);
    this.add(panelbutton, BorderLayout.SOUTH);
    this.setVisible(true);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}


public ActionListener play_button = new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        display();
        //read();
        musicname.setText(filedirectory);
    }
};

public ActionListener stop_button = new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        pause();
    }
};

public ActionListener next_button = new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        next();
    }
};

public ActionListener last_button = new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        last();
    }
};

public void display() {

    try {
        player = Manager.createPlayer(new URL(filedirectory));
        readlyric=new Thread();
    } catch (NoPlayerException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    player.start();
    readlyric.start();

}

public void pause() {
    player.stop();
    readlyric.stop();
}

public void next() {
    pause();
    if (position < playlist.size()) {
        filedirectory = playlist.get(++position).toString();
    } else {
        position = 0;
        filedirectory = playlist.get(position).toString();
    }
    display();
}

public void last() {
    pause();
    if (position > 0) {
        filedirectory = playlist.get(--position).toString();
        display();
    } else {

    }
}

@Override
public void run() {
    // TODO Auto-generated method stub
    System.out.println("enter method run");
    int lastTime = 0;
    int minute, second, milliSecond, total;
    String str = "", time, words;

    try {
        while ((str = br.readLine()) != null) {
            // 获取'[]'中的时间字符串
            time = str.substring(str.indexOf('[') + 1, str.indexOf(']'));
            minute = Integer.parseInt(time.substring(0, time.indexOf(':')));
            second = Integer.parseInt(time.substring(time.indexOf(':') + 1,
                    time.indexOf('.')));
            milliSecond = Integer
                    .parseInt(time.substring(time.indexOf(".") + 1));
            // 计算总时间
            total = minute * 60 * 1000 + second * 1000 + milliSecond * 10;

            Thread.sleep(total - lastTime);

            System.out.println("get lyric");
            // 将歌词前面的时间信息去掉,在控制台输出歌词
            words = str.substring(str.indexOf("]") + 1);

            System.out.println(words);
            lyric.append(words);
            lyric.append("\n");
            // 立即绘制指定区域
            lyric.paintImmediately(lyric.getBounds());

            lastTime = total;
        }
    } catch (NumberFormatException | IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

// public void read() {
//
// int lastTime = 0;
// int minute, second, milliSecond, total;
// String str = "", time, words;
//
// try {
// while ((str = br.readLine()) != null) {
// // 获取'[]'中的时间字符串
// time = str.substring(str.indexOf('[') + 1, str.indexOf(']'));
// minute = Integer.parseInt(time.substring(0, time.indexOf(':')));
// second = Integer.parseInt(time.substring(time.indexOf(':') + 1,
// time.indexOf('.')));
// milliSecond = Integer
// .parseInt(time.substring(time.indexOf(".") + 1));
// // 计算总时间
// total = minute * 60 * 1000 + second * 1000 + milliSecond * 10;
//
// Thread.sleep(total - lastTime);
//
// // 将歌词前面的时间信息去掉,在控制台输出歌词
// words = str.substring(str.indexOf("]") + 1);
//
// System.out.println(words);
// lyric.append(words);
// lyric.append("\n");
// // 立即绘制指定区域
// lyric.paintImmediately(lyric.getBounds());
//
// lastTime = total;
// }
// } catch (NumberFormatException | IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// }

public static void main(String[] args) {

    PlayFrame playframe = new PlayFrame();

}

}

2个回答

调试下,线程中的代码有没有被执行

xhw951122
xhw951122 好像没有进入run方法,这是为什么?
5 年多之前 回复

按上播放按键(play_button)后,还是没有歌词吗?
建议你:
1)确定歌词文件 LRC 是否存在?
2)歌词文件的路径名生成是否正确?

91program
91program 获取歌词是解析 br 变量的,你调试一下看看进入线程时此变量的值是否正确呢?
5 年多之前 回复
xhw951122
xhw951122 我的lyc文件是放在工程里的,开始用一个函数实现可以在控制台输出,现在改用线程来写,连控制台都输出不了了
5 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐