MP3 음악 파일 재생

3260 단어 자바음악.
package com.imooc.test;


import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javazoom.jl.decoder.Bitstream;
import javazoom.jl.decoder.BitstreamException;
import javazoom.jl.decoder.Header;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;

/**
 * 
 * @ClassName: Mp3Player
 * @Description: TODO(  .mp3     )
 * @author Long
 * @date 2015 9 15    3:02:09
 */
public class Mp3Player {

	/**
	 * 
	 * @Title: getMp3Time
	 * @Description: TODO(  mp3       )
	 * @author fyf
	 * @return time(00:00)
	 */
	public String getMp3Time(String mp3FilePath) {

		try {
			File file = new File(mp3FilePath);

			if (file.exists() && file.isFile()) {
				FileInputStream fis = new FileInputStream(file);
				int bytes = fis.available();

				Bitstream bit = new Bitstream(fis);
				Header header = bit.readFrame();
				long time = (long) header.total_ms(bytes);

				return formatTime(time);
			}
		} catch (IOException | BitstreamException e) {
			System.out.println(e);
		}

		return null;
	}

	/**
	 * 
	 * @Title: formatTime
	 * @Description: TODO(     :mm:ss; : )
	 * @author fyf
	 * @return time(00:00)
	 */
	public String formatTime(long time) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("mm:ss");

		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(time);

		return dateFormat.format(calendar.getTime());
	}

	/**
	 * 
	 * @Title: playerMp3
	 * @Description: TODO(  mp3)
	 * @author fyf
	 * @param mp3File
	 *            mp3    
	 */
	public void playerMp3(String mp3FilePath) {
		BufferedInputStream buffer = null;

		/**
		 *        
		 */
		try {
			buffer = new BufferedInputStream(new FileInputStream(mp3FilePath));
		} catch (Exception e) {
			try {
				buffer = new BufferedInputStream(new URL(mp3FilePath).openStream());
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}

		try {
			Player player = new Player(buffer);
			player.play();
		} catch (JavaLayerException e) {
			e.printStackTrace();
		}

	}

	public static void main(String[] args) {
		Mp3Player mp3Player = new Mp3Player();
		//      
		String mp3FilePath = "D:\\CloudMusic\\    .mp3";
		String mp3Time = mp3Player.getMp3Time(mp3FilePath);
		System.out.println("        :"+mp3Time);
		mp3Player.playerMp3(mp3FilePath);

		//       
		mp3FilePath = "http://music.baidutt.com/up/kwcawscw/yyaumy.mp3";
		mp3Player.playerMp3(mp3FilePath);
		

	}
}


 가방 "jl 1.0.1. jar" 를 가 져 와 야 합 니 다.

좋은 웹페이지 즐겨찾기