JAvaはurlでリモートmp 3を再生し、再生時間を取得する
1654 ワード
JAVA初心者は、お客様の要求に応じて、プログラムにリモートMp 3を再生する機能を追加する必要があります.ネット上で一周してみると、すべてローカルmp 3を再生していることがわかりました.その後、URLConnectionのgetInputStream方法でストリームを取得できることがわかりました.わざわざ記録しておきます.この機能はjlパッケージが必要で、jl 1.0をダウンロードします.
下は実現の構想だ
ローカルmp 3を再生
下は実現の構想だ
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(" ");
}
}
ローカルmp 3を再生
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");
}
}