title: changing midi volume in java
body: <p>first of all changing volume is working but i got some problems while changing the volume so 1) after changing the volume like setting it to 0 (no volume) you still hearing parts of the song in the default value of the volume 2)after song changed the volume getting back to the default value of the volume</p>
  <pre><code>public static void setVolume(double value) {
      System.out.println();
      int CHANGE_VOLUME = 7;
      midivol =(value);
      try {
          if (synthesizer.getDefaultSoundbank() == null) {
              System.out.println(444);
                  ShortMessage volumeMessage = new ShortMessage();
                  for (int i = 0; i &lt; 16; i++) {
                      volumeMessage.setMessage(ShortMessage.CONTROL_CHANGE, i, CHANGE_VOLUME,(int)(value * 127.0));
                      volumeMessage.setMessage(ShortMessage.CONTROL_CHANGE, i, 39, (int)(value * 127.0));
                      MidiSystem.getReceiver().send(volumeMessage, -1);
                  }
          } else {  
               MidiChannel[] channels = synthesizer.getChannels();
  
                for( int c = 0; c &lt; channels.length; c++ ) {
                   if(channels[c] != null)   channels[c].controlChange( 7, (int)( value*127.0) );
                }
          }
          music.setSequence(sequence);
      } catch (Exception e) {
              e.printStackTrace();
          }
  }
  </code></pre>
  <p>playing midi:</p>
  <pre><code> private void playMidi(String location) {
      double gain =Slider.musicvolume;
      music = null;
      //synthesizer = null;
      sequence = null;
      File midiFile = new File(location);
      try {
      sequence = MidiSystem.getSequence(midiFile);
          music = MidiSystem.getSequencer(false);
          music.open();
          music.setSequence(sequence);
      } catch (Exception e) {
          System.err.println(&quot;Problem loading MIDI file.&quot;);
          e.printStackTrace();
          return;
      }
      if (music instanceof Synthesizer) {
          synthesizer = (Synthesizer) music;
      } else {
          try {
              synthesizer = MidiSystem.getSynthesizer();
              synthesizer.open();
              if (synthesizer.getDefaultSoundbank() == null) {
                  music.getTransmitter().setReceiver(MidiSystem.getReceiver());
              } else {
                  music.getTransmitter().setReceiver(synthesizer.getReceiver());
              }
          } catch (Exception e) {
              e.printStackTrace();
              return;
          }
      }
        MidiChannel[] channels = synthesizer.getChannels();
       for (int i = 0; i &lt; channels.length; i++) {
          channels[i].controlChange(7, (int) (gain * 127.0));
        }
       try {
          music.setSequence(sequence);
      } catch (InvalidMidiDataException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
      music.setLoopCount(Sequencer.LOOP_CONTINUOUSLY);
      music.start();
  
  }
  </code></pre>