Java Help

Get help on programming - C++, Java, Delphi, etc.
Post Reply
Worship
Registered User
Posts: 505
Joined: 28 Jan 2006, 02:00
Location: Johannesburg

Java Help

Post by Worship »

Hi guys okay before I think RustyPup helped me with getting videos to work in java. However the user must select the file to play and I would want it rather to just open up a specific video. Attached is the two files of coding for making the video works. So any help would be appreciated


This is the file that needs to be changed to work for a specific video and not for a user chosen video
// Fig. 21.7: MediaTest.java
// A simple media player
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.JFileChooser;
import javax.swing.JFrame;

public class MediaTest
{
// launch the application
public static void main (String args [])
{
// create a file chooser
JFileChooser fileChooser = new JFileChooser ();

// show open file dialog
int result = fileChooser.showOpenDialog (null);

if (result == JFileChooser.APPROVE_OPTION) // user chose a file
{
URL mediaURL = null;


try
{
// get the file as URL
mediaURL = fileChooser.getSelectedFile ().toURL ();

} // end try
catch (MalformedURLException malformedURLException)
{
System.err.println ("Could not create URL for the file");
} // end catch

if (mediaURL != null) // only display if there is a valid URL
{
JFrame mediaTest = new JFrame ("Media Tester");
mediaTest.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

MediaPanel mediaPanel = new MediaPanel (mediaURL);
mediaTest.add (mediaPanel);

mediaTest.setSize (300, 300);
mediaTest.setVisible (true);
} // end inner if
} // end outer if
} // end main
} // end class MediaTest





And this is the MediaPanel


import java.net.URL;
import javax.media.CannotRealizeException;
import javax.media.Manager;
import javax.media.NoPlayerException;
import javax.media.Player;
import javax.swing.JPanel;

public class MediaPanel extends JPanel
{
public MediaPanel( URL mediaURL )
{
setLayout( new BorderLayout() ); // use a BorderLayout

// Use lightweight components for Swing compatibility
Manager.setHint( Manager.LIGHTWEIGHT_RENDERER, true );

try
{
// create a player to play the media specified in the URL
Player mediaPlayer = Manager.createRealizedPlayer( mediaURL );

// get the components for the video and the playback controls
Component video = mediaPlayer.getVisualComponent();
Component controls = mediaPlayer.getControlPanelComponent();

if ( video != null )
add( video, BorderLayout.CENTER ); // add video component

if ( controls != null )
add( controls, BorderLayout.SOUTH ); // add controls

mediaPlayer.start(); // start playing the media clip
} // end try
catch ( NoPlayerException noPlayerException )
{
System.err.println( "No media player found" );
} // end catch
catch ( CannotRealizeException cannotRealizeException )
{
System.err.println( "Could not realize media player" );
} // end catch
catch ( IOException iOException )
{
System.err.println( "Error reading from the source" );
} // end catch
} // end MediaPanel constructor
} // end class MediaPanel
User avatar
rustypup
Registered User
Posts: 8872
Joined: 13 Dec 2004, 02:00
Location: nullus pixius demonica
Contact:

Post by rustypup »

the MediaPanel is set up do do precisely that. all it requires is a valid url.

Code: Select all

JFrame mediaTest = new JFrame ("Media Tester"); 
mediaTest.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); 

MediaPanel mediaPanel = new MediaPanel (someOtherURL); 
mediaTest.add (mediaPanel); 

mediaTest.setSize (300, 300); 
mediaTest.setVisible (true);
the only change required within the MediaPanel is a some mechanism to invoke the mediaPlayer.start() outside of its instantiation. you'd prefer to have the user start the player....
Most people would sooner die than think; in fact, they do so - Bertrand Russel
Worship
Registered User
Posts: 505
Joined: 28 Jan 2006, 02:00
Location: Johannesburg

Post by Worship »

Sorry I didn't pick up what you said :(

And my problem is that the user has to choose which file to play rather than a file being automatically chosen. If you could show me changes in the coding like what to replace with what I would appreciate it
Worship
Registered User
Posts: 505
Joined: 28 Jan 2006, 02:00
Location: Johannesburg

Post by Worship »

Oh wait i understand what you mean but what is an example of a valid URL cause i have tried to use one (that i think is right) but it doesn't work
User avatar
rustypup
Registered User
Posts: 8872
Joined: 13 Dec 2004, 02:00
Location: nullus pixius demonica
Contact:

Post by rustypup »

post some sample code :!:

note: the sample provided was to give you an idea of how the JMF works. it's brittle and largely unusable in anything resembling production code. the design requires some major work before i would consider it a workable solution :wink:
Most people would sooner die than think; in fact, they do so - Bertrand Russel
Worship
Registered User
Posts: 505
Joined: 28 Jan 2006, 02:00
Location: Johannesburg

Post by Worship »

I actually think it works quite nicely really the only problem is that how to make an url for a video? Because if I "print" mediaURL then it give me like 'file/......" and so on but that only after selecting a file and if i just try to input mediaurl instead of choosing it, it says java.lang.string not relevant in java.lang.URL. so how do you make it URL?? thanks
User avatar
rustypup
Registered User
Posts: 8872
Joined: 13 Dec 2004, 02:00
Location: nullus pixius demonica
Contact:

Post by rustypup »

ok...

check out java.net.URL as well as the Java Tutorial to read up on how to use them... URL instantiation, when you know how, is very simple...

in its simplest form:

Code: Select all

try{
     java.net.URL u = new java.net.URL("file:c:\\pathToFile\\File.mov");
//or
     java.net.URL u = new java.net.URL("file:c:/pathToFile/File.mov");
}
catch(MalformedURLException mue){
     //handle error
}
Most people would sooner die than think; in fact, they do so - Bertrand Russel
Worship
Registered User
Posts: 505
Joined: 28 Jan 2006, 02:00
Location: Johannesburg

Post by Worship »

Thanks I will have a go at it later
Worship
Registered User
Posts: 505
Joined: 28 Jan 2006, 02:00
Location: Johannesburg

Post by Worship »

Rusty Thanks so so much it works perfectly got it in my program. :D :D :D :D :D
User avatar
rustypup
Registered User
Posts: 8872
Joined: 13 Dec 2004, 02:00
Location: nullus pixius demonica
Contact:

Post by rustypup »

:wink: good to hear!
Most people would sooner die than think; in fact, they do so - Bertrand Russel
Post Reply