简易的本地音乐播放器 适用于Java初学者
我知道肯定会有人说都1202年了怎么还有人在用AudioClip,没有别的原因,因为我也刚学Java,刚好看到一个这样的教程就刚好写了一个这样的播放器。
我用的eclipse写的程序,一共有4个类,相应的音频文件和界面背景图片自己下载。注意:音频文件只能是.wav文件,可以去网上下载MP3文件然后用格式工厂转换格式。
代码绝对有用,已经用过了,但是由于比较简易,整体比较粗糙很多功能无法实现,我也是初学者,所以整体代码可能有很多不够好的地方求大佬们轻喷
使用缺陷:
1.开始播放只能播放第一首歌曲,我这里是《晴天》;
2.点击歌曲按钮只能点击一次,切歌只能点击下一首,或者点击暂停播放然后自己点击一首歌曲播放;
3.循环播放问题比较多,写的比较随便,只能在已经播放歌曲的状 态下点击,而且循环播放之后无法暂停播放只能退出;
界面设计借鉴参考了这篇文章
图片:
图片文件放在包里面了;
代码直接粘贴复制就能用了,但是写的很粗糙
MusicPlayer类
import java.util.Scanner;
import java.applet.*;
import java.awt.Frame;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.*;
public class MusicPlayer {
private static AudioClip ac;
public static void player(int a,int b) throws MalformedURLException,
InterruptedException {
Scanner input=new Scanner(System.in);
JApplet applate = new JApplet();
URL urlAudio;
String x;
switch(a)
{
case 1:x="D:\\FFOutput\\周杰伦 - 晴天.wav";
File f = new File(x);
urlAudio = f.toURL();
ac = Applet.newAudioClip(urlAudio);
ac.stop();
ac.play();
Title_1.n =2;
Thread.sleep(2000);
System.out.println("正在播放《晴天》");
break;
case 2:x="D:\\FFOutput\\蜡笔小心 - MOM.wav";
f = new File(x);
urlAudio = f.toURL();
ac = Applet.newAudioClip(urlAudio);
ac.stop();
ac.play();
Title_1.n =3;
Thread.sleep(2000);
System.out.println("正在播放《MOM》");
break;
case 3:x="D:\\FFOutput\\星辰大海 - 黄霄云.wav";
f = new File(x);
urlAudio = f.toURL();
ac = Applet.newAudioClip(urlAudio);
ac.stop();
ac.play();
Title_1.n =4;
Thread.sleep(2000);
System.out.println("正在播放《星辰大海》");
break;
case 4:x="D:\\FFOutput\\回到夏天.wav";
f = new File(x);
urlAudio = f.toURL();
ac = Applet.newAudioClip(urlAudio);
ac.stop();
ac.play();
Title_1.n =5;
Thread.sleep(2000);
System.out.println("正在播放《回到夏天》");
break;
case 5:x="D:\\FFOutput\\大籽-白月光与朱砂痣.wav";
f = new File(x);
urlAudio = f.toURL();
ac = Applet.newAudioClip(urlAudio);
ac.stop();
ac.play();
Title_1.n =6;
Thread.sleep(2000);
System.out.println("正在播放《白月光与朱砂痣》");
break;
case 6:x="D:\\FFOutput\\欢 - 刘大壮.wav";
f = new File(x);
urlAudio = f.toURL();
ac = Applet.newAudioClip(urlAudio);
ac.stop();
ac.play();
Title_1.n =7;
Thread.sleep(2000);
System.out.println("正在播放《欢》");
break;
case 7:x="D:\\FFOutput\\四季予你 - 程响.wav";
f = new File(x);
urlAudio = f.toURL();
ac = Applet.newAudioClip(urlAudio);
ac.stop();
ac.play();
Title_1.n =8;
Thread.sleep(2000);
System.out.println("正在播放《四季予你》");
break;
case 8:x="D:\\FFOutput\\夏天的风.wav";
f = new File(x);
urlAudio = f.toURL();
ac = Applet.newAudioClip(urlAudio);
ac.stop();
ac.play();
Title_1.n =9;
Thread.sleep(2000);
System.out.println("正在播放《夏天的风》");
break;
case 9:
x="D:\\FFOutput\\爱,存在.wav";
f = new File(x);
urlAudio = f.toURL();
ac = Applet.newAudioClip(urlAudio);
ac.stop();
ac.play();
Title_1.n =10;
Thread.sleep(2000);
System.out.println("正在播放《爱,存在》");
break;
case 10:x="D:\\FFOutput\\飞鸟和蝉.wav";
f = new File(x);
urlAudio = f.toURL();
ac = Applet.newAudioClip(urlAudio);
ac.play();
Title_1.n =1;
Thread.sleep(2000);
System.out.println("正在播放《飞鸟和蝉》");
break;
case 11:ac.stop();
player(b,0);
Thread.sleep(1000);
break;
case 12://ac.stop();
player(1,0);
Thread.sleep(1000);
break;
}
}
public static void stop()
{
ac.stop();
}
public static void run1() throws InterruptedException
{
((Thread) ac).yield();
}
public static String getLoad()
{
String x=null;
switch(Title_1.n-1)
{
case 1:x="D:\\FFOutput\\周杰伦 - 晴天.wav";
break;
case 2:x="D:\\FFOutput\\蜡笔小心 - MOM.wav";
break;
case 3:x="D:\\FFOutput\\星辰大海 - 黄霄云.wav";
break;
case 4:x="D:\\FFOutput\\回到夏天.wav";
break;
case 5:x="D:\\FFOutput\\大籽-白月光与朱砂痣.wav";
break;
case 6:x="D:\\FFOutput\\欢 - 刘大壮.wav";
break;
case 7:x="D:\\FFOutput\\四季予你 - 程响.wav";
break;
case 8:x="D:\\FFOutput\\夏天的风.wav";
break;
case 9:x="D:\\FFOutput\\爱,存在.wav";
break;
case 10:x="D:\\FFOutput\\飞鸟和蝉.wav";
break;
}
ac.stop();
Loop.loop(x,0);
System.out.println("正在循环播放该歌曲");
return x;
}
}
界面设计类
import javax.swing.AbstractButton;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.applet.AudioClip;
import java.io.*;
import java.applet.Applet;
import java.awt.Button;
import java.awt.Color;
import java.awt.Font;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
public class Title_1 extends JFrame {
public static int n=0;
public Title_1() throws MalformedURLException, InterruptedException {
super("栗子猪播放器"); //设置标题
int num=1;
setSize(1080, 1080); //设置大小,
setLocation(550, 200); //设置位置
setResizable(false); //窗体大小固定
String path = "src/itle_1.jpg"; //背景图片的路径。(相对路径或者绝对路径。本例图片放于"java项目名"的文件下)
ImageIcon background = new ImageIcon(path); // 背景图片
JLabel label = new JLabel(background); // 把背景图片显示在一个标签里面
label.setBounds(0, 0, this.getWidth(), this.getHeight()); // 把标签的大小位置设置为图片刚好填充整个面板
JPanel imagePanel = (JPanel) this.getContentPane(); // 把内容窗格转化为JPanel,否则不能用方法setOpaque()来使内容窗格透明
imagePanel.setOpaque(false);
this.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE)); // 把背景图片添加到分层窗格的最底层作为背景
File title_music;
URI uri_t1;
URL url_t1;
setLayout(null); //清空布局
JButton b1=new JButton("-开始播放-"); //设置按钮名字
b1.setFont(new Font("黑体", Font.PLAIN,25)); //设置按钮中的字体属性
b1.setBounds(285,150,170,70); //设置按钮位置,及按钮大小
b1.setContentAreaFilled(false); //设置按钮透明
b1.setForeground(Color.BLACK); //设置前景色
add(b1);
JButton b2=new JButton("-退出-");
b2.setFont(new Font("黑体", Font.PLAIN,25));
b2.setBounds(625,150,170,70);
b2.setContentAreaFilled(false);
b2.setForeground(Color.BLACK);
add(b2);
JButton b10=new JButton("暂停播放");
b10.setFont(new Font("黑体", Font.PLAIN,25));
b10.setBounds(370,750,170,70);
b10.setContentAreaFilled(false);
b10.setForeground(Color.BLACK);
add(b10);
JButton b3=new JButton("-下一首-");
b3.setFont(new Font("黑体", Font.PLAIN,25));
b3.setBounds(455,150,170,70);
b3.setContentAreaFilled(false);
b3.setForeground(Color.BLACK);
add(b3);
JButton b4=new JButton("《晴天》");
b4.setFont(new Font("黑体", Font.PLAIN,25));
b4.setBounds(370,300,170,70);
b4.setContentAreaFilled(false);
b4.setForeground(Color.BLACK);
add(b4);
JButton b5=new JButton("《MOM》");
b5.setFont(new Font("黑体", Font.PLAIN,25));
b5.setBounds(540,300,170,70);
b5.setContentAreaFilled(false);
b5.setForeground(Color.BLACK);
add(b5);
JButton b6=new JButton("《星辰大海》");
b6.setFont(new Font("黑体", Font.PLAIN,25));
b6.setBounds(370,370,170,70);
b6.setContentAreaFilled(false);
b6.setForeground(Color.BLACK);
add(b6);
JButton b7=new JButton("《回到夏天》");
b7.setFont(new Font("黑体", Font.PLAIN,25));
b7.setBounds(540,370,170,70);
b7.setContentAreaFilled(false);
b7.setForeground(Color.BLACK);
add(b7);
JButton b8=new JButton("《白月光与朱砂痣》");
b8.setFont(new Font("黑体", Font.PLAIN,25));
b8.setBounds(370,440,170,70);
b8.setContentAreaFilled(false);
b8.setForeground(Color.BLACK);
add(b8);
JButton b9=new JButton("《欢》");
b9.setFont(new Font("黑体", Font.PLAIN,25));
b9.setBounds(540,440,170,70);
b9.setContentAreaFilled(false);
b9.setForeground(Color.BLACK);
add(b9);
JButton b11=new JButton("《四季予你》");
b11.setFont(new Font("黑体", Font.PLAIN,25));
b11.setBounds(370,510,170,70);
b11.setContentAreaFilled(false);
b11.setForeground(Color.BLACK);
add(b11);
JButton b12=new JButton("《夏天的风》");
b12.setFont(new Font("黑体", Font.PLAIN,25));
b12.setBounds(540,510,170,70);
b12.setContentAreaFilled(false);
b12.setForeground(Color.BLACK);
add(b12);
JButton b13=new JButton("《爱,存在》");
b13.setFont(new Font("黑体", Font.PLAIN,25));
b13.setBounds(370,580,170,70);
b13.setContentAreaFilled(false);
b13.setForeground(Color.BLACK);
add(b13);
JButton b14=new JButton("《飞鸟和蝉》");
b14.setFont(new Font("黑体", Font.PLAIN,25));
b14.setBounds(540,580,170,70);
b14.setContentAreaFilled(false);
b14.setForeground(Color.BLACK);
add(b14);
JButton b15=new JButton("循环播放");
b15.setFont(new Font("黑体", Font.PLAIN,25));
b15.setBounds(540,750,170,70);
b15.setContentAreaFilled(false);
b15.setForeground(Color.BLACK);
add(b15);
myEvent1(b9,6,0);
myEvent1(b4,1,0);
myEvent1(b5,2,0);
myEvent1(b6,3,0);
myEvent1(b7,4,0);
myEvent1(b8,5,0);
myEvent1(b11,7,0);
myEvent1(b12,8,0);
myEvent1(b13,9,0);
myEvent1(b14,10,0);
myEvent1(b3,n,1);
//myEvent1(b12,n,0);
myEvent(b2);
myEvent3(b10);
myEvent4(b1);
myEvent5(b15);
//myEvent5(b3);
setVisible(true); //刷新并设置可见
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //点关闭按钮时退出
}
private void myEvent1(JButton b,final int m,int a) throws InterruptedException
{
if(a==0)
{
n=m;
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try {
MusicPlayer.player(m,0);
//System.out.println("n="+n+"m="+m);
} catch (MalformedURLException | InterruptedException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
}
}
});
}
if(a==1)
{
//MusicPlayer.stop();
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try {
//final int x=n;
MusicPlayer.player(11,n);
} catch (MalformedURLException | InterruptedException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
}
}
});
}
}
private void myEvent3(JButton b)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println("停止播放");
MusicPlayer.stop();
}
});
}
private void myEvent(JButton b)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println("退出播放器");
System.exit(0);
}
});
}
private void myEvent4(JButton b)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try {
//MusicPlayer.stop();
MusicPlayer.player(12,0);
}
catch (MalformedURLException | InterruptedException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
}
}
});
}
private void myEvent5(JButton b)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
MusicPlayer.getLoad();
}
});
}
}
循环播放是今天加上去的所以加了一个类
LOOP类
import java.applet.Applet;
import java.applet.AudioClip;
import java.io.File;
import java.net.URI;
import java.net.URL;
public class Loop {
public static void loop(String x,int a)
{
File title_music;
URI uri_t1;
URL url_t1;
try{
title_music = new File(x); //将背景音乐放入缓冲区
uri_t1 = title_music.toURI();
url_t1 = uri_t1.toURL(); //解析地址
AudioClip aau;
aau = Applet.newAudioClip(url_t1);
aau.loop(); //循环播放
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
测试类
import java.net.MalformedURLException;
public class Test {
public static void main(String[] args) throws MalformedURLException, InterruptedException
{
//new AwtDemo();
Title_1 a=new Title_1();
//MusicPlayer p=new MusicPlayer();
}
}
最后,做的确实很粗糙,因为我也刚学,希望各位大佬们多多指导,代码也写得比较乱老师们望见谅。
本人第一篇博客写的不好哈哈哈哈别太介意