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

本文介绍了如何使用Java实现一个简单的音乐播放器,包括播放、暂停、停止功能以及切换音乐。文章提供了核心类Player的详细代码,涵盖播放音乐、控制播放状态和界面交互。此外,还讨论了可能遇到的问题,如音乐信息获取、界面设计和打包成jar文件的路径问题。
摘要由CSDN通过智能技术生成

本文实例为大家分享了java音乐播放器的具体代码,供大家参考,具体内容如下

这个是源码结构介绍

edef26f59123e8838b5425ddd12073aa.png

这个是界面,有点简陋,见笑了,但是基本上的东西都有了,没办法,没有美工的程序写的界面

658d970ce0373354be4ab2f214348acf.png

直接上源代码Player.java

package com.service;

import java.io.File;

import java.io.IOException;

import java.util.ArrayList;

import java.util.Random;

import javax.sound.sampled.*;

import javax.swing.JSlider;

import javax.swing.JTable;

import com.list.MusicList;

import com.list.ThreadList;

import com.list.ViewList;

import com.model.Model;

import com.model.Music;

import com.view.View;

/*"duration"

"author"

"title"

"copyright"

"date"

"comment"*/

public class Player extends Thread{

private Player p;

private long time = 0;

Object lock = new Object();//一个空的对象,没什么意义

private boolean paused = false;// 暂停 继续

public boolean isPaused() {

return paused;

}

public void setPaused(boolean paused) {

this.paused = paused;

}

private JSlider jSliderPlayProgress;//播放进度条

private boolean over = false;//开始 结束

//是否自动播放下一曲

private boolean isNext=true;

private Music music;//音乐

AudioInputStream din = null;

SourceDataLine line=null;

private FloatControl volume = null;

private JSlider jSliderVolume;

public JSlider getjSliderVolume() {

return jSliderVolume;

}

public void setjSliderVolume(JSlider jSliderVolume) {

this.jSliderVolume = jSliderVolume;

}

public Player(JSlider jSliderVolume,JSlider jSliderPlayProgress) {

super();

this.jSliderVolume = jSliderVolume;

this.jSliderPlayProgress=jSliderPlayProgress;

}

public Music getMusic() {

return music;

}

public void setMusic(Music music) {

this.music = music;

}

public FloatControl getVolume(){

return volume;

}

//播放音乐

public void run(){

AudioInputStream in=null;

try {

File file = new File(music.getPath());

//播放不了的歌曲,直接下一首,并且在音乐列表中删除

try {

in = AudioSystem.getAudioInputStream(file);

} catch (Exception e) {

//删除有点小问题

MusicList.getList().remove(music.getId());

ViewList.getList().get(0).getJt().setModel(new Model());

nextmusic();

}

AudioFormat baseFormat = in.getFormat();

AudioFormat decodedFormat = new AudioFormat(

AudioFormat.Encoding.PCM_SIGNED,

baseFormat.getSampleRate(), 16, baseFormat.getChannels(),

baseFormat.getChannels() * 2, baseFormat.getSampleRate(),

false);

if(baseFormat.getEncoding()==AudioFormat.Encoding.PCM_UNSIGNED || baseFormat.getEncoding()==AudioFormat.Encoding.ULAW ||

baseFormat.getEncoding()==AudioFormat.Encoding.ALAW || baseFormat.getEncoding()==AudioFormat.Encoding.PCM_SIGNED){

time=(file.length()*8000000)/((int)(decodedFormat.getSampleRate()*baseFormat.getSampleSizeInBits()));

}else{

int bitrate=0;

if(baseFormat.properties().get("bitrate")!=null){

//取得播放速度(单位位每秒)

bitrate=(int)((Integer)(baseFormat.properties().get("bitrate")));

if(bitrate!=0)

time=(file.length()*8000000)/bitrate;

}

}

din = AudioSystem.getAudioInputStream(decodedFormat, in);

DataLine.Info info = new DataLine.Info(SourceDataLine.class, decodedFormat);

line = (SourceDataLine) AudioSystem.getLine(info);

line.open();

setVolume();

jSliderPlayProgress.setMaximum((int)time);

jSliderPlayProgress.setValue(0);

if(line!=null){

line.open(decodedFormat);

byte[] data = new byte[4096];

int nBytesRead;

synchronized (lock) {

while ((nBytesRead = din.read(data, 0, data.length)) != -1) {

while (paused) {

if(line.isRunning()) {

line.stop();

System.out.println("暂停");

}

try {

lock.wait();

System.out.println("等待");

}

catch(InterruptedException e) {

}

}

if(!line.isRunning()&&!over) {

System.out.println("开始播放");

line.start();

}

if (over&&line.isRunning()) {

System.out.println("停止播放");

jSliderPlayProgress.setValue(0);

isNext=false;

line.drain();

line.stop();

line.close();

}

jSliderPlayProgress.setVa

  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值