【实例简介】有图形用户界面、能根据本地路径导入本地音乐,项目中含jmf相关 jar包下载

【实例截图】

【核心代码】

import java.io.*;

//import java.applet.AudioClip;

import java.awt.BorderLayout;

import java.awt.Button;

import java.awt.Color;

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.Panel;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import javax.sound.sampled.AudioFileFormat;

import javax.sound.sampled.AudioFormat;//获得音频文件中包含的音频数据的格式。

import javax.sound.sampled.AudioInputStream;//通过转换提供的音频输入流,获得所指示格式的音频输入流。

import javax.sound.sampled.AudioSystem;

//import javax.sound.sampled.Clip;

import javax.sound.sampled.DataLine;

import javax.sound.sampled.SourceDataLine;

import javax.sound.sampled.UnsupportedAudioFileException;

import javax.swing.JOptionPane;

//import com.sun.javafx.collections.MappingChange.Map;

//

//import javazoom.jl.decoder.Manager;

//import javazoom.jl.player.Player;

//import javax.media.*;

//

//import javax.media.bean.*;

//import javax.management.*;

//import javax.media.bean.playerbean.*;

//import java.time.*;

//这里是标记

@SuppressWarnings("unused")

public class SoundPlayerTest extends Frame{

private static final long serialVersionUID = 1L;

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

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

String filepath;// 播放文件目录

String filename;// 播放文件名称

AudioInputStream audioInputStream;// 文件流

AudioFormat audioFormat;// 文件格式

SourceDataLine sourceDataLine;// 输出设备

List list;// 文件列表

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

Label labelfilename;//播放文件显示标签

Button buttonNext,buttonLast,buttonStop,showlist;//上、下一首、停止按钮,保存当前当前打开文件列表的音乐

int songNum,num,i,f=0;

int judge=0;

//String F[]=new String[10];

public SoundPlayerTest() throws IOException {

// 设置窗体属性

setLayout(new BorderLayout());

setTitle("音月");

setSize(380,500);

// 建立菜单栏

MenuBar menubar = new MenuBar();

Menu menufile = new Menu("菜单");

MenuItem menuopen = new MenuItem("打开");

MenuItem itemExit=new MenuItem("退出");

menufile.add(menuopen);

menufile.addSeparator();

menufile.add(itemExit);

menuopen.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

try {

open();

} catch (IOException e1) {

// TODO Auto-generated catch block

//e1.printStackTrace();

}

}

});

itemExit.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

System.exit(0);

}

});

menubar.add(menufile);

setMenuBar(menubar);

// 文件列表

list = new List();

list.addMouseListener(new MouseAdapter() {

public void mouseClicked(MouseEvent e) {

// 双击时处理

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

// 播放选中的文件

filename = list.getSelectedItem();

songNum=list.getSelectedIndex();

num=songNum;

// System.out.printf("位置%d\n",songNum);

play();

}

}

});

list.setBackground(Color.pink);

add(list, "Center");

// 信息显示

Panel panel = new Panel(new GridLayout(0, 1));

labelfilepath = new Label("正在播放:");

labelfilename = new Label("播放列表");

buttonLast=new Button("上一首");

buttonLast.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

songNum=list.getSelectedIndex();

if(songNum>0){

list.select(songNum-1);

filename = list.getItem(songNum-1);

num=songNum-1;

play();

}

}

});

//****************************************

buttonStop=new Button("停止");

buttonStop.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

isStop=true;

labelfilepath.setText("播放停止");

}

});

//

//****************************************

showlist=new Button("导入");

showlist.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

//歌曲显示

list.removeAll();

String w = "音乐路径.txt";

BufferedReader in = null;

try {

in = new BufferedReader( new FileReader(w));

} catch (FileNotFoundException e2) {

// TODO Auto-generated catch block

//e2.printStackTrace();

}

String nowid = null;

try {

nowid = in.readLine();

} catch (IOException e2) {

// TODO Auto-generated catch block

//e2.printStackTrace();

}

labelfilepath.setText(nowid);

while(nowid!=null)

{

//F[f]=nowid;

filepath=nowid;

if (filepath != null) {

labelfilepath.setText("音乐路径:" filepath);

File filedir = new File(filepath);

File[] filelist = filedir.listFiles();//过滤该路径所在文件是wav,MP3格式的文件并在面板中显示

for (File file : filelist) {

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

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

list.add(filename);

}

}

}

//f ;

try {

nowid=in.readLine();

} catch (IOException e1) {

// TODO Auto-generated catch block

//e1.printStackTrace();

}

}

try {

in.close();

} catch (IOException e1) {

// TODO Auto-generated catch block

//e1.printStackTrace();

}

}

});

//*****************************************

buttonNext=new Button("下一首");

buttonNext.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

songNum=list.getSelectedIndex();

if((songNum 1)

list.select(songNum 1);

filename = list.getItem(songNum 1);

num=songNum 1;

play();

}

}

});

buttonNext.setBackground(Color.ORANGE);

buttonLast.setBackground(Color.GREEN);

buttonStop.setBackground(Color.GRAY);

showlist.setBackground(Color.blue);

list.setBackground(Color.white);

panel.add(labelfilepath);

panel.add(buttonLast);

panel.add(buttonStop);

panel.add(buttonNext);

panel.add(showlist);

panel.add(labelfilename);

add(panel, "North");

// 注册窗体关闭事件

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

System.exit(0);

}

});

setVisible(true);

}

// 打开

private void open() throws IOException {

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

dialog.setVisible(true);

boolean m=true;

String w = "音乐路径.txt";

BufferedReader in=new BufferedReader( new FileReader(w));

String nowid=in.readLine();

filepath = dialog.getDirectory();//保存对话框的路径

while(nowid!=null)

{

if(nowid.equals(filepath))

{

JOptionPane.showMessageDialog(null, "该路径已保存!"); //弹出提示框

m=false;

break ;

}

nowid=in.readLine();

}

if(m){

RandomAccessFile randomFile = new RandomAccessFile(w, "rw");

long fileLength = randomFile.length(); //将写文件指针移到文件尾。

randomFile.seek(fileLength);

randomFile.writeBytes(filepath "\r\n");

randomFile.close();

}

if (filepath != null) {

labelfilepath.setText("音乐路径:" filepath);

String song;

song=filepath ".txt";

BufferedWriter out=new BufferedWriter( new FileWriter(song));

// System.out.println("新建成功标记");

// 显示文件列表

File filedir = new File(filepath);

File[] filelist = filedir.listFiles();//过滤该路径所在文件是wav,MP3格式的文件并在面板中显示

for (File file : filelist) {

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

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

list.add(filename);

out.write(filename);

out.newLine();

}

}

out.close();

}

}

// 播放

private void play() {

try {

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

// 等待播放线程停止

//System.out.print("开始播放:" filename);

while (!hasStop) {

//System.out.print(".");

try {

Thread.sleep(10);

} catch (Exception e) {

}

}

//System.out.println("");

//进行路径的选择判断

String flag=null;

String M[]=new String[5];

int m=0;

String w = "音乐路径.txt";

BufferedReader in=new BufferedReader( new FileReader(w));

String nowid=in.readLine();

while(nowid!=null)

{

M[m]=nowid;

nowid=in.readLine();

m ;

}

in.close();

for(int i=0;i

{

String ww = M[i] ".txt";

BufferedReader nn=new BufferedReader( new FileReader(ww));

String nowd=nn.readLine();

while(nowd!=null)

{

if( nowd.equals(filename) ){

filepath =M[i];

break;

}

else

nowd= nn.readLine();

}

nn.close();

}

File file = new File(filepath filename);

//文件长度

long minute = 0,second = 0;

long total = 0;

try {

AudioFileFormat aff = AudioSystem.getAudioFileFormat(file);

java.util.Map props = aff.properties();

if (props.containsKey("duration"))

total = (long) Math.round((((Long) props.get("duration"))

.longValue())/1000);

//System.out.println("这里显示的是播放的时长\n");

minute=total/60000;

second=total/1000-minute*60;

} catch (UnsupportedAudioFileException e) {

// TODO Auto-generated catch block

//e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

//e.printStackTrace();

}

labelfilepath.setText("正在播放:" filename " " "共时长" minute "分 " second "秒");

// 取得文件输入流

audioInputStream = AudioSystem.getAudioInputStream(file);

audioFormat = audioInputStream.getFormat();

// 转换wav文件编码

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();

//playThread.join();

//System.out.printf("\n这里2是%d",judge);

} catch (Exception e) {

//e.printStackTrace();

}

}

public static void main(String args[]) throws IOException {

new SoundPlayerTest();

}

private void clcle(){

if((num 1)

list.select(num 1);

filename = list.getItem(num 1);

hasStop = false;

num=songNum 1;

songNum=num;

System.out.printf("\n这是songnum的显示位置\n%d",songNum);

System.out.printf("\n这是num的显示位置\n%d",num);

play();

}

}

// 播放线程

class PlayThread extends Thread {

private boolean iss;

byte tempBuffer[] = new byte[320];

public void run() {

try {

int cnt;

judge=0;

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;

iss=true;

System.out.println("1" hasStop "\n");

System.out.println("iss" iss "\n");

System.out.println(hasStop);

if(hasStop)

clcle();

}

catch (Exception e) {

e.printStackTrace();

//System.exit(0);

}

//finally{

//if(hasStop)

// clcle();

// }

}

}

}

java音乐播放器代码_java 音乐播放器 示例源码(亲测可用)相关推荐

  1. java超市买东西代码_java超市购物系统源代码(源码大小10M)

    java超市购物系统源代码(源码大小10M) 本站提供几百套大型商业源码,平均一元一套,火爆下载中...... QQ:283072.283672 EMAIL:web@hur.cn.jhwjeffrey ...

  2. android指南针校准 代码_android 指南针app源码(亲测可用)

    [实例简介] [实例截图] [核心代码] package cn.icast.zhinanzhen; import android.app.Activity; import android.conten ...

  3. java音频播放器代码_java音乐播放器实现代码

    本文实例为大家分享了java音乐播放器的具体代码,供大家参考,具体内容如下 这个是源码结构介绍 这个是界面,有点简陋,见笑了,但是基本上的东西都有了,没办法,没有美工的程序写的界面 直接上源代码Pla ...

  4. java实验项目代码_java web 期末项目实验源码20套,自用学习非常不错!

    分享java web 期末项目实验源码20套,自用学习非常不错! 我自己也从里面学习到了很多东西! 1.BBS论坛系统(jsp+sql) 2.ERP管理系统(jsp+servlet) 3.OA办公自动 ...

  5. java抢答器代码_java竞争抢答器

    /*实现一个竞拍抢答程序:要求设置三个抢答者(三个线程),而后同时发出抢答指令,抢答成功者显示成功提示,抢答失败者给出失败提示.*/ package Java多线程_01; import java.u ...

  6. java 解压文件_java实现解压zip文件,(亲测可用)!!!!!!

    项目结构: Util.java内容: package com.cfets.demo; import java.io.File; import java.io.FileOutputStream; imp ...

  7. java求sobel算子代码_sobel算子原理及opencv源码实现

    sobel算子原理及opencv源码实现 简要描述 sobel算子主要用于获得数字图像的一阶梯度,常见的应用和物理意义是边缘检测. 原理 算子使用两个33的矩阵(图1)算子使用两个33的矩阵(图1)去 ...

  8. 打电话android代码,android 拨打电话例子源码(亲测可用)

    [实例简介] [实例截图] [核心代码] package androidCall.pack; import java.util.regex.Matcher; import java.util.rege ...

  9. android手机 代码下载,android手机NFC 示例源码下载

    手机NFC测试软件 资源下载此资源下载价格为2D币,请先登录 资源文件列表 TestNFC/.DS_Store , 6148 TestNFC/._.DS_Store , 4096 TestNFC/.c ...

  10. java读取微信消息_android 读取微信聊天内容 示例源码(AccessibilityService)

    [实例简介] 微信聊天界面的布局,查看方法: AndroidStudio--Tools--Android--Android Device Monitor [实例截图] [核心代码] public cl ...

最新文章

  1. Git Merge VS Rebase
  2. 大系统化小之后,微信如何解决大规模微服务下的难题?
  3. TLS1.3 协议的Golang 实现——ClientHello
  4. LInux命令行参数
  5. 时间自适应卷积:比自注意力更快的特征提取器
  6. 使用 SourceTree 操作时弹出 password required
  7. Atitit 引流矩阵与矩阵引流 推广方法 attilax总结
  8. qimage加载jpg失败_QImage基本api
  9. 第18集丨不立志,天下无可成之事
  10. Android面试准备复习之Android知识点大扫描 .
  11. 【OpenGL ES】纹理贴图
  12. 修改Sublime Text 默认*.sublime-package文件
  13. Flink Interval Join Left Join
  14. 埃拉托色尼筛选法(Eratosthenes Sieve)分析
  15. 面试笔记:面经-猿辅导-一面
  16. python之爬虫神器selenium:猫眼电影榜单并进行数据可视化
  17. Pr CC 2017安装一直提示缺少MSVCR120.dll
  18. 拉噗拉司金字塔LaplacianPyramid学习笔记(一半章子怡 + 一半孙俪)
  19. 开关电源主滤波电解电容器屡爆原因及预防
  20. 报税系统服务器地址怎么填,报税上海服务器地址

热门文章

  1. 芯片可靠性与商规、工规、车规
  2. 《Linux设备驱动开发详解 A》一一3.1 Linux内核的发展与演变
  3. 知乎|10个程序员必备免费电子书下载网站
  4. python无限循环怎么停止,如何在Python中安全地停止无限循环?
  5. 「图与推荐指南针」: 顶级学者/研究组有哪些?
  6. [C语言] PTA 7-55 查询水果价格
  7. 利用日志管理,溯源追踪解决安全问题
  8. python plot_surface 画表面网格图
  9. java 生成随机加减乘除_用Java随机生成四则运算
  10. JS清除IE浏览器缓存的方法