java实现播放mp3功能

  1.首先引入jlayer.jar

<!-- https://mvnrepository.com/artifact/javazoom/jlayer -->
<dependency>
    <groupId>javazoom</groupId>
    <artifactId>jlayer</artifactId>
    <version>1.0.1</version>
</dependency>

  2. 代码如下

  

package com.xinfushe.taskbot.util;

import java.io.BufferedInputStream;
import java.io.FileInputStream;

import javazoom.jl.player.Player;

/**
 * MP3播放器
 *
 * @author lifq
 *
 * 2017年4月7日  下午5:29:57
 */
public class MP3Player {
    
    private String filename;
    private Player player;
    
    public MP3Player(String filename) {
        this.filename = filename;
    }

    public void play() {
        try {
            BufferedInputStream buffer = new BufferedInputStream(
                    new FileInputStream(filename));
            player = new Player(buffer);
            player.play();
        } catch (Exception e) {
            System.out.println(e);
        }
    }


    public static void main(String[] args) {
        MP3Player mp3 = new MP3Player("d:/2.mp3");
        mp3.play();
    }
}

 

posted @ 2017-04-11 15:23  Java碎碎念  阅读(...)  评论(...编辑  收藏