如何在Java中停止音频?

时间:2017-02-04 15:07:40

标签: java audio

我想在调用getoutput方法时播放音乐,并在调用完成后停止播放音乐。我能够做到前者但不能做后者。在我完成调用getoutput方法后如何停止播放音乐?

import java.io.InputStream;
import javax.sound.sampled.AudioSystem;

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

public class Student {

    public void music() {

        try {
            // open the sound file as a Java input stream
            String gongFile = "C:\\Users\\wei liang\\Documents\\Programming fundamentals\\T7-Arrays\\Assignment\\TT.wav";
            InputStream in = new FileInputStream(gongFile);

            // create an audiostream from the inputstream
            AudioStream audioStream = new AudioStream(in);

            // play the audio clip with the audioplayer class
            AudioPlayer.player.start(audioStream);

        } catch (Exception ex) {
            System.out.println("Error! Can't find file.");
            ex.printStackTrace();
        }
    }
}

这是我调用音乐方法的主要方法,以及Student类中未显示的另一种方法。

public class StudentUser {

    //Main method
    public static void main(String args[]) {

        //creating a new Student object
        Student stud = new Student();

        //Calling the music method
        stud.music();

        //Calling the getoutput method
        stud.getoutput();

    }
}

1 个答案:

答案 0 :(得分:0)

我建议使用Thread,线程将保持有效直到播放音乐文件并且它将自动结束。

由于您未提供getoutput方法,因此您只需拨打music方法即可播放音乐并结束。

您的Student课程就像:

import javax.sound.sampled.*;
import java.io.File;

class Student {

    public synchronized void music(final String fileName, final SoundOptions mode) {
        Thread music = new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    AudioInputStream inputStream = AudioSystem.getAudioInputStream(
                            new File(fileName));

                    final Clip audioLineClip = (Clip) AudioSystem.getLine(
                            new Line.Info(Clip.class));
                    audioLineClip.open(inputStream);
                    audioLineClip.setFramePosition(0);

                    audioLineClip.addLineListener(new LineListener() {
                        @Override
                        public void update(LineEvent event) {
                            LineEvent.Type type = event.getType();
                            if (type == LineEvent.Type.OPEN) {
                            } else if (type == LineEvent.Type.CLOSE) {
                                System.exit(0);
                            } else if (type == LineEvent.Type.START) {
                            } else if (type == LineEvent.Type.STOP) {
                                audioLineClip.close();
                            }
                        }
                    });

                    switch (mode) {
                        case Stop:
                            audioLineClip.stop();
                            break;
                        case Play:
                            audioLineClip.start();
                            break;
                        case Loop:
                            audioLineClip.loop(Clip.LOOP_CONTINUOUSLY);
                            break;
                    }
                } catch (Exception e) {
                    System.err.println(e.getMessage());
                }
            }
        });

        if (mode != SoundOptions.Stop) {
            music.start();

            synchronized (music) {
                while (true) {
                    try {
                        music.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }

        } else {
            music.interrupt();
        }
    }


}

enum SoundOptions {
    Play, Loop, Stop
}

StudentUser类:

public class StudentUser {

    private static String gongFile = "C:\\Users\\wei liang\\Documents\\Programming fundamentals\\T7-Arrays\\Assignment\\TT.wav";

    //Main method
    public static void main(String args[]) {

        //Creating a new Student object
        Student stud = new Student();

        //Calling the music method and it is stops when music ends
        stud.music(gongFile, SoundOptions.Play);
    }
}