java - 用java声音不玩?甚至16或8位?

标签 java audio file-io wav

您好,我的问题是,尽管我不知道自己做错了什么,但我无法让程序播放声音,我遵循了一些视频的指导,到目前为止,我仍然不知道该怎么做。我什至将文件转换为.wav和8或16位,所以我知道这不是文件,因为Java支持它。

这是代码和错误消息:

import java.io.File;
import java.util.ArrayList;
import java.util.concurrent.ThreadPoolExecutor;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.FloatControl;

public class MusicPlayer implements Runnable {

    private ArrayList<String> musicFiles;
    private int currentSongIndex;

    public MusicPlayer(String...files){

        musicFiles = new ArrayList<String>();


        for(String file : files){

            musicFiles.add("./Music Player/src" + file + ".wav");




        }


    }

    private void playSound(String fileName){

        try{

            File soundFile = new File(fileName);
            AudioInputStream ais = AudioSystem.getAudioInputStream(soundFile);
            AudioFormat format = ais.getFormat();
            DataLine.Info info = new DataLine.Info(Clip.class, format);

            Clip clip = (Clip) AudioSystem.getLine(info);
            clip.open(ais);
            FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);

            gainControl.setValue(-10);
            clip.start();

        }catch(Exception e){

            e.printStackTrace();


        }


    }

    public static void main(String[] args) {

//      ThreadPoolExecutor pool = new ThreadPoolExecutor(2);
        MusicPlayer music = new MusicPlayer("Chillout");

        music.playSound("./Music Player/src/Chillout.wav");

    }


    @Override
    public void run() {

        playSound(musicFiles.get(currentSongIndex));


    }

}

Error message:


java.io.FileNotFoundException: .\Music Player\src\Chillout.wav (The system cannot find the path specified)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(Unknown Source)
    at java.io.FileInputStream.<init>(Unknown Source)
    at com.sun.media.sound.WaveFloatFileReader.getAudioInputStream(Unknown Source)
    at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)
    at MusicPlayer.playSound(MusicPlayer.java:39)
    at MusicPlayer.main(MusicPlayer.java:65)

我不明白,我尝试使用scr中的文件以及项目文件夹中的文件,仍然找不到文件?

谢谢

最佳答案

我将在上面引用我自己的评论,该评论提供了有关如何解决问题的简单说明。现在,Internet上有许多人正在寻找解决方案,大多数答案和视频都令人失望,因为我搜索了一个小时,找到了可以正常工作的东西。

如果您正在寻找解决此问题的简便方法,请享受;)。

It was a problem that I solved just by changing folder address and instantiating an object music player into the the run method, this makes sense because you need to start the player through the constructor passing the string, the gainControl, has nothing to do with it because it's just something for later use. Thank you though I ll post the solution.


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.concurrent.ThreadPoolExecutor;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.FloatControl;

public class MusicPlayer implements Runnable {

    private ArrayList<String> musicFiles;

    public MusicPlayer(String...files){

        musicFiles = new ArrayList<String>();


        for(String file : files){

            musicFiles.add("C:"fullpath"/Music Player/src/" + file + ".wav");

        }


    }

    private void playSound(String fileName){

        try{

            File soundFile = new File(fileName);
            AudioInputStream ais = AudioSystem.getAudioInputStream(soundFile);
            AudioFormat format = ais.getFormat();
            DataLine.Info info = new DataLine.Info(Clip.class, format);

            Clip clip = (Clip) AudioSystem.getLine(info);
            clip.open(ais);
            FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);

            gainControl.setValue(-10);
            clip.start();

        }catch(Exception e){

            e.printStackTrace();



        }
    }

    @Override
    public void run() {

        MusicPlayer music = new MusicPlayer("Chillout");
        music.playSound("C:"fullpath"/Music/Chillout.wav");

    }


}

关于java - 用java声音不玩?甚至16或8位?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43713319/

相关文章:

java - 如何在Android中查找单个乐器的和弦

java - 可以使用什么过程来比较两个单独列表中的元素?

java - 如何使用 Selenium WebDriver 的 Assert 来检查登录是否成功?

ios - Cordova 媒体插件破坏 iOS 上的 HTML5 音频标签

jquery - Jquery 中的文本突出显示与音频同步

python - 在 python 中读取 .daq (Data Acquisition Toolbox for Matlab) 文件格式

php - 如何判断文件是否可从脚本写入?

java - 将文件写入随机不连续的硬盘位置

audio - 调整频率范围-Web Audio API分析器

c++ - 为什么QProcess::readAllStandardOutput不读取所有输出 channel ?