gpt4 book ai didi

java - 如何随机播放和播放阵列中的音频文件

转载 作者:行者123 更新时间:2023-12-03 01:51:32 24 4
gpt4 key购买 nike

我的代码有一些问题,我想改组包含.mp3音频文件的文件数组,并在单击按钮时播放其中的一个文件!我绝对不知道该怎么做,我需要一些帮助,真正的帮助!这是我的代码!.....它可以运行,但可以一次播放所有文件....我希望它随机化,只播放其中一个文件。

import javax.swing.*;

import sun.audio.AudioPlayer;
import sun.audio.AudioStream;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Array;
import java.util.Random;

public class Quest1 extends JFrame {

String word [] = { "C:/Users/HP/Desktop/eclipse/audio.wav",
"C:/Users/HP/Desktop/eclipse/baby.wav",
"C:/Users/HP/Desktop/eclipse/board.wav",
"C:/Users/HP/Desktop/eclipse/bomb.wav",
"C:/Users/HP/Desktop/eclipse/gym.wav",
"C:/Users/HP/Desktop/eclipse/football.wav",
"C:/Users/HP/Desktop/eclipse/school.wav",
"C:/Users/HP/Desktop/eclipse/keyboard.wav",
"C:/Users/HP/Desktop/eclipse/computer.wav",
"C:/Users/HP/Desktop/eclipse/name.wav" };


JButton click;

public Quest1 () {

getContentPane().setBackground(Color.DARK_GRAY);

setLayout(new GridBagLayout());

GridBagConstraints g = new GridBagConstraints();


g.anchor = GridBagConstraints.CENTER;
g.gridx = 4;
g.gridy = 5;
g.gridwidth = 2;
g.insets = new Insets (50, 2, 2, 2);
g.fill = GridBagConstraints.CENTER;
fill = new JTextField(15);
add (fill, g);

g.anchor = GridBagConstraints.CENTER;
g.gridx = 4;
g.gridy = 8;
g.gridwidth = 2;
g.insets = new Insets (30, 2,2,2);
click = new JButton("Play");
add(click, g);

click.addActionListener (new ActionListener (){
public void actionPerformed (ActionEvent x) {

Random rand = new Random();
for (int i = 0; i < word.length;) {
int random = rand.nextInt(word.length);
String temp = word[i];
word [i] = word[random];
word[random] = temp;

InputStream in =null;
AudioStream out = null;
try {
in = new FileInputStream(temp);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
out = new AudioStream(in);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
AudioPlayer.player.start(out);
}
return;
}
});
}
}

最佳答案

如果它是一个普通的Java数组,其中包含要播放的文件,则最简单的方法是简单地生成一个随机索引,并从该数组中获取具有该索引的对象:

//Index of the random song
int r = (int) (Math.random() * (songs.length - 1);
//Get the song from the array
songs[r]
...

编辑:

您说过要从阵列中播放一个随机声音,因为您知道代码只是按正确的顺序播放了阵列中的所有文件。

如果只想播放一个,则必须删除 for循环。

正确的代码:
click.addActionListener (new ActionListener (){
public void actionPerformed (ActionEvent x) {
//Get random filepath from the array
Random rand = new Random();
int random = rand.nextInt(word.length);
String temp = word[random];

InputStream in =null;
AudioStream out = null;
//Get the actual file from the path
try {
in = new FileInputStream(temp);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
out = new AudioStream(in);
} catch (IOException e) {
e.printStackTrace();
}
//Play the file
AudioPlayer.player.start(out);
return;
}
});

关于java - 如何随机播放和播放阵列中的音频文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39308944/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com