java类javax.sound.sampled.LineListener的实例源码

MeicoApp.java 文件源码 项目:meico 阅读 17 收藏 0 点赞 0 评论 0
/**
 * playback start method extended by some gui stuff
 */
@Override
public void play() {
    try {
        super.play();
    } catch (LineUnavailableException | IOException e) {
        e.printStackTrace();
    }
    panel[2].setText("\u25A0");
    panel[2].setBackground(new Color(232, 232, 232));

    // listen to the audioClip and when it is finished playing, trigger the stop() method to clean up and reset the button
    LineListener listener = new LineListener() {
        public void update(LineEvent event) {
            if (event.getType() == LineEvent.Type.STOP) {
                stop();
            }
        }
    };
    this.getAudioClip().addLineListener(listener);
}
MultiClip.java 文件源码 项目:opsu-dance 阅读 19 收藏 0 点赞 0 评论 0
/**
 * Plays the clip with the specified volume.
 * @param volume the volume the play at
 * @param listener the line listener
 * @throws LineUnavailableException if a clip object is not available or
 *         if the line cannot be opened due to resource restrictions
 */
public void start(float volume, LineListener listener) throws LineUnavailableException {
    Clip clip = getClip();
    if (clip == null)
        return;

    // PulseAudio does not support Master Gain
    if (clip.isControlSupported(FloatControl.Type.MASTER_GAIN)) {
        // set volume
        FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
        float dB = (float) (Math.log(volume) / Math.log(10.0) * 20.0);
        gainControl.setValue(dB);
    }

    if (listener != null)
        clip.addLineListener(listener);
    clip.setFramePosition(0);
    clip.start();
}
Audio.java 文件源码 项目:neo 阅读 18 收藏 0 点赞 0 评论 0
/**
 * <strong><em>playSound</em></strong><br /><br />
 * 
 * &emsp;Plays a .wav audio file located in /res/audio/.<br />
 * &emsp;<em>E.g.</em> <sub>audio</sub><br /><br />
 * &emsp;File location would be: <sub>/res/audio/audio.wav</sub><br />
 * &emsp;and would be played automatically.
 * 
 * @param audio - File name.
 */
public void playSound(String audio){
    try{
        AudioInputStream audioInputStream =
            AudioSystem.getAudioInputStream(
                 getClass().getResource("/audio/"+audio+".wav"));
        Clip clip = AudioSystem.getClip();
        clip.open(audioInputStream);
        clip.start();
        clip.addLineListener(new LineListener() {

            @Override
            public void update(LineEvent arg0) {
                if(arg0.getFramePosition()==clip.getFrameLength()){
                    clip.close();
                }
            }
        });
        clips.put(audio, clip);
    }catch(Exception e){
        e.printStackTrace();
    }
}
Sound.java 文件源码 项目:WorldGrower 阅读 31 收藏 0 点赞 0 评论 0
private Clip openClip(boolean closeAfterPlaying) throws UnsupportedAudioFileException, IOException, LineUnavailableException {
    AudioInputStream audioStream = AudioSystem.getAudioInputStream(new ByteArrayInputStream(audioFilePath));
    DataLine.Info info = getLineInfo(audioStream);
    Clip audioClip = (Clip) AudioSystem.getLine(info);

    if (closeAfterPlaying) {
        audioClip.addLineListener(new LineListener() {
            @Override
            public void update(LineEvent myLineEvent) {
                if (myLineEvent.getType() == LineEvent.Type.STOP)
                    audioClip.close();
            }
        });
    }

    audioClip.open(audioStream);
    return audioClip;
}
TrainingExerciseBase.java 文件源码 项目:ShootOFF 阅读 19 收藏 0 点赞 0 评论 0
private static void playSound(File soundFile, Optional<LineListener> listener) {
    if (isSilenced) {
        System.out.println(soundFile.getPath());
        return;
    }

    if (!soundFile.isAbsolute()) {
        soundFile = new File(System.getProperty("shootoff.home") + File.separator + soundFile.getPath());
    }

    try {
        final AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(soundFile);
        playSound(audioInputStream, listener);
    } catch (UnsupportedAudioFileException | IOException e) {
        logger.error(String.format("Error reading sound file to play: soundFile = %s", soundFile), e);
    }
}
Player.java 文件源码 项目:MakamBox 阅读 27 收藏 0 点赞 0 评论 0
public void setPlayer(Wavefile af) throws Exception{
    clip = af.getClip();
       clip.open();
       fullLength = clip.getFrameLength();
       endPoint = fullLength;
       gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
       playing = false;
       clip.addLineListener(new LineListener(){
        @Override
        public void update(LineEvent arg0) {
            while(playing){
                int tempframe = clip.getFramePosition();
                MakamBoxAnalysis.positionSlide.setValue(tempframe);
                if (stopbutton!=null&&tempframe == fullLength){
                    stopbutton.doClick();
                } else if(stopbutton!=null && tempframe>=endPoint){
                    playAgain();
                } else if (tempframe == fullLength){
                    stop();
                }
            }
        }
       });
}
UI.java 文件源码 项目:txtUML 阅读 22 收藏 0 点赞 0 评论 0
private void playSirenSound() {
    try {
        File soundFile = new File(sirenFile);
        AudioInputStream soundIn = AudioSystem.getAudioInputStream(soundFile);
        AudioFormat format = soundIn.getFormat();
        DataLine.Info info = new DataLine.Info(Clip.class, format);
        clip = (Clip) AudioSystem.getLine(info);
        clip.addLineListener(new LineListener() {
            @Override
            public void update(LineEvent event) {
                if (event.getType() == LineEvent.Type.STOP) {
                    soundOn = false;
                }
            }
        });
        clip.open(soundIn);
        clip.start();
        soundOn = true;
    } catch (Exception e) {
        e.printStackTrace();
    }
}
MultiClip.java 文件源码 项目:opsu 阅读 19 收藏 0 点赞 0 评论 0
/**
 * Plays the clip with the specified volume.
 * @param volume the volume the play at
 * @param listener the line listener
 * @throws LineUnavailableException if a clip object is not available or
 *         if the line cannot be opened due to resource restrictions
 */
public void start(float volume, LineListener listener) throws LineUnavailableException {
    Clip clip = getClip();
    if (clip == null)
        return;

    // PulseAudio does not support Master Gain
    if (clip.isControlSupported(FloatControl.Type.MASTER_GAIN)) {
        // set volume
        FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
        float dB = (float) (Math.log(volume) / Math.log(10.0) * 20.0);
        gainControl.setValue(Utils.clamp(dB, gainControl.getMinimum(), gainControl.getMaximum()));
    } else if (clip.isControlSupported(FloatControl.Type.VOLUME)) {
        // The docs don't mention what unit "volume" is supposed to be,
        // but for PulseAudio it seems to be amplitude
        FloatControl volumeControl = (FloatControl) clip.getControl(FloatControl.Type.VOLUME);
        float amplitude = (float) Math.sqrt(volume) * volumeControl.getMaximum();
        volumeControl.setValue(Utils.clamp(amplitude, volumeControl.getMinimum(), volumeControl.getMaximum()));
    }

    if (listener != null)
        clip.addLineListener(listener);
    clip.setFramePosition(0);
    clip.start();
}
Process.java 文件源码 项目:maryspeak 阅读 20 收藏 0 点赞 0 评论 0
private void playAudio(AudioInputStream audio)
    {
        LineListener lineListener = new LineListener() 
        {
            public void update(LineEvent event) 
            {
                if (event.getType() == LineEvent.Type.START) { Log.print(Log.DEBUG,"Audio started playing."); }
                else if (event.getType() == LineEvent.Type.STOP) { Log.print(Log.DEBUG,"Audio stopped playing."); } 
                else if (event.getType() == LineEvent.Type.OPEN) { Log.print(Log.DEBUG,"Audio line opened."); } 
                else if (event.getType() == LineEvent.Type.CLOSE) { Log.print(Log.DEBUG,"Audio line closed."); }
            }
        };

        AudioPlayer ap = new AudioPlayer(audio, lineListener);
        ap.start();
}
VentanaInternaGrabador.java 文件源码 项目:Sistemas-Multimedia 阅读 17 收藏 0 点赞 0 评论 0
public VentanaInternaGrabador(final File f) {
    initComponents();

    recorder = new SMSoundPlayerRecorder(f);
    this.setTitle(f.getName());
    LineListener lineListener = new LineListener() {
        @Override
        public void update(LineEvent event) {
            if (event.getType() == Type.START) {
                recorderButton.setEnabled(false);
                stopButton.setEnabled(true);
            }
            if (event.getType() == Type.STOP) {
                recorderButton.setEnabled(true);
                stopButton.setEnabled(false);
                VentanaInternaReproductor vir = new VentanaInternaReproductor(f);
                VentanaPrincipal.getEscritorio().add(vir);
                vir.setVisible(true);
            }

        }
    };
    ((SMSoundPlayerRecorder) recorder).setLineListener(lineListener);
    this.pack();
}
VentanaInternaReproductor.java 文件源码 项目:Sistemas-Multimedia 阅读 16 收藏 0 点赞 0 评论 0
public VentanaInternaReproductor(File f) {
    initComponents();

    player = new SMSoundPlayerRecorder(f);
    this.setTitle(f.getName());
    LineListener lineListener = new LineListener() {
        @Override
        public void update(LineEvent event) {
            if (event.getType() == Type.START) {
                play.setEnabled(false);
                stop.setEnabled(true);
            }
            if (event.getType() == Type.STOP) {
                play.setEnabled(true);
                play.setSelected(false);
                stop.setEnabled(false);
            }
        }
    };
    ((SMSoundPlayerRecorder)player).setLineListener(lineListener);
    this.pack();
}
VentanaInternaGrabador.java 文件源码 项目:Sistemas-Multimedia 阅读 19 收藏 0 点赞 0 评论 0
public VentanaInternaGrabador() {
    initComponents();
    exist=true;
    recorder = new SMSoundPlayerRecorder(new File("nuevo"));
    LineListener lineListener = new LineListener() {
        @Override
        public void update(LineEvent event) {
            if (event.getType() == Type.START) {
                recorderButton.setEnabled(false);
                stopButton.setEnabled(true);
            }
            if (event.getType() == Type.STOP) {
                recorderButton.setEnabled(true);
                stopButton.setEnabled(false);
            }

        }
    };
    ((SMSoundPlayerRecorder) recorder).setLineListener(lineListener);
    this.pack();
}
VentanaInternaReproductor.java 文件源码 项目:Sistemas-Multimedia 阅读 15 收藏 0 点赞 0 评论 0
public VentanaInternaReproductor(File f) {
    initComponents();
    if (f != null) {
        player = new SMSoundPlayer(f);
        this.setTitle(f.getName());
        LineListener lineListener = new LineListener() {
            @Override
            public void update(LineEvent event) {
                if (event.getType() == Type.START) {
                    play.setEnabled(false);
                    stop.setEnabled(true);
                }
                if (event.getType() == Type.STOP) {
                    play.setEnabled(true);
                    play.setSelected(false);
                    stop.setEnabled(false);
                }
            }
        };
        ((SMSoundPlayer) player).setLineListener(lineListener);
        this.pack();
    }
}
JSAudio.java 文件源码 项目:Amber-IDE 阅读 18 收藏 0 点赞 0 评论 0
JSAudio(AudioInputStream as) throws LineUnavailableException, IOException {
    Mixer mix = AudioIO.findMixer(as.getFormat());
    clip = (Clip) (mix != null ? mix.getLine(new Line.Info(Clip.class)) : AudioSystem.getLine(new Line.Info(Clip.class)));
    clip.open(as);
    clip.addLineListener(new LineListener() {
        public void update(LineEvent event) {
            if (loop && event.getType() == Type.STOP) {
                EventQueue.invokeLater(new Runnable() {
                    public void run() {
                        clip.start();
                    }
                });
            }
        }
    });
}
JSAudioRecordingClip.java 文件源码 项目:romanov 阅读 18 收藏 0 点赞 0 评论 0
JSAudioRecordingClip(Clip clip, AudioMetaData mdata)
{
    c = clip;
    // because Clip doesn't give access to the loop count
    // we just loop it ourselves by triggering off of a STOP event
    c.addLineListener( new LineListener()
    {
        public void update(LineEvent event)
        {
            if ( event.getType().equals( LineEvent.Type.STOP ) )
            {
                if ( playing && loopCount != 0 )
                {
                    c.setMicrosecondPosition( 0 );
                    c.start();
                    if ( loopCount > 0 )
                    {
                        loopCount--;
                    }
                }
                else
                {
                    playing = false;
                }
            }
        }
    } );
    playing = false;
    loopCount = 0;
    meta = mdata;
}
AbstractLine.java 文件源码 项目:OpenJSharp 阅读 17 收藏 0 点赞 0 评论 0
public final void addLineListener(LineListener listener) {
    synchronized(listeners) {
        if ( ! (listeners.contains(listener)) ) {
            listeners.addElement(listener);
        }
    }
}
SoftMixingDataLine.java 文件源码 项目:OpenJSharp 阅读 20 收藏 0 点赞 0 评论 0
final void sendEvent(LineEvent event) {
    if (listeners.size() == 0)
        return;
    LineListener[] listener_array = listeners
            .toArray(new LineListener[listeners.size()]);
    for (LineListener listener : listener_array) {
        listener.update(event);
    }
}
SoftMixingMixer.java 文件源码 项目:OpenJSharp 阅读 18 收藏 0 点赞 0 评论 0
private void sendEvent(LineEvent event) {
    if (listeners.size() == 0)
        return;
    LineListener[] listener_array = listeners
            .toArray(new LineListener[listeners.size()]);
    for (LineListener listener : listener_array) {
        listener.update(event);
    }
}
AbstractLine.java 文件源码 项目:jdk8u-jdk 阅读 17 收藏 0 点赞 0 评论 0
public final void addLineListener(LineListener listener) {
    synchronized(listeners) {
        if ( ! (listeners.contains(listener)) ) {
            listeners.addElement(listener);
        }
    }
}
SoftMixingDataLine.java 文件源码 项目:jdk8u-jdk 阅读 20 收藏 0 点赞 0 评论 0
final void sendEvent(LineEvent event) {
    if (listeners.size() == 0)
        return;
    LineListener[] listener_array = listeners
            .toArray(new LineListener[listeners.size()]);
    for (LineListener listener : listener_array) {
        listener.update(event);
    }
}
SoftMixingMixer.java 文件源码 项目:jdk8u-jdk 阅读 18 收藏 0 点赞 0 评论 0
private void sendEvent(LineEvent event) {
    if (listeners.size() == 0)
        return;
    LineListener[] listener_array = listeners
            .toArray(new LineListener[listeners.size()]);
    for (LineListener listener : listener_array) {
        listener.update(event);
    }
}
AbstractLine.java 文件源码 项目:openjdk-jdk10 阅读 17 收藏 0 点赞 0 评论 0
@Override
public final void addLineListener(LineListener listener) {
    synchronized(listeners) {
        if ( ! (listeners.contains(listener)) ) {
            listeners.addElement(listener);
        }
    }
}
SoftMixingDataLine.java 文件源码 项目:openjdk-jdk10 阅读 21 收藏 0 点赞 0 评论 0
final void sendEvent(LineEvent event) {
    if (listeners.size() == 0)
        return;
    LineListener[] listener_array = listeners
            .toArray(new LineListener[listeners.size()]);
    for (LineListener listener : listener_array) {
        listener.update(event);
    }
}
SoftMixingMixer.java 文件源码 项目:openjdk-jdk10 阅读 32 收藏 0 点赞 0 评论 0
private void sendEvent(LineEvent event) {
    if (listeners.size() == 0)
        return;
    LineListener[] listener_array = listeners
            .toArray(new LineListener[listeners.size()]);
    for (LineListener listener : listener_array) {
        listener.update(event);
    }
}
SDLLinuxCrash.java 文件源码 项目:openjdk-jdk10 阅读 16 收藏 0 点赞 0 评论 0
public static SourceDataLine start() throws Exception {
    AudioFormat format = new AudioFormat(44100, 16, 2, true, false);
    if (addLen) {
        staticLen+=(int) (staticLen/5)+1000;
    } else {
        staticLen-=(int) (staticLen/5)+1000;
    }
    if (staticLen>8*44100*4) {
        staticLen = 8*44100*4;
        addLen=!addLen;
    }
    if (staticLen<1000) {
        staticLen = 1000;
        addLen=!addLen;
    }
    int len = staticLen;
    len -= (len % 4);
    out("    preparing to play back "+len+" bytes == "+bytes2Ms(len, format)+"ms audio...");

    DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
    SourceDataLine sdl = (SourceDataLine) AudioSystem.getLine(info);
    sdl.addLineListener(new LineListener() {
            public void update(LineEvent e) {
                if (e.getType() == LineEvent.Type.STOP) {
                    out("    calling close() from event dispatcher thread");
                    ((SourceDataLine) e.getSource()).close();
                }
                else if (e.getType() == LineEvent.Type.CLOSE) {
                }
            }
        });

    out("    opening...");
    sdl.open();
    out("    starting...");
    sdl.start();
    (new Thread(new SDLLinuxCrash(sdl, len))).start();
    return sdl;
}
AbstractLine.java 文件源码 项目:openjdk9 阅读 17 收藏 0 点赞 0 评论 0
public final void addLineListener(LineListener listener) {
    synchronized(listeners) {
        if ( ! (listeners.contains(listener)) ) {
            listeners.addElement(listener);
        }
    }
}
SoftMixingDataLine.java 文件源码 项目:openjdk9 阅读 31 收藏 0 点赞 0 评论 0
final void sendEvent(LineEvent event) {
    if (listeners.size() == 0)
        return;
    LineListener[] listener_array = listeners
            .toArray(new LineListener[listeners.size()]);
    for (LineListener listener : listener_array) {
        listener.update(event);
    }
}
SoftMixingMixer.java 文件源码 项目:openjdk9 阅读 18 收藏 0 点赞 0 评论 0
private void sendEvent(LineEvent event) {
    if (listeners.size() == 0)
        return;
    LineListener[] listener_array = listeners
            .toArray(new LineListener[listeners.size()]);
    for (LineListener listener : listener_array) {
        listener.update(event);
    }
}
SoundController.java 文件源码 项目:opsu-dance 阅读 22 收藏 0 点赞 0 评论 0
/**
 * Plays a sound clip.
 * @param clip the Clip to play
 * @param volume the volume [0, 1]
 * @param listener the line listener
 */
private static void playClip(MultiClip clip, float volume, LineListener listener) {
    if (clip == null)  // clip failed to load properly
        return;

       currentSoundComponent = clip;

    if (volume > 0f && !isMuted) {
        try {
            clip.start(volume, listener);
        } catch (LineUnavailableException e) {
            explode(String.format("Could not start a clip '%s'.", clip.getName()), e, DEFAULT_OPTIONS);
        }
    }
}
ClipPlayer.java 文件源码 项目:FxEditor 阅读 17 收藏 0 点赞 0 评论 0
public static void play(InputStream in)
{
    try
    {
        AudioInputStream stream = AudioSystem.getAudioInputStream(in);
        AudioFormat format = stream.getFormat();
        DataLine.Info info = new DataLine.Info(Clip.class, format);

        final Clip clip = (Clip)AudioSystem.getLine(info);
        clip.addLineListener(new LineListener()
        {
            public void update(LineEvent ev)
            {
                LineEvent.Type t = ev.getType();
                if(t == LineEvent.Type.STOP)
                {
                    clip.close();
                }
            }
        });
        clip.open(stream);
        clip.start();
    }
    catch(Exception e)
    {
        Log.ex(e);
    }
}


问题


面经


文章

微信
公众号

扫码关注公众号