java通过url播放远程mp3及获取播放时长

JAVA新手,根据客户要求,需要在程序中加入一个播放远程Mp3的功能,在网上找了一圈,发现都是播放本地mp3,后来发现可以通过URLConnection的getInputStream方法获取流,特地记录下来,此功能需要jl包,下载jl1.0。

下边是实现思路

public class ReadMp3 {
	private String _songName;
	ReadMp3(String songName) throws IOException, Exception{
		_songName = songName;
		URL url = new URL("http://fs.w.kugou.com/201901082013/9ec2463ebd15378341c24d982ccf183f/G009/M00/16/13/SQ0DAFUWmN-ALX4LADv-LPZ8GHs526.mp3");
		URLConnection con = null;
		try{
			con = url.openConnection();
		}catch(IOException e){
			e.printStackTrace();
		}
		
		BufferedInputStream bis = new BufferedInputStream(con.getInputStream());
		Bitstream bt = new Bitstream(bis);
		
                //获取mp3时间长度
		Header header = bt.readFrame();
                int mp3Length = con.getContentLength();
		int time = (int) header.total_ms(mp3Length);
		System.out.println(time / 1000);

                Player player = new Player(bis);
		player.play();
	}
	
	public static void main(String[] args) throws Exception{
		ReadMp3 mp3 = new ReadMp3("贝加尔湖畔");
	}
}

播放本地mp3

public class PlayLocalMp3{
	private String _songName;
	
	PlayLocalMp3(String songName) throws IOException, Exception{
		_songName = songName;
		
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File(_songName)));
		Player player = new Player(bis);
		player.play();
	}
	
	public static void main(String[] args) throws IOException, Exception{
		PlayLocalMp3 playMp3 = new PlayLocalMp3("C:\\Users\\ll\\Downloads\\beijiaerhupan.mp3");
	}
	
}

 

你可能感兴趣的:(java)