gpt4 book ai didi

java - jlayer.javazoom播放器无法停止mp3

转载 作者:行者123 更新时间:2023-12-01 11:46:18 25 4
gpt4 key购买 nike

如何在jlayer中停止MP3? (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();
}
}

最佳答案

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();
}

}
}

关于java - jlayer.javazoom播放器无法停止mp3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29104942/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com