玩在eclipse OGG文件 [英] Playing ogg files in eclipse

查看:164
本文介绍了玩在eclipse OGG文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想将声音添加到我的乒乓球比赛,但由于某些原因,我似乎无法得到的声音播放...有没有错误消息,这意味着该路径是正确的,但声音根本不玩了。

以下是我的code为背景音乐,所有的东西傍切出,先谢谢了〜

 进口java.applet.Applet中;
    进口java.applet.AudioClip中;
    进口的java.net.URL;
    进口javax.swing.JApplet中;
    进口javax.swing.JPanel中;    公共类pong1继承JPanel {        静态音频剪辑音乐;
        静态的URL路径; //音效档路径        公共静态无效的主要(字串[] args){            PATH = pong1.class.getResource(Battle2.ogg); //变更网址
            音乐= Applet.newAudioClip(路径); //加载声音
            music.loop();
        }
    }


解决方案

下面是使用的 javazoom库

 进口的java.io.File;
进口javax.sound.sampled.AudioFormat中;
进口javax.sound.sampled.AudioInputStream中;
进口javax.sound.sampled.AudioSystem;
进口javax.sound.sampled.DataLine中;
进口javax.sound.sampled.SourceDataLine;
进口javazoom.spi.PropertiesContainer;公共类OGGPlayer {
    公众最终字符串文件名;    公共布尔mustStop = FALSE;
    公共OGGPlayer(字符串pFileName){
        文件名= pFileName;
    }    公共无效播放()抛出异常{
        mustStop = FALSE;
        档案文件=新的文件(文件名);
        的AudioInputStream的AudioInputStream = AudioSystem.getAudioInputStream(文件);
        如果(的AudioInputStream == NULL){
            抛出新的异常(无法获得音频输入流);
        }
        AudioFormat的baseFormat = audioInputStream.getFormat();
        AudioFormat的德codedFormat =新AudioFormat的(AudioFormat.Encoding.PCM_SIGNED,
            baseFormat.getSampleRate(),16,baseFormat.getChannels(),
            baseFormat.getChannels()* 2,baseFormat.getSampleRate(),假);
        德的AudioInputStream codedAudioInputStream = AudioSystem.getAudioInputStream(德codedFormat,
            的AudioInputStream);
        如果(!(德codedAudioInputStream的instanceof PropertiesContainer)){
            抛出新的异常(错PropertiesContainer实例);
        }        DataLine.Info dataLineInfo =新DataLine.Info(SourceDataLine.class,德codedFormat);
        SourceDataLine的SourceDataLine的=(的SourceDataLine)AudioSystem.getLine(dataLineInfo);
        sourceDataLine.open(德codedFormat);        字节[] = tempBuffer新的字节[4096];        //开始
        sourceDataLine.start();
        INT nbReadBytes = 0;
        而(nbReadBytes!= -1){
            如果(mustStop){
                打破;
            }
            nbReadBytes =去codedAudioInputStream.read(tempBuffer,0,tempBuffer.length);
            如果(nbReadBytes!= -1)
                sourceDataLine.write(tempBuffer,0,nbReadBytes);
        }        // 停止
        sourceDataLine.drain();
        sourceDataLine.stop();
        sourceDataLine.close();
        德codedAudioInputStream.close();
        audioInputStream.close();
    }    公共无效setMustStop(布尔pMustStop){
        mustStop = pMustStop;
    }    公共无效停止(){
        mustStop = TRUE;
    }}

简单地让音乐在后台打了调用这个类的一个新的您的应用程序。

So i'm trying to add sound to my pong game, but for some reason i can't seem to get the sounds to play... There are no error messages, meaning the path is correct, but the sounds simply don't play.

The following is my code for background music, with all the pong stuff cut out, thanks in advance~

    import java.applet.Applet;
    import java.applet.AudioClip;
    import java.net.URL;
    import javax.swing.JApplet;
    import javax.swing.JPanel;

    public class pong1 extends JPanel {

        static AudioClip music;
        static URL path; // soundfile path

        public static void main(String[] args) {

            path = pong1.class.getResource("Battle2.ogg"); // change url
            music = Applet.newAudioClip(path); // load sound
            music.loop();
        }
    }

解决方案

Here is a class that plays a OGG file using javazoom library:

import java.io.File;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;
import javazoom.spi.PropertiesContainer;

public class OGGPlayer {
    public final String fileName;

    public boolean mustStop = false;


    public OGGPlayer(String pFileName) {
        fileName = pFileName;
    }

    public void play() throws Exception {
        mustStop = false;
        File file = new File(fileName);
        AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(file);
        if (audioInputStream == null) {
            throw new Exception("Unable to get audio input stream");
        }
        AudioFormat baseFormat = audioInputStream.getFormat();
        AudioFormat decodedFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
            baseFormat.getSampleRate(), 16, baseFormat.getChannels(),
            baseFormat.getChannels() * 2, baseFormat.getSampleRate(), false);
        AudioInputStream decodedAudioInputStream = AudioSystem.getAudioInputStream(decodedFormat,
            audioInputStream);
        if (!(decodedAudioInputStream instanceof PropertiesContainer)) {
            throw new Exception("Wrong PropertiesContainer instance");
        }

        DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, decodedFormat);
        SourceDataLine sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo);
        sourceDataLine.open(decodedFormat);

        byte[] tempBuffer = new byte[4096];

        // Start
        sourceDataLine.start();
        int nbReadBytes = 0;
        while (nbReadBytes != -1) {
            if (mustStop) {
                break;
            }
            nbReadBytes = decodedAudioInputStream.read(tempBuffer, 0, tempBuffer.length);
            if (nbReadBytes != -1)
                sourceDataLine.write(tempBuffer, 0, nbReadBytes);
        }

        // Stop
        sourceDataLine.drain();
        sourceDataLine.stop();
        sourceDataLine.close();
        decodedAudioInputStream.close();
        audioInputStream.close();
    }

    public void setMustStop(boolean pMustStop) {
        mustStop = pMustStop;
    }

    public void stop() {
        mustStop = true;
    }

}

Simply call this class in a new Thread of your application so that the music is played in background.

这篇关于玩在eclipse OGG文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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