0

如何在 jlayer 中停止 MP3?(the stop()不再使用)

我的代码如下:

//main class mp3_main

    private AdvancedPlayer player;

    public static void main(String[] args) {

    String file="C:\\cd.mp3";
    mp3PlayerSample mp3 = new mp3PlayerSample(file);

    mp3.play();
    mp3.stop();

}


//class mp3PlayerSample

private String filename;
private BufferedInputStream buffer;
private AdvancedPlayer player;
//constructor
public mp3PlayerSample(String filename) 
{
    this.filename = filename;
}

//start method
public void play() 
{
    FileInputStream fis;
    try {
        fis = new FileInputStream(this.filename);
        buffer = new BufferedInputStream(fis);
        try {
            this.player=new AdvancedPlayer(buffer);
            player.play();
        } catch (JavaLayerException e) {
            e.printStackTrace();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}
//stop method
public void stop()
{
     if(player != null){
          player.close();
     } 
}
4

2 回答 2

1

您需要在自己的线程中运行播放器,现在您的 main 方法在调用 play() 之后阻塞,直到播放完成。

请注意,jlayer 中包含的 Player/AdvancedPlayer 类是作为示例代码来演示如何连接解码音频的解码和输出。他们不是完全成熟的播放器(例如,甚至没有音量控制)。

于 2015-03-17T17:02:12.973 回答
0
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;

import java.util.Scanner;

class Music extends Thread{

        public void run(){


                try {
                        FileInputStream fileInputStream = new FileInputStream("Freedom.mp3");
                        Player player = new Player(fileInputStream);
                        player.play();

                }catch(FileNotFoundException e) {
                    e.printStackTrace();
                }catch(JavaLayerException e) {
                    e.printStackTrace();
                }

        }   

}

public class Main {

    public static void main (String[]args){

        Scanner keyboard = new Scanner(System.in);

        Music music = new Music();
        music.start();

        System.out.println("Stop music: ");
            int off = keyboard.nextInt();

        if(off == 0) {
            music.stop();
        }

    }
}
于 2017-09-12T17:32:07.533 回答