MP3Player

MADDilli16

Grünschnabel
Hallo Leute!
Ich versuche einen Mp3Player zu erstellen, der unter Windoof und Linux läuft. Dafür ist Java eigentlich genau richtig. Hab jezt ein Plugin von der javazoom Seite gedownloadet. Das funktioniert auch. Nun hab ich aber ein Problem. Wenn ich auf den Play-Button drücke startet das Lied. Der Button bleibt aber hängen. Ich kann das Fenster nicht schließen und auf keinen anderen Button klicken bis das Lied zu Ende gelaufen ist.Denke das liegt an der while-Schleife beim Abspielen. Hoffe mir kann einer helfen.

Hier ist der Code:
Klasse 1

import java.awt.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.File;
import java.io.IOException;

import javax.sound.sampled.*;

class MediaPlayerPanel
extends JPanel implements LineListener, ActionListener{

SourceDataLine line;
AudioFormat decodedFormat;
AudioInputStream inputStream = null;
/**
* Konstruktor
* Verwaltung des JPaneld für den MediaPlayer
* @param rec Record
* Datentyp Record
*/
MediaPlayerPanel() {
setSize(300, 95);
setLocation(0, 0);
setBackground(Color.red);
setLayout(null);
setVisible(true);
add(pause());
add(start());
add(stop());

JLabel plus = new JLabel("+");
plus.setFont(new Font("", Font.BOLD, 15));
plus.setBounds(255, 30, 10, 10);

JLabel minus = new JLabel("-");
minus.setFont(new Font("", Font.BOLD, 15));
minus.setBounds(257, 55, 8, 8);

add(plus);
add(minus);
}

public void openFile() {

try {
File file = new File("test.mp3");
AudioInputStream in = AudioSystem.getAudioInputStream(file);
AudioFormat baseFormat = in.getFormat();
decodedFormat = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED,
baseFormat.getSampleRate(), 16, baseFormat.getChannels(),
baseFormat.getChannels() * 2, baseFormat.getSampleRate(),
false);

inputStream = AudioSystem.getAudioInputStream(decodedFormat, in);
DataLine.Info info = new DataLine.Info(SourceDataLine.class,
decodedFormat);
line = (SourceDataLine) AudioSystem.getLine(info);

}
catch (Exception e) {

}
}

public void testPlay(String filename) {
try {
File file = new File(filename);

AudioInputStream in = AudioSystem.getAudioInputStream(file);

AudioInputStream din = null;
AudioFormat baseFormat = in.getFormat();
AudioFormat decodedFormat = new AudioFormat(AudioFormat.Encoding.
PCM_SIGNED,
baseFormat.getSampleRate(),
16,
baseFormat.getChannels(),
baseFormat.getChannels() * 2,
baseFormat.getSampleRate(),
false);
inputStream = AudioSystem.getAudioInputStream(decodedFormat, in);
// Play now.
rawplay(decodedFormat, inputStream);
in.close();
}
catch (Exception e) {
System.out.println("Message" + e.getMessage());
}
}

private void rawplay(AudioFormat targetFormat, AudioInputStream din) throws
IOException, LineUnavailableException {
byte[] data = new byte[4096];
line = getLine(targetFormat);
line.addLineListener(this);
if (line != null) {
// Start
line.start();
int nBytesRead = 0, nBytesWritten = 0;
while (nBytesRead != -1) {

try {
nBytesRead = din.read(data, 0, data.length);
}
catch (Exception e) {
System.out.print(e.getMessage());
}

if (nBytesRead != -1) {
//System.out.println(nBytesWritten);
nBytesWritten = line.write(data, 0, nBytesRead);
}
}
//System.out.println(nBytesRead);
//System.out.println(nBytesWritten);


//while(!line.isRunning()){
//System.out.println(din.read());
//System.out.println(din.read());
//}

// Stop
line.drain();
line.stop();
line.close();
din.close();
}
}

private SourceDataLine getLine(AudioFormat audioFormat) throws
LineUnavailableException {
SourceDataLine res = null;
DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
res = (SourceDataLine) AudioSystem.getLine(info);
res.open(audioFormat);
return res;
}

public void update(LineEvent le) {
System.out.println("update" + le.getFramePosition());
}

/**
* liefert einen JButton zurück
* @return JButton
* JButton mit Stopeigenschaften
*/
public JButton stop() {
JButton stop = new JButton("Stop");
stop.setFocusable(false);
stop.setBounds(180, 15, 50, 30);
stop.setBorderPainted(false);
stop.addActionListener(this);
return stop;
}

public JButton pause() {
JButton pause = new JButton("Pause");
pause.setFocusable(false);
pause.setBounds(125, 5, 50, 30);
pause.setBorderPainted(false);
pause.addActionListener(this);
return pause;
}

public JButton start() {
JButton start = new JButton("Play");
start.setFocusable(false);
start.setVisible(true);
start.setBounds(70, 15, 50, 30);
start.setBorderPainted(false);
start.addActionListener(this);
return start;
}

public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("Play")){


testPlay("test.mp3");
}else{
if(e.getActionCommand().equals("Pause")){
System.out.println("Pause");
}else{
if(e.getActionCommand().equals("Stop")){
if (line != null) {
line.drain();
line.stop();
line.close();
try {
inputStream.close();
}
catch (Exception ex) {

}
}

}else{

}
}
}
}
}




Klasse 2

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;


class TFrame
extends JFrame
implements ActionListener{

TFrame() {
Container con = new Container();
MediaPlayerPanel panel = new MediaPlayerPanel();
getContentPane().add(con, BorderLayout.CENTER);

con.add(panel);
con.setBounds(0, 0, 640, 480);
setBounds(0, 0, 640, 480);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}

public void actionPerformed(ActionEvent e) {
}

public static void main(String[] args){
TFrame a = new TFrame();

}
}
 
Hallo!

Die GUI "friert" ein, da du den Track innerhalb des EventDispatch Threads abspielst, der so nicht mehr dazu kommt die GUI neu zu zeichnen und auf Ereignisse zu reagieren. Verlagere das abspielen des Tracks einfach in einen eigenen Thread.

Gruß Tom
 
Danke für die schnelle Antwort, Tom. Aber kannst du mir noch sagen wie man mit threads arbeitet oder eine gute Doku empfehlen? Hab damit noch nichts gemacht.

Danke im Voraus!
 
Danke nochmals für eure Hilfe. Hab grade das tutorial durchgelesen und mal ein, zwei sachen ausprobiert, aber ich hab leider keine Idee wie ich jetzt die parallelschaltung zwischen dem player und dem Frame hinbekomme!?

MFG
 
Ok. Hab es doch teilweise geschafft. Hab jetzt von Thread abgeleitet. Und rufe in der run()-Methode Play auf. Wenn ich jetzt den Player wieder mit der stop()-Methode stoppe ,kann ich kein weiteres Mal die run()-Methode aufrufen. Woran kann das liegen? Hab auch noch keine Idee zu dem Pause-Button!?

MFG
 
Kann mir noch einer sagen, wie man an die spielzeit der *wav bzw. *mp3 -Datei kommt? wollte den Befehl ausioFileFormat.getProperty(duration) eingeben. Leider gibt es dort kein getProperty(), obwohl dies in der API steht.

MFG
 

Neue Beiträge

Zurück