# Seek in MP3 Stream



## MasterHimself (25. Oktober 2005)

Ich habe ein schwerwiegendes Problem,

ich nutze die Mp3spi Klassen, um in Java einen MP3 Player wie Winamp zu bauen,
das einzigste was mir noch fehlt, ist eine Methode im Player, die die MP3 Datei
an eine bestimmte Position setzt. Ich habe keine Ideen wie ich das anstellen soll, 
goggle hat mir auch nicht geholfen...


```
package mediaplayer.player;

import mediaplayer.playlist.*;
import java.io.*;
import javax.sound.sampled.*;

public class Player extends Thread{
	private static final long serialVersionUID = 100;

private static File track;
private static AudioInputStream mp3Stream;
private static AudioInputStream wavStream;
private static AudioFormat baseFormat;
private static AudioFormat decodedFormat;
private static SourceDataLine line;
private static boolean pause = false;
private static boolean stopped = true;
private static boolean started = false;
private static double time;
private static FloatControl settings;
private static BooleanControl advsettings;


	
	public static void startbreakPlay(){
		if(pause){
			pause = false;
		}
		else{
		if(stopped){
			stopped = false;
			initPlay();
			if(! started){
				started = true;
				Player p1 = new Player();
				p1.start();
			}
		}
		else{
			pause = true;
		}
		}
	}
	
	public static void stopPlay(){
		if(! stopped){
			stopped = true;
		}
	}
	
	public static void goForward(){
		stopPlay();
		Playlist.goForward();
		startbreakPlay();
	}
	
	public static void goBackward(){
		stopPlay();
		Playlist.goBackward();
		startbreakPlay();
	}
	
	public static String getPosition(){
		long tmp = Math.round(100*time);
		return String.valueOf(tmp/100.0d);
	}
	
	public static double getBalance(){
		settings = (FloatControl) line.getControl(FloatControl.Type.BALANCE);
		return settings.getValue();
	}
	
	public static void setBalance(double value){
		settings = (FloatControl) line.getControl(FloatControl.Type.BALANCE);
		settings.setValue((float)value);
	}
	
	public static boolean getMute(){
		advsettings = (BooleanControl) line.getControl(BooleanControl.Type.MUTE);
		return advsettings.getValue();
	}
	
	public static void setMute(boolean value){
		advsettings = (BooleanControl) line.getControl(BooleanControl.Type.MUTE);
		advsettings.setValue(value);
	}
	
	public static double getMasterGain(){
		settings = (FloatControl) line.getControl(FloatControl.Type.MASTER_GAIN);
		return settings.getValue();
	}
	
	public static void setMasterGain(double value){
		settings = (FloatControl) line.getControl(FloatControl.Type.MASTER_GAIN);
		settings.setValue((float)value);
	}
	
	public void run(){
		try{
		byte[] data = new byte[4096];
		int nBytesRead = 0;
		if(line != null){
			while(nBytesRead != -1){
				while(pause) Thread.sleep(10);
				while(stopped) Thread.sleep(10);
				time = line.getMicrosecondPosition()/1000000.0d;
				nBytesRead = wavStream.read(data,0,data.length);
				line.write(data,0,nBytesRead);
			}
		}
		}
		catch(Exception ex){}
		started = false;
		goForward();
	}
	
	private static void initPlay(){
		try{
		track = new File(Playlist.getCurrentEntry());
		mp3Stream = AudioSystem.getAudioInputStream(track);
		
		baseFormat = mp3Stream.getFormat();
		decodedFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
				                        baseFormat.getSampleRate(),
				                        16,
				                        baseFormat.getChannels(),
				                        baseFormat.getChannels()*2,
				                        baseFormat.getSampleRate(),
				                        false);
		
		wavStream = AudioSystem.getAudioInputStream(decodedFormat, mp3Stream);
		line = getLine(decodedFormat);
		line.start();
		}
		catch(Exception ex){}
	}
	
	private static SourceDataLine getLine(AudioFormat format) throws LineUnavailableException{
		SourceDataLine tmp = null;
		DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
		tmp = (SourceDataLine) AudioSystem.getLine(info);
		tmp.open(format);
		return tmp;
	}
}
```

Wenn jemand eine Idee oder auch nur einen Ansatz für das Problem weiß, wäre ich sehr 
dankbar.


----------



## Looky (16. Februar 2008)

Hi,

ich weiß, das Thema ist schon ziemlich alt aber ich würd gerne wissen ob du eine Lösung gefunden hast und wenn ja ob du die mal posten kannst, daich gerade bei demselben problem sitze..

Thx
Chriz


----------

