如何使用Java代码和JSP从浏览器播放MP3?

时间:2014-02-06 05:44:51

标签: java html jsp mp3

我需要的是从JSP播放MP3文件,但我不知道如何。 这是我的Java MP3播放器的代码,它的工作正常,问题是我需要在我的浏览器上播放音乐(像Web服务这样的东西)

package reproductor;
import java.io.File;
import javazoom.jlgui.basicplayer.BasicPlayer;

public class Reproductor {
public BasicPlayer player;

public Reproductor() {
    player = new BasicPlayer();
}
public void coge(String y){

}
public void Play() throws Exception {
    player.play();
}

public void AbrirFichero(String ruta) throws Exception {
    player.open(new File(ruta));
    player.play();
}

public void Pausa() throws Exception {
    player.pause();
}

public void Continuar() throws Exception {
    player.resume();
}

public void Stop() throws Exception {
    player.stop();
}
public void reproducemp3 () throws Exception{
   try {
    Reproductor   mi_reproductor = new Reproductor();
        mi_reproductor.AbrirFichero("C:/Users/Welrk/Downloads/preview.mp3");
        mi_reproductor.Play();
    } catch (Exception ex) {
        System.out.println("Error: " + ex.getMessage());
    }
}
public static void main(String args[]) throws Exception{
 Reproductor y = new Reproductor();
 y.AbrirFichero("C:/Users/Welrk/Downloads/preview.mp3");
 y.Play();
}
}  

我试过这样的事情,但我真的输了,你能提供的任何帮助都会很棒

<%@page import="java.io.*" %>
<%@ page import="reproductor.*" %>
<%@ page import="reproductor.main"%>
<%@page import="javazoom.jlgui.basicplayer.BasicPlayer" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
    <%         
        String temp="C:/Users/Welrk/Documents/NetBeansProjects/Musica/web/preview.mp3";        
        Reproductor object = new Reproductor();
        object.player.open(new File(temp));           
    %>
    </body>
</html>

1 个答案:

答案 0 :(得分:0)

除非您的要求不同(您被迫使用JSP播放器),为什么不依赖HTML5音频控件reference here

<audio controls>
 <source src="/pathtoyourfile.mp3" type="audio/mpeg">
</audio>