0

Java デスクトップ アプリケーションでサウンド (.wma、.mp3 などの任意の形式の音楽ファイル) を再生するにはどうすればよいでしょうか? (アプレットではありません)

次のコードを使用しました (スタック オーバーフローに関する別の質問から取得) が、例外がスローされます。

public class playsound {
    public static void main(String[] args) {
s s=new s();
s.start();
    }
}
class s extends Thread{
    public void run(){
        try{
            InputStream in = new FileInputStream("C:\\Users\\srgf\\Desktop\\s.wma");
         AudioStream as =    new AudioStream(in); //line 26
            AudioPlayer.player.start(as);
        }
        catch(Exception e){
            e.printStackTrace();
            System.exit(1);
        }
    }
}

プログラムを実行すると、次の例外がスローされます。

java.io.IOException: could not create audio stream from input stream
    at sun.audio.AudioStream.<init>(AudioStream.java:82)
    at s.run(delplaysound.java:26)
4

3 に答える 3

0

JavaFX (JDK にバンドルされている) の使用は非常に簡単です。次のインポートが必要になります。

import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.util.Duration;

import java.nio.file.Paths;

手順:

JavaFX を初期化します。

new JFXPanel();

Media(サウンド)を作成します。

Media media = new Media(Paths.get(filename).toUri().toString());

MediaPlayerサウンドを再生する を作成します。

MediaPlayer player = new MediaPlayer(media);

そして、次を再生しMediaます。

player.play();

MediaPlayer.setStartTime()とを使用して、開始/停止時間も設定できますMediaPlayer.setStopTime()

player.setStartTime(new Duration(Duration.ZERO)); // Start at the beginning of the sound file
player.setStopTime(1000); // Stop one second (1000 milliseconds) into the playback

または、 で遊ぶのをやめることもできますMediaPlayer.stop()

オーディオを再生するサンプル関数:

public static void playAudio(String name, double startMillis, double stopMillis) {
    Media media = new Media(Paths.get(name).toUri().toString());
    MediaPlayer player = new MediaPlayer(media);

    player.setStartTime(new Duration(startMillis));
    player.setStopTime(new Duration(stopMillis));
    player.play();
}

詳細については、JavaFX javadocを参照してください。

于 2016-01-28T22:25:19.500 に答える
0

このライブラリを使用してください: http://www.javazoom.net/javalayer/javalayer.html

public void play() {
        String song = "http://www.ntonyx.com/mp3files/Morning_Flower.mp3";
        Player mp3player = null;
        BufferedInputStream in = null;
        try {
          in = new BufferedInputStream(new URL(song).openStream());
          mp3player = new Player(in);
          mp3player.play();
        } catch (MalformedURLException ex) {
        } catch (IOException e) {
        } catch (JavaLayerException e) {
        } catch (NullPointerException ex) {
        }

}

同様の質問をしているすべての人に役立つことを願っています:-)

于 2012-09-25T06:32:50.590 に答える
0

うーん。これは私のものの宣伝のように見えるかもしれませんが、ここで私の API を使用できます。

https://github.com/s4ke/HotSound

再生はこれで非常に簡単です。

代替手段: Java Clips (プリバッファリング) を使用する

... code ...
// specify the sound to play
File soundFile = new File("pathToYouFile");
//this does the conversion stuff for you if you have the correct SPIs installed
AudioInputStream inputStream = 
getSupportedAudioInputStreamFromInputStream(new FileInputStream(soundFile));

// load the sound into memory (a Clip)
DataLine.Info info = new DataLine.Info(Clip.class, inputStream.getFormat());
Clip clip = (Clip) AudioSystem.getLine(info);
clip.open(sound);

// due to bug in Java Sound, explicitly exit the VM when
// the sound has stopped.
clip.addLineListener(new LineListener() {
  public void update(LineEvent event) {
    if (event.getType() == LineEvent.Type.STOP) {
      event.getLine().close();
      System.exit(0);
    }
  }
});

// play the sound clip
clip.start();
... code ...

次に、次のメソッドが必要です。

public static AudioInputStream getSupportedAudioInputStreamFromInputStream(InputStream pInputStream) throws UnsupportedAudioFileException,
        IOException {
    AudioInputStream sourceAudioInputStream = AudioSystem
            .getAudioInputStream(pInputStream);
    AudioInputStream ret = sourceAudioInputStream;
    AudioFormat sourceAudioFormat = sourceAudioInputStream.getFormat();
    DataLine.Info supportInfo = new DataLine.Info(SourceDataLine.class,
            sourceAudioFormat,
            AudioSystem.NOT_SPECIFIED);
    boolean directSupport = AudioSystem.isLineSupported(supportInfo);
    if(!directSupport) {
        float sampleRate = sourceAudioFormat.getSampleRate();
        int channels = sourceAudioFormat.getChannels();
        AudioFormat newFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
                sampleRate,
                16,
                channels,
                channels * 2,
                sampleRate,
                false);
        AudioInputStream convertedAudioInputStream = AudioSystem
                .getAudioInputStream(newFormat, sourceAudioInputStream);
        sourceAudioFormat = newFormat;
        ret = convertedAudioInputStream;
    }
    return ret;
}

クリップの例のソース (私が少し変更しています): http://www.java2s.com/Code/Java/Development-Class/AnexampleofloadingandplayingasoundusingaClip.htm

SPI は、クラスパスに .jar を追加することで追加されます

mp3 の場合は次のとおりです。

于 2012-09-27T01:01:01.763 に答える