【问题标题】:How to shuffle and play an audio file from an array如何随机播放和播放数组中的音频文件
【发布时间】:2016-09-03 16:18:09
【问题描述】:

我的代码有一些问题,我想随机播放一个包含 .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 arrays audio actionlistener shuffle


【解决方案1】:

如果它是一个普通的 java 数组来保存你想要播放的文件,最简单的方法是简单地生成一个随机索引并从数组中获取具有该索引的对象:

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

编辑:

你说你想从数组中播放一个随机声音,因为你知道你的代码只是以正确的顺序播放数组中的所有文件。

如果您只想玩 ONE,则必须删除 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;
        }
    });     

【讨论】:

  • 嗯,不知道你能不能帮我写下播放声音文件的语法,我好像没搞清楚.......谢谢!
  • 我需要更多信息来帮助您播放文件本身。
  • 我已经编辑了我的帖子。
  • 没问题@Presh_K7 :)
猜你喜欢
  • 2014-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-21
  • 2013-07-17
  • 1970-01-01
  • 2012-12-24
  • 2020-09-12
相关资源
最近更新 更多