如何在java应用程序中播放音频文件

如何在java应用程序中播放音频文件,第1张

java里可以使用AudioClip 来播放音频文件

API参考内容如下

AudioClip 接口是用于播放音频剪辑的简单抽象。多个 AudioClip 项能物扰够同时凯蚂差播放,得到的声音混合在一起可产生合成声音。

从以下版本开始:

JDK1.0

方法摘要

void    loop()

以循环方式开始播放此音频剪辑。  

void    play()

开始播放此音频剪辑。  

void    stop()

停止播放此音频剪盯皮辑。  

当然了, 如果播放比较复杂的音频视频, 那么可以使用java媒体框架 JMF

首先下载播放mp3的包,比如mp3spi1.9.4.jar。在工程中添加这个包。

播放器演示代码如下

package com.test.audio

import java.io.File

import java.awt.BorderLayout

import java.awt.FileDialog

import java.awt.Frame

import java.awt.GridLayout

import java.awt.Label

import java.awt.List

import java.awt.Menu

import java.awt.MenuBar

import java.awt.MenuItem

import java.awt.MenuShortcut

import java.awt.Panel

import java.awt.event.ActionEvent

import java.awt.event.ActionListener

import java.awt.event.KeyEvent

import java.awt.event.MouseAdapter

import java.awt.event.MouseEvent

import java.awt.event.WindowAdapter

import java.awt.event.WindowEvent

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 MusicPlayer extends Frame {

    /**

 * 

 */

private static final long serialVersionUID = -2605658046194599045L

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

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

 

    String filepath// 播放文件目录

    String filename// 播放文件名称

    AudioInputStream audioInputStream// 文件流

    AudioFormat audioFormat// 文件格式

    SourceDataLine sourceDataLine// 输出设备

 

    List list// 文件列表

    Label labelfilepath//播放目录显示标签

    Label labelfilename//播放文件游宴显示标签

 

    public MusicPlayer() {

        // 设置窗体属性

        setLayout(new BorderLayout())

        setTitle("MP3 Music Player")

        setSize(350, 370)

 

        // 建立菜单栏

        MenuBar menubar = new MenuBar()

        Menu menufile = new Menu("File")

        MenuItem menuopen = new MenuItem("Open", new MenuShortcut(KeyEvent.VK_O))

        menufile.add(menuopen)

 闹磨正       menufile.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                open()

            }

        })

        menubar.add(menufile)

        setMenuBar(menubar)

 

        // 文件列表

        list = new List(10)

        list.addMouseListener(new MouseAdapter() {

            public void mouseClicked(MouseEvent e) {

                // 双击时处理

                if (e.getClickCount() == 2) {

                    // 播放选中的文件

                    filename = list.getSelectedItem()

                    play()

                }

            }

        })

        add(list, "Center")

 

        // 信息显示

        Panel panel = new Panel(new GridLayout(2, 1))

        labelfilepath = new Label("Dir:")

        labelfilename = new Label("File:")

        panel.add(labelfilepath)

        panel.add(labelfilename)

        add(panel, "North")

 

        // 注册窗体关闭事件

        addWindowListener(new WindowAdapter() {

            public void windowClosing(WindowEvent e) {

                System.exit(0)

            }

        })

        setVisible(true)

    }

 

    // 打开

    private void open() {

        FileDialog dialog = new FileDialog(this, "Open", 0)

        dialog.setVisible(true)

        filepath = dialog.getDirectory()

        if (filepath != null) {

            labelfilepath.setText("Dir:" + filepath)

 

            // 显示文件列表

            list.removeAll()

            File filedir = new File(filepath)

            File[] filelist = filedir.listFiles()

            for (File file : filelist) {

                String filename = file.getName().toLowerCase()

                if (filename.endsWith(".mp3") || filename.endsWith(".wav")) {

                    list.add(filename)

                }

            }

        }

    }

 

    // 播放

    private void play() {

        try {

            isStop = true// 停止播放线程

            // 等待播放线程停止

            System.out.print("Start:" + filename)

            while (!hasStop) {

                System.out.print(".")

                try {

                    Thread.sleep(10)

                } catch (Exception e) {

                }

            }

            System.out.println("")

            File file = new File(filepath + filename)

            labelfilename.setText("Playing:" + filename)

 

            // 取得文件输入流

            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[]) {

        new MusicPlayer()

    }

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12491655.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-25
下一篇 2023-05-25

发表评论

登录后才能评论

评论列表(0条)

保存