环声音java目录 [英] Loop sound in java directory

查看:100
本文介绍了环声音java目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要用一个声音类别一些解释。于是我做了一个声音对象类,让我简单地调用任何声音,我需要在以后的时间。我有我在声音文件夹中的同一个目录中的Java文件的文件。所以这是〜\\游戏\\声音\\ music.wav 现在,每当我通过创建一个对象,然后调用它执行从主类此code时,它给我的文件不存在,我指着我到不正确的文件吗?目前我文件名music.wav我怎么可能只是将其指向声音目录,而不硬编码的目录,以便它可以在任何CPU协同工作。

 公众声音(字符串文件名){
        尝试{
                档案文件=新的文件(文件名);
                如果(file.exists()){
                    myClip =(音频剪辑)Applet.newAudioClip(file.toURI()的toURL());
                }其他{
                    抛出新的RuntimeException(声音:未找到文件:+文件名);
                }
        }赶上(MalformedURLException的E){
            抛出新的RuntimeException(声音:错误的URL:+ E);
        }
}
公共无效播放(){
    myClip.play();
}


解决方案

我希望这code会帮助你的。我创建了一个下的的的src 的名为资源的。在资源包,我把我所有的声音文件。

 进口java.io. *;
进口的java.net.URL;
进口javax.sound.sampled中*。公共枚举SoundEffect中{
    BUSY(资源/电话busy.wav),
    呼叫(资源/电话calling.wav),
    断开(资源/电话disconnect.wav),
    RING(资源/电话ring.wav);    //每个音效都有自己的剪辑,装载有它自己的声音文件。
    私人夹夹;
    私人URL网址;
    私人的AudioInputStream的AudioInputStream;    //构造函数构造枚举的每个元素都有自己的声音文件。
    SoundEffect中(字符串soundFileName){
        尝试{
            //使用URL(而不是文件),从磁盘和JAR读取。
            this.url = this.getClass()getClassLoader()的getResource(soundFileName)。
            //设置从声音文件管道的音频输入流。
            this.audioInputStream = AudioSystem.getAudioInputStream(URL);
            //获取剪辑资源。
            夹= AudioSystem.getClip();
            //打开音频剪辑和负载样品从音频输入流。
            clip.open(的AudioInputStream);        }赶上(UnsupportedAudioFileException E){
            e.printStackTrace();
        }赶上(IOException异常五){
            e.printStackTrace();
        }赶上(LineUnavailableException E){
            e.printStackTrace();
        }
    }    //播放或从开始重新播放声音效果,经复卷。
    公共无效播放(){        clip.loop(Clip.LOOP_CONTINUOUSLY);    }    公共无效停止(){        clip.stop(); //停止播放器,如果它仍在运行
        clip.flush();
        clip.setFramePosition(0);
    }    //可选的静态方法pre-负载的所有声音文件。
    静态无效的init(){
        值(); //呼吁所有元素的构造
    }    公共布尔isActive(){        返回clip.isActive();
    }    公共布尔isOpen会(){        返回clip.isOpen();
    }    公共无效setFramePosition(){
        clip.setFramePosition(0);    }}

本类是Swing应用程序测试SoundEffect中枚举

 进口java.awt中的*。
java.awt.event中导入*。进口的javax.swing *。//测试SoundEffect中枚举在Swing应用程序
@燮pressWarnings(串行)
公共类SoundEffectDemo扩展的JFrame {
    //构造
    公共SoundEffectDemo(){
        // pre-负载的所有声音文件
        //设置UI组件
        容器CP = this.getContentPane();
        cp.setLayout(新的FlowLayout(FlowLayout.CENTER,10,10));        JButton的btnSound1 =的新的JButton(CALLING);
        btnSound1.addActionListener(新的ActionListener(){
            @覆盖
            公共无效的actionPerformed(ActionEvent的五){
                SoundEffect.CALLING.play();
            }
        });
        cp.add(btnSound1);        JButton的btnSound2 =的新的JButton(RING);
        btnSound2.addActionListener(新的ActionListener(){
            @覆盖
            公共无效的actionPerformed(ActionEvent的五){
                SoundEffect.RING.play();
            }
        });
        cp.add(btnSound2);        JButton的btnSound3 =的新的JButton(BUSY);
        btnSound3.addActionListener(新的ActionListener(){
            @覆盖
            公共无效的actionPerformed(ActionEvent的五){                SoundEffect.BUSY.play();
            }
        });
        cp.add(btnSound3);        JButton的btnSound4 =的新的JButton(停止声音);
        btnSound4.addActionListener(新的ActionListener(){
            @覆盖
            公共无效的actionPerformed(ActionEvent的五){
                对于(SoundEffect中值:SoundEffect.values​​()){
                    如果(value.isActive()){
                        value.stop();
                    }
                }            }
        });
        cp.add(btnSound4);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setTitle(测试SoundEffct);
        this.pack();
        this.setVisible(真);
    }    公共静态无效的主要(字串[] args){
        新SoundEffectDemo();
    }
}

I need some explanation with a sound class. So I made a Sound object class to allow me to simply call any sound I need to at a later time. I have my file in a sounds folder in the same directory as the java files. So it's ~\Game\sounds\music.wav Now every time I execute this code from the main class via creating an object then calling it, it gives me file doesn't exist, am I pointing incorrectly into the file? Currently my fileName is only "music.wav" how could I just point it to the sounds directory without hard coding the directory so it can work on any cpu.

public Sound(String fileName) {
        try {
                File file = new File(fileName);
                if (file.exists()) {
                    myClip = (AudioClip) Applet.newAudioClip(file.toURI().toURL());
                } else {
                    throw new RuntimeException("Sound: file not found: " + fileName);
                }
        } catch (MalformedURLException e) {
            throw new RuntimeException("Sound: malformed URL: " + e);
        }
}
public void play() {
    myClip.play();
}

解决方案

I hope this code will helping you. I created a package under src named resources. Under resources package, I put all my sound files.

import java.io.*;
import java.net.URL;
import javax.sound.sampled.*;

public enum SoundEffect  {


    BUSY("resources/phone-busy.wav"),   
    CALLING("resources/phone-calling.wav"),         
    DISCONNECT("resources/phone-disconnect.wav"),
    RING("resources/telephone-ring.wav");  

    // Each sound effect has its own clip, loaded with its own sound file.
    private Clip clip;
    private URL url;
    private AudioInputStream audioInputStream;

    // Constructor to construct each element of the enum with its own sound file.
    SoundEffect(String soundFileName) {
        try {
            // Use URL (instead of File) to read from disk and JAR.
            this.url = this.getClass().getClassLoader().getResource(soundFileName);
            // Set up an audio input stream piped from the sound file.
            this.audioInputStream = AudioSystem.getAudioInputStream(url);
            // Get a clip resource.
            clip = AudioSystem.getClip();
            // Open audio clip and load samples from the audio input stream.
            clip.open(audioInputStream);

        } catch (UnsupportedAudioFileException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (LineUnavailableException e) {
            e.printStackTrace();
        }
    }

    // Play or Re-play the sound effect from the beginning, by rewinding.
    public void play() {

        clip.loop(Clip.LOOP_CONTINUOUSLY); 

    }

    public void stop(){

        clip.stop();   // Stop the player if it is still running
        clip.flush();
        clip.setFramePosition(0);
    }

    // Optional static method to pre-load all the sound files.
    static void init() {
        values(); // calls the constructor for all the elements
    }

    public boolean isActive(){

        return clip.isActive();
    }

    public boolean isOpen() {

        return clip.isOpen();
    }

    public void setFramePosition() {
        clip.setFramePosition(0);

    }

}

This Class is for testing the SoundEffect enum in a Swing application

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

// Testing the SoundEffect enum in a Swing application
@SuppressWarnings("serial")
public class SoundEffectDemo extends JFrame {


    // Constructor
    public SoundEffectDemo() {
        // Pre-load all the sound files


        // Set up UI components
        Container cp = this.getContentPane();
        cp.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));

        JButton btnSound1 = new JButton("CALLING");
        btnSound1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                SoundEffect.CALLING.play(); 
            }
        });
        cp.add(btnSound1);

        JButton btnSound2 = new JButton("RING");
        btnSound2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                SoundEffect.RING.play();
            }
        });
        cp.add(btnSound2);

        JButton btnSound3 = new JButton("BUSY");
        btnSound3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                SoundEffect.BUSY.play();
            }
        });
        cp.add(btnSound3);

        JButton btnSound4 = new JButton("Stop Sound ");
        btnSound4.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                for(SoundEffect value : SoundEffect.values()){
                    if(value.isActive()){
                        value.stop();
                    }
                }

            }
        });
        cp.add(btnSound4);


        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setTitle("Test SoundEffct");
        this.pack();
        this.setVisible(true);
    }

    public static void main(String[] args) {
        new SoundEffectDemo();
    }
}

这篇关于环声音java目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆