Java 怎么设置播放MIDI音频的音量

2019年7月29日20:09:30Java 怎么设置播放MIDI音频的音量已关闭评论 543 1056字阅读3分31秒

1.简介

Java 怎么设置播放MIDI音频的音量?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.sound.midi.InvalidMidiDataException;
import javax.sound.midi.MidiChannel;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.MidiUnavailableException;
import javax.sound.midi.Sequence;
import javax.sound.midi.Sequencer;
import javax.sound.midi.Synthesizer;

public class Main {

  public void main(String[] argv) {
    try {
      
      Sequence sequence = MidiSystem.getSequence(new File("midifile"));

      
      sequence = MidiSystem.getSequence(new URL("http:

      
      Sequencer sequencer = MidiSystem.getSequencer();
      sequencer.open();
      sequencer.setSequence(sequence);
      

      if (sequencer instanceof Synthesizer) {
        Synthesizer synthesizer = (Synthesizer) sequencer;
        MidiChannel[] channels = synthesizer.getChannels();

        
        double gain = 0.9D;
        for (int i = 0; i < channels.length; i++) {
          channels[i].controlChange(7, (int) (gain * 127.0));
        }
      }
      
      sequencer.start();
    } catch (MalformedURLException e) {
    } catch (IOException e) {
    } catch (MidiUnavailableException e) {
    } catch (InvalidMidiDataException e) {
    }
  }
}

 
继续阅读
编程十万个怎么办