当前位置:网站首页>添加自己喜欢的背景音乐
添加自己喜欢的背景音乐
2022-06-11 18:19:00 【C_x_330】
添加背景音乐
音乐类
package com.Cx_330.TankGame4;
import javax.sound.sampled.*;
import java.io.File;
import java.io.IOException;
/** * @author Cx_330 * @version 1.0 */
public class AePlayWave extends Thread {
private String filename;
public AePlayWave(String wavfile) {
filename = wavfile;
}
public void run() {
File soundFile = new File(filename);
AudioInputStream audioInputStream = null;
try {
audioInputStream = AudioSystem.getAudioInputStream(soundFile);
} catch (Exception e1) {
e1.printStackTrace();
return;
}
AudioFormat format = audioInputStream.getFormat();
SourceDataLine auline = null;
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
try {
auline = (SourceDataLine) AudioSystem.getLine(info);
auline.open(format);
} catch (Exception e) {
e.printStackTrace();
return;
}
auline.start();
int nBytesRead = 0;
//这是缓冲
byte[] abData = new byte[512];
try {
while (nBytesRead != -1) {
nBytesRead = audioInputStream.read(abData, 0, abData.length);
if (nBytesRead >= 0)
auline.write(abData, 0, nBytesRead);
}
} catch (IOException e) {
e.printStackTrace();
return;
} finally {
auline.drain();
auline.close();
}
}
}
注意事项
- 音乐的格式是Wav的形式,你可以到网上搜索或者在网易云下载在转化为Wav的格式也行
- 下载好的音乐要放在Out目录下,和放照片的位置一样,否则播放不出来
添加播放
new AePlayWave("src\\x.wav").start();
边栏推荐
猜你喜欢

Ti am64x - the latest 16nm processing platform, designed for industrial gateways and industrial robots

JS实现全屏展示的具体方法

2023年西安交通大学管理学院MPAcc提前批面试网报通知

全志科技T3开发板(4核ARM Cortex-A7)——MQTT通信协议案例

牛客刷题——把字符串转换成整数
![[C语言]用结构体把最高分的学生输出,可有多个最高分](/img/4e/836a8f717a2d9bf5f999a934ff4c91.png)
[C语言]用结构体把最高分的学生输出,可有多个最高分

Niu Ke's question -- finding the least common multiple

Niu Ke's questions -- binary search tree and bidirectional linked list
Summary of common mysql/redis interview questions

牛客刷题——两种排序方法
随机推荐
Mysql从0到1的完全深入学习--阶段二---基础篇
[golang] leetcode - 349 Intersection of two arrays (hash table)
Two methods for matlab to save imshow drawing pictures to a specified folder
Signal processing and capture
【无标题】
牛客刷题——Fibonacci数列
Force deduction questions -- create a string based on a binary tree
Reading summary of nacos2.x source code
5分钟了解攻防演练中的红蓝紫
高性能架构设计
金融银行_催收系统简介
[Golang]力扣Leetcode - 349. 两个数组的交集(哈希表)
下载代码,并编译环境的问题
Quanzhi technology T3 development board (4-core arm cortex-a7) - video development case
[c language] output the students with the highest scores with a structure. There can be multiple highest scores
Common interview questions of network and concurrent programming
2022-2023年西安交通大学管理学院MEM提前批面试网报通知
Force deduction 33 questions, search rotation sorting array
264 Concepts
2023年西安交通大学管理学院MPAcc提前批面试网报通知