本代码搬运b站上一位up主的
github的地址:GitHub - gulihua10010/playPiano: Java弹奏钢琴乐曲
项目结构如下:
首先是play包下三个类文件:
Animation:
package cn.jianwoo.play;
/**
* @author gulihua
* @Description
* @date 2022-11-15 00:58
*/
public class Animation extends Thread{
/** 音符 */
private String[] notes;
/** 间隔时间(单位:毫秒) */
private int times;
public Animation(int times) {
this.times = times;
}
public Animation(String[] notes, int times) {
this.notes = notes;
this.times = times;
}
public String[] getNotes() {
return this.notes;
}
public void setNotes(String[] notes) {
this.notes = notes;
}
public int getTimes() {
return this.times;
}
public void setTimes(int times) {
this.times = times;
}
public Animation loadNotes(String notes) {
this.notes = notes.split(" ");
return this;
}
@Override
public void run() {
try {
int times = this.times;
new Audio("audio/test.mp3").start();
sleep(1000);
int no = 1;
// System.out.print(no+": ");
for (int i = 0; i < this.notes.length; i++)
{
if (notes[i].length()<1){
continue;
}
String n = this.notes[i].replace("+","").replace("-","");
if (n.equals("\n")||n.equals("\r")){
System.out.print("\n");
no++;
// System.out.print(no+": ");
continue;
}
switch (n)
{
case "0":
System.out.print("_");
break;
case "1":
System.out.print("▁");
break;
case "2":
System.out.print("▂");
break;
case "3":
System.out.print("▃");
break;
case "4":
System.out.print("▄");
break;
case "5":
System.out.print("▅");
break;
case "6":
System.out.print("▆");
break;
case "7":
System.out.print("▇");
break;
}
System.out.print(" ");
sleep(times);
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
Audio:
package cn.jianwoo.play;
import java.io.InputStream;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import cn.hutool.core.io.resource.ResourceUtil;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;
public class Audio
{
private static InputStream is;
private Player player;
ExecutorService service = Executors.newCachedThreadPool();
public Audio(String path)
{
is = ResourceUtil.getStream(path);
try
{
player = new Player(is);
}
catch (JavaLayerException e)
{
e.printStackTrace();
}
}
public void start()
{
service.submit(() -> {
try
{
player.play();
}
catch (JavaLayerException e)
{
}
});
}
}
AudioPlay:
package cn.jianwoo.play;
import cn.hutool.core.io.FileUtil;
/**
* @author gulihua
* @Description
* @date 2022-11-12 18:24
*/
public class AudioPlay extends Thread
{
/** 音符 */
private String[] notes;
/** 间隔时间(单位:毫秒) */
private int times;
/** 模式 */
private Mode mode;
public AudioPlay(String[] notes, int times)
{
this.notes = notes;
this.times = times;
}
public AudioPlay(String[] notes, int times, Mode mode)
{
this.notes = notes;
this.times = times;
this.mode = mode;
}
public AudioPlay(String filePath, int times)
{
String content = FileUtil.readString(filePath,"UTF-8");
this.notes = content.split(" ");
this.times = times;
}
public AudioPlay(int times)
{
this.times = times;
}
public Mode getMode()
{
return this.mode;
}
public void setMode(Mode mode)
{
this.mode = mode;
}
public String[] getNotes()
{
return this.notes;
}
public void setNotes(String[] notes)
{
this.notes = notes;
}
public AudioPlay loadNotes(String notes)
{
this.notes = notes.split(" ");
return this;
}
public int getTimes()
{
return this.times;
}
public void setTimes(int times)
{
this.times = times;
}
@Override
public void run()
{
try
{
int times = this.times;
new Audio("audio/test.mp3").start();
sleep(1000);
for (int i = 0; i < notes.length; i++)
{
if (notes[i].length()<1){
continue;
}
switch (notes[i])
{
case "1--":
new Audio("audio/ll1.mp3").start();
sleep(times / 2);
break;
case "2--":
new Audio("audio/ll2.mp3").start();
sleep(times / 2);
break;
case "3--":
new Audio("audio/ll3.mp3").start();
sleep(times / 2);
break;
case "4--":
new Audio("audio/ll4.mp3").start();
sleep(times / 2);
break;
case "5--":
new Audio("audio/ll5.mp3").start();
sleep(times / 2);
break;
case "6--":
new Audio("audio/ll6.mp3").start();
sleep(times / 2);
break;
case "7--":
new Audio("audio/ll7.mp3").start();
sleep(times / 2);
break;
case "1-":
new Audio("audio/l1.mp3").start();
sleep(times / 2);
break;
case "2-":
new Audio("audio/l2.mp3").start();
sleep(times / 2);
break;
case "3-":
new Audio("audio/l3.mp3").start();
sleep(times / 2);
break;
case "4-":
new Audio("audio/l4.mp3").start();
sleep(times / 2);
break;
case "5-":
new Audio("audio/l5.mp3").start();
sleep(times / 2);
break;
case "6-":
new Audio("audio/l6.mp3").start();
sleep(times / 2);
break;
case "7-":
new Audio("audio/l7.mp3").start();
sleep(times / 2);
break;
case "1":
new Audio("audio/m1.mp3").start();
sleep(times / 2);
break;
case "2":
new Audio("audio/m2.mp3").start();
sleep(times / 2);
break;
case "3":
new Audio("audio/m3.mp3").start();
sleep(times / 2);
break;
case "4":
new Audio("audio/m4.mp3").start();
sleep(times / 2);
break;
case "5":
new Audio("audio/m5.mp3").start();
sleep(times / 2);
break;
case "6":
new Audio("audio/m6.mp3").start();
sleep(times / 2);
break;
case "7":
new Audio("audio/m7.mp3").start();
sleep(times / 2);
break;
case "1+":
new Audio("audio/h1.mp3").start();
sleep(times / 2);
break;
case "2+":
new Audio("audio/h2.mp3").start();
sleep(times / 2);
break;
case "3+":
new Audio("audio/h3.mp3").start();
sleep(times / 2);
break;
case "4+":
new Audio("audio/h4.mp3").start();
sleep(times / 2);
break;
case "5+":
new Audio("audio/h5.mp3").start();
sleep(times / 2);
break;
case "6+":
new Audio("audio/h6.mp3").start();
sleep(times / 2);
break;
case "7+":
new Audio("audio/h7.mp3").start();
sleep(times / 2);
break;
case "1++":
new Audio("audio/hh1.mp3").start();
sleep(times / 2);
break;
case "2++":
new Audio("audio/hh2.mp3").start();
sleep(times / 2);
break;
case "3++":
new Audio("audio/hh3.mp3").start();
sleep(times / 2);
break;
case "4++":
new Audio("audio/hh4.mp3").start();
sleep(times / 2);
break;
case "5++":
new Audio("audio/hh5.mp3").start();
sleep(times / 2);
break;
case "6++":
new Audio("audio/hh6.mp3").start();
sleep(times / 2);
break;
case "7++":
new Audio("audio/hh7.mp3").start();
sleep(times / 2);
break;
case "0":
sleep(times / 2);
break;
default:
continue;
}
sleep(times / 2);
times = this.times;
}
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
enum Mode {
/** 主奏 */
MAIN,
/** 伴奏 */
ACCOMPANIMENTS
}
}
测试类:
package cn.jianwoo.test;
import cn.jianwoo.play.Animation;
import cn.jianwoo.play.AudioPlay;
import java.io.File;
/**
* @author gulihua
* @Description
* @date 2022-11-13 17:52
*/
public class Test1 {
public static void main(String[] args) {
String path =
new File("").getAbsolutePath() + File.separator + "src/main/resources/notes" + File.separator;
String notes =
" 7- 1 2 3 0 5- 5 3 0 0 0 0 0 0 0 0 \n" +
" 7- 1 2 3 0 5- 5 3 2 3 1 2 7- 1 5- 0 \n" +
" 7- 1 2 3 0 5- 5 3 0 0 0 0 0 0 0 0 \n" +
" 7- 1 2 3 0 5- 5 3 2 3 1 2 7- 1 5- 0 \n" +
" 7 1+ 2+ 3+ 0 5 5+ 3+ 0 0 0 0 0 0 0 0 \n" +
" 7 1+ 2+ 3+ 0 5 5+ 3+ 2+ 3+ 1+ 2+ 7 1+ 5 0 \n" +
" 7 1+ 2+ 3+ 0 5 5+ 3+ 0 0 0 0 0 0 0 0 \n" +
" 2 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 \n" +
" 2 0 0 1 2 0 0 1 2 0 3 0 5 0 3 0 \n" +
" 2 0 0 1 2 0 0 1 2 3 2 1 6- 0 0 0 \n" +
" 2 0 0 1 2 0 0 1 2 0 3 0 5 0 3 0 \n" +
" 2 0 0 3 2 0 1 2 2 0 0 0 0 0 0 0 \n" +
" 2 0 0 1 2 0 0 1 2 0 3 0 5 0 3 0 \n" +
" 2 0 0 3 2 0 1 0 6- 0 0 0 \n" +
" 3 2 1 2 1 0 0 0 \n" +
" 3 2 1 2 1 0 0 \n" +
" 5- 3 2 1 2 0 0 1 0 0 0 0 0 \n" +
" 1 0 2 0 3 0 1 0 6 0 5 6 0 0 0 \n" +
" 2 7 0 6 7 0 0 0 0 \n" +
" 7 0 6 7 0 0 3 0 1+ 2+ 1+ 7 6 0 0 \n" +
" 5 6 0 5 6 0 5 6 5 6 0 5 1 0 5 0 3 3 0 0 0 0 0 0 0 \n" +
" 1 0 2 0 3 0 1 0 6 0 5 6 0 0 0 \n" +
" 2 7 0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0 0 0 0 0 5-- 0 0 0 0 0 0 0 \n" +
" 1- 0 0 0 0 0 0 0 1- 0 0 0 0 0 0 0 \n" +
" 4-- 0 0 0 0 0 0 0 5-- 0 0 0 0 0 0 0 \n" +
" 3-- 0 0 0 0 0 0 0 6-- 0 0 0 0 0 0 0 \n" +
" 4-- 0 0 0 0 0 0 0 5-- 0 0 0 0 0 0 0 \n" +
" 1- 0 0 0 0 0 0 0 1- 0 0 0 0 0 0 0 \n" +
" 4-- 0 0 0 0 0 0 0 5-- 0 0 0 0 0 0 0 \n" +
" 3-- 0 0 0 0 0 0 0 6-- 0 0 0 0 0 0 0 \n" +
" 4-- 0 0 0 0 0 0 0 5-- 0 0 0 0 0 0 0 \n" +
" 6-- 0 0 0 3- 0 0 0 6- 0 0 0 0 0 0 0 \n" +
" 4-- 0 0 0 4- 0 0 0 5-- 0 0 0 5- 0 0 0 \n" +
" 3-- 0 0 0 3- 0 0 0 6-- 0 0 0 6- 0 0 0 \n" +
" 4-- 0 0 0 4- 0 0 0 5-- 0 0 0 5- 0 0 0 \n" +
" 1-- 0 0 0 1- 0 0 0 3-- 0 0 0 3- 0 0 0 \n" +
" 4-- 0 0 0 4- 0 0 0 5-- 0 0 0 5- 0 0 0 \n" +
" 3-- 0 0 0 3- 0 0 0 6-- 0 0 0 6- 0 0 0 \n" +
" 4-- 0 0 0 4- 0 0 0 5-- 0 0 0 5- 0 0 0 \n" +
" 6-- 0 0 0 3- 0 0 0 6-- 0 0 0 0 0 0 0 \n" +
" 4-- 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \n" +
" 5-- 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \n" +
" 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \n" +
" 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ";
new AudioPlay(180).loadNotes(notes + note1).start();
new AudioPlay(180).loadNotes(accompaniments + accompaniments1).start();
new Animation(180).loadNotes(notes + note1).start();
}
}
演示如下:
项目中的resource文件夹在这个百度网盘链接中:
链接:https://pan.baidu.com/s/1LN6qKI0HYDmtUgbVBNc0IA?pwd=coin
提取码:coin