如何重复在Java中的音频文件 [英] How to repeat audio file in java

查看:406
本文介绍了如何重复在Java中的音频文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我该如何获得音频文件连续重复?眼下只是音乐播放一次,而且,这样。我试图让声音重复一遍又一遍。我会得到音频文件的另一种方式,还是有更简单的方法?

code:

 进口的java.io.File;
进口java.io.FileInputStream中;
进口java.io.IOException异常;
进口的java.io.InputStream;进口sun.audio.AudioPlayer;
进口sun.audio.AudioStream;公共类声音{
    公共静态无效的音乐(字符串文件名){
        尝试{
            在的InputStream =新的FileInputStream(新文件(文件名));
            语音串流语音串流=新的语音串流(中);
            AudioPlayer.player.start(语音串流);
        }赶上(IOException异常五){
            e.printStackTrace();
        }
    }
}

我该如何称呼它:

  //播放音乐。
公共无效音乐(){
    Sound.music(RES /音乐/我喜欢你Hat.wav);
}


解决方案

  1. 请不要使用太阳。* 包,这些都是私人包和可能并不总是可用。看看这个例子中,了解如何获得一些想法的音频剪辑播放。

  2. 看看 剪辑 和的 剪辑#环

作为一个非常简单的例子...

 的AudioInputStream的AudioInputStream = AudioSystem.getAudioInputStream(...);
夹夹= AudioSystem.getClip();
clip.open(的AudioInputStream);
clip.loop(Clip.LOOP_CONTINUOUSLY);
clip.start();

How would i get the audio file to repeat continuously? Right now the music just plays once and that;s it. I am trying to get the sound to repeat over and over. Would I have to get the audio file another way or is there a simpler way?

Code:

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

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

public class Sound {
    public static void music(String fileName) {
        try {
            InputStream in = new FileInputStream(new File(fileName));
            AudioStream audioStream = new AudioStream(in);
            AudioPlayer.player.start(audioStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

How i call it:

// Plays the music.
public void music() {
    Sound.music("res/music/I Like Your Hat.wav");
}

解决方案

  1. Don't use sun.* packages, these are private packages and may not always be available. Take a look at this example for some ideas on how to get a audio clip to play.
  2. Take a look at Clip and Clip#loop

As a very simple example...

AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(...);
Clip clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.loop(Clip.LOOP_CONTINUOUSLY);
clip.start();

这篇关于如何重复在Java中的音频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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