前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java实现MP3

java实现MP3

作者头像
py3study
发布2020-01-08 17:06:01
2K0
发布2020-01-08 17:06:01
举报
文章被收录于专栏:python3python3

下载MpegAudioSPI1.9.4

http://www.javazoom.net/mp3spi/mp3spi.html

在项目中添加mp3spi1.9.4.jar  jl1.0.jar  tritonus_share.jar三个jar包

copy下面的代码到类中!

package org.mp3;

import java.io.File;

import javax.sound.sampled.AudioFormat;

import javax.sound.sampled.AudioInputStream;

import javax.sound.sampled.AudioSystem;

import javax.sound.sampled.DataLine;

import javax.sound.sampled.SourceDataLine;

public class TestMP3 {

    boolean isStop = true;// 控制播放线程

    boolean hasStop = true;// 播放线程状态

    AudioInputStream audioInputStream;// 音频文件流

    AudioFormat audioFormat;// 文件格式

    SourceDataLine sourceDataLine;// 输出设备

// 播放

private void play() {

        try {

            isStop = true;// 停止播放线程

            // 等待播放线程停止

            while (!hasStop) {

                System.out.print(".");

                try {

                    Thread.sleep(10);

                } catch (Exception e) {

                }

            }

            System.out.println("");

            File file = new File("/home/mayi/11.mp3");//linux路径

            // 取得文件输入流

            audioInputStream = AudioSystem.getAudioInputStream(file);

            audioFormat = audioInputStream.getFormat();

            // 转换mp3文件编码

            if (audioFormat.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) {

                audioFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,

                        audioFormat.getSampleRate(), 16, audioFormat

                                .getChannels(), audioFormat.getChannels() * 2,

                        audioFormat.getSampleRate(), false);

                audioInputStream = AudioSystem.getAudioInputStream(audioFormat,

                        audioInputStream);

            }

            // 打开输出设备

            DataLine.Info dataLineInfo = new DataLine.Info(

                    SourceDataLine.class, audioFormat,

                    AudioSystem.NOT_SPECIFIED);

            sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo);

            sourceDataLine.open(audioFormat);

            sourceDataLine.start();

            // 创建独立线程进行播放

            isStop = false;

            Thread playThread = new Thread(new PlayThread());

            playThread.start();

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

    //线程!!

    class PlayThread extends Thread {

        byte tempBuffer[] = new byte[320];

        public void run() {

            try {

                int cnt;

                hasStop = false;

                // 读取数据到缓存数据

                while ((cnt = audioInputStream.read(tempBuffer, 0,

                        tempBuffer.length)) != -1) {

                    if (isStop)

                        break;

                    if (cnt > 0) {

                        // 写入缓存数据

                        sourceDataLine.write(tempBuffer, 0, cnt);

                    }

                }

                // Block等待临时数据被输出为空

                sourceDataLine.drain();

                sourceDataLine.close();

                hasStop = true;

            } catch (Exception e) {

                e.printStackTrace();

                System.exit(0);

            }

        }

    }

    public static void main(String args[]) {

        TestMP3 test=new TestMP3();

        test.play();

    }

}

运行!  OK    有空研究研究 audiospl的源码!!

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-03 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档