Hi, Im trying to make a media player in Eclipse.
Its just a simple thing not an advanced one but i cant get it to work.
I have looked at some examples and tried to create a simular version.
I could really use some hints on how to make it play the files.
this is the code: (the problems are specified in the bottom of the thread)

MediaPlayer.java:

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.EventHandler;
import java.io.File;

import javax.sound.midi.ControllerEventListener;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;


public class MediaPlayer extends JFrame {
    private Player player;
    private File file;

    public MediaPlayer(){
        super ("demo");
        JButton openFile = new JButton("open file to play");
        openFile.addActionListener(
                new ActionListener() {
                    public void actionPerformed (ActionEvent e )
                    { 
                        openFile();
                        createPlayer();
                    }
                }
    );
        getContentPane().add(openFile,BorderLayout.NORTH);
        setSize(300,300);
        show();

    }

    private void openFile(){
        JFileChooser fileChooser=new JFileChooser();
        fileChooser.setFileSelectionMode(
                JFileChooser.FILES_ONLY);
        int result=fileChooser.showOpenDialog(this);
        if (result==JFileChooser.CANCEL_OPTION)
            file=null;
        else file=fileChooser.getSelectedFile();}

    private void createPlayer(){
        if (file==null) return;
        removePreviousPlayer();

        try{
            player=Manager.createRealizedPlayer(file.toURL());
            player.addControllerListener (new EventHandler());
            player.start();
        }
        catch (Exception e){JOptionPane.showMessageDialog( this,
                "Invalid file or location", "Error loading file",
                JOptionPane.ERROR_MESSAGE);}
        }
            private void removePreviousPlayer()
            {
            if (player==null)
                return;
            player.close();

            Component visual = player.getVisualComponent();
            Component control = player.getControlPanelComponent();

            Container c=getContentPane();

            if(visual !=null)
                c.remove(control);
            if (control !=null)
                c.remove(control);
            }

private class EventHandler implements ControllerListener {
    public void controllerUpdate (ControllerEvent e){
        if (e instanceof RealizeCompleteEvent){
         Container c=getContentPane();

            Component visualComponent= player.getVisualComponent();
                if (visualComponent !=null)
                c.add(visualComponent, BorderLayout.CENTER);

            Component controlsComponent=player.getControlPanelComponent();
                if (controlsComponent !=null)
                c.add(controlsComponent, BorderLayout.SOUTH);

            c.doLayout();

        }
    }}  
    }

Player.java

import java.awt.Component;

import MediaPlayer.EventHandler;


public class Player {

    /**
     * @param args
     */
    public static void main(String[] args) {


    }

    public void addControllerListener() {


    }

    public void start() {
        if (this != null)
            this.start();
    }

    public void close() {
        this.close();
    }

    public Component getVisualComponent() {

        return null;
    }

    public Component getControlPanelComponent() {

        return null;
    }
}

and the program: MPlayer.java

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;


public class MPlayer {

    /**
     * @param args
     */
    public static void main(String[] args) {
        MediaPlayer mp=new MediaPlayer();

        mp.addWindowListener(
                new WindowAdapter(){
                    public void windowClosing(WindowEvent e)
                    {System.exit(0);}
                });

    }

}

The player opens and you can choose files but i cant get it to play them.
As you see i am yet to specify the methods in Player.java.
Manager also cannot be resolved to a type, where do i create this?
and what do i do with it.
Any hints on what i have to do to make it work?
'Couse im really really outa my leauge here :P
The private class EventHandler also has some flaws if i may call it so :P
The Controller Listener , (ControllerEvent e) and RealizeCompleteEvent cannot be resolved to a type.
I would REALLY really appreciate some help on this one.

what kind of media files are you trying to play and do you have everything needed to play 'm installed?

can you be a bit more specific about what goes wrong, what errors you get, post the relevant code and try to use code tags. it'll make it a lot easier for us to read and easier to spot any problems

normal mp3 filers for a start would be good =)
problems occour here;

try{
player=Manager.createRealizedPlayer(file.toURL());
player.addControllerListener (new EventHandler());
player.start();
}

eclipse cant resolve thos to a type and suggest to create a class etc.

the other problem area is here ;

private class EventHandler implements ControllerListener {
public void controllerUpdate (ControllerEvent e){
if (e instanceof RealizeCompleteEvent){
Container c=getContentPane();

same problem cannot be resolved to a type.
i also suspect im missing something in my Player part , where some of the commands are only set to return null;

ive downladed JMF 2.1.1e Software.

make sure your classpath/project libraries contain all the info/jars your application needs.

hey it suddenly works :P
i tried ctrl+shift+o after re-installing the sun application =)
thx mate, guess its solved then =)
cool :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.