# Volume bei Musikwiedergabe



## Frank1975 (28. Feb 2010)

Hallo zusammen,

mir fehlen die Grundlagen, in den untenstehenden MP3 Player Code (Teil)
eine Funktion Set Volume zu integrieren.

Ich hoffe jemand von Euch kann mir helfen.


```
package HauptPlayer;

import java.io.InputStream;

import javazoom.jl.decoder.Bitstream;
import javazoom.jl.decoder.BitstreamException;
import javazoom.jl.decoder.Decoder;
import javazoom.jl.decoder.Header;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.decoder.SampleBuffer;
import javazoom.jl.player.AudioDevice;
import javazoom.jl.player.FactoryRegistry;
import javazoom.jl.player.advanced.PlaybackEvent;
import javazoom.jl.player.advanced.PlaybackListener;

//import javax.sound.sampled.Control;
//import javazoom.jlgui.basicplayer.*;

public class HauptPlayer {

	private Bitstream bitstream;
	private Decoder decoder;
	private AudioDevice audio;
	private boolean closed = false;
	private boolean complete = false;
	private int lastPosition = 0;
	private PlaybackListener listener;

	public HauptPlayer(InputStream stream) throws JavaLayerException {
		this(stream, null);
	}

	public HauptPlayer(InputStream stream, AudioDevice device) throws JavaLayerException {
		bitstream = new Bitstream(stream);

		if (device != null)
			audio = device;
		else
			audio = FactoryRegistry.systemRegistry().createAudioDevice();
		audio.open(decoder = new Decoder());
	}

	public void play() throws JavaLayerException {
		play(Integer.MAX_VALUE);
	}

	public boolean playing = true;
	public boolean pause = false;

	public boolean play(int frames) throws JavaLayerException {
		boolean ret = true;
		if (listener != null)
			listener.playbackStarted(createEvent(PlaybackEvent.STARTED));
		while (frames-- > 0 && ret) {
			if (playing) {
				ret = decodeFrame();
			} else {
				try {
					frames++;
					Thread.sleep(10);
				} catch (Exception e) {
				}
			}
		}
		{
			AudioDevice out = audio;
			if (out != null) {
				System.out.println(audio.getPosition());
				out.flush();
				System.out.println(audio.getPosition());
				synchronized (this) {
					complete = (!closed);
					close();
				}

				// report to listener
				if (listener != null)
					listener.playbackFinished(createEvent(out, PlaybackEvent.STOPPED));
			}
		}
		return ret;
	}

	/**
	 * Schließt den player und stoppt alles
	 */
	public synchronized void close() {
		AudioDevice out = audio;
		if (out != null) {
			closed = true;
			audio = null;
			out.close();
			lastPosition = out.getPosition();
			try {
				bitstream.close();
			} catch (BitstreamException ex) {
			}
		}
	}

	/**
	 * Decodiert einzelne Frames
	 * 
	 * @return true wenn keine Frames mehr da sind zum decodieren, false andererseits.
	 */
	protected boolean decodeFrame() throws JavaLayerException {
		try {
			AudioDevice out = audio;
			if (out == null)
				return false;

			Header h = bitstream.readFrame();
			if (h == null)
				return false;

			// sample buffer set when decoder constructed
			SampleBuffer output = (SampleBuffer) decoder.decodeFrame(h, bitstream);

			synchronized (this) {
				out = audio;
				if (out != null) {
					out.write(output.getBuffer(), 0, output.getBufferLength());
				}
			}

			bitstream.closeFrame();
		} catch (RuntimeException ex) {
			throw new JavaLayerException("Exception decoding audio frame", ex);
		}
		return true;
	}

	protected boolean skipFrame() throws JavaLayerException {
		Header h = bitstream.readFrame();
		System.out.println(h.ms_per_frame());
		if (h == null)
			return false;
		bitstream.closeFrame();
		return true;
	}

	public boolean play(final int start, final int end) throws JavaLayerException {
		boolean ret = true;
		int offset = start;
		while (offset-- > 0 && ret)
			ret = skipFrame();
		return play(end - start);
	}

	private PlaybackEvent createEvent(int id) {
		return createEvent(audio, id);
	}

	private PlaybackEvent createEvent(AudioDevice dev, int id) {
		return null;// new PlaybackEvent(this, id, dev.getPosition());
	}

	public void setPlayBackListener(PlaybackListener listener) {
		this.listener = listener;
	}

	public PlaybackListener getPlayBackListener() {
		return listener;
	}

	public void stop() {
		listener.playbackFinished(createEvent(PlaybackEvent.STOPPED));
		close();
	}

	public synchronized boolean isComplete() {
		return complete;
	}

	public int getPosition() {
		int position = lastPosition;

		AudioDevice out = audio;
		if (out != null) {
			position = out.getPosition();
		}
		return position;
	}

	public void pause() {
		playing = false;
		pause = true;
	}

	public void weiter() {
		playing = true;
		pause = false;
	}

	public boolean ispause() {
		return pause;

	}

	public boolean isplaying() {
		// TODO Auto-generated method stub
		return playing;
	}
}
```


----------



## Frank1975 (8. Mrz 2010)

Kann denn jemand  mir sagen, was fehlt, damit ich antworten bekommen kann?

Irgendwie muss man doch im Übergang des decodierten Datenstroms zum Audiogerät die
Lautstärke regeln können


----------



## Frank1975 (29. Apr 2010)

ich versuchs nochmals mit einer anderen Frage....

Auch wenn es dann nicht plattformunabhängig ist, ist es möglich das Volume von Windows
(Hauptgain) einzustellen? Ich meine ohne auf einen Audiostream, der in Java gespielt wird, Bezug 
zu nehmen / einzuwirken.

Z.B. Windows Media player spielt etwas ab und ein Java Programm reguliert den MainGain exter...


----------



## Frank1975 (30. Apr 2010)

folgenden Code habe ich nun bereitstehen:

```
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Control;
import javax.sound.sampled.FloatControl;
import javax.sound.sampled.Line;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.Mixer;
import javax.sound.sampled.Port;
 
public class ControlTest {
 
	public static void main(String[] args) throws LineUnavailableException, InterruptedException {
		Mixer.Info[] mixerInfos = AudioSystem.getMixerInfo();
		for (int i = 0; i < mixerInfos.length; i++) {
			System.out.println("MIXER:\t" + mixerInfos[i]);
			Mixer mixer = AudioSystem.getMixer(mixerInfos[i]);
			Control[] controls = mixer.getControls();
			for (int j = 0; j < controls.length; j++) {
				System.out.println("\tmixer control:\t" + controls[j]);
			}
 
			try {
				mixer.getLine(Port.Info.LINE_OUT);
				System.out.println("got line_out");
			} catch (Exception e) {
			}
 
			Line[] sourceLines = mixer.getSourceLines();
			for (int j = 0; j < sourceLines.length; j++) {
				System.out.println("\tsource line:\t" + sourceLines[j]);
				Control[] lineControls = sourceLines[j].getControls();
				for (int k = 0; k < lineControls.length; k++) {
					System.out.println("\t\tline control:\t" + lineControls[k]);
				}
			}
			Line[] targetLines = mixer.getTargetLines();
			for (int j = 0; j < targetLines.length; j++) {
				System.out.println("\ttarget line:\t" + sourceLines[j]);
				Control[] lineControls = targetLines[j].getControls();
				for (int k = 0; k < lineControls.length; k++) {
					System.out.println("\t\tline control:\t" + lineControls[k]);
				}
			}
 
			// Line.Info[] sourceLineInfos = mixer.getSourceLineInfo();
			// for (int j = 0; j < sourceLineInfos.length; j++) {
			// System.out.println("\tsource line info:\t" + sourceLineInfos[j]);
			// setVolume(sourceLineInfos[j]);
			// }
			Line.Info[] targetLineInfos = mixer.getTargetLineInfo();
			for (int j = 0; j < targetLineInfos.length; j++) {
				System.out.println("\ttarget line info:\t" + targetLineInfos[j]);
				setVolume(targetLineInfos[j]);
			}
		}
	}
	static boolean setVolume(Line.Info lineInfo) {
		try {
			Line line = AudioSystem.getLine(lineInfo);
			line.open();
			System.out.println("\t*** line opened");
			FloatControl control = (FloatControl) line.getControl(FloatControl.Type.VOLUME);
			System.out.println("\t*** got volume control");
			System.out.println("\t\t\t*** old value = " + control.getValue());
			// set the volume to half its maximal value
			control.setValue(control.getMinimum() + (control.getMaximum() - control.getMinimum()) / 2f);
			System.out.println("\t\t\t*** new value = " + control.getValue());
			line.close();
		} catch (Exception e) {
			return false;
		}
		return true;
	}


}
```

Ausgabe:


> MIXER:	Primärer Soundtreiber, version Unknown Version
> MIXER:	Speakers (Realtek High Definition Audio), version Unknown Version
> MIXER:	Realtek Digital Output (Realtek High Definition Audio), version Unknown Version
> MIXER:	Primärer Soundaufnahmetreiber, version Unknown Version
> ...



Änderungen am new Value in allen Einstellungen ergeben trotzdem keine 
Änderung an der Lautstärke in der Ausgabe


----------

