|
is there any way to play mp3 and avi files in java ??
it needs to be platform independent..
|
|
|
Yes you can play MP3 and .wav files in Java, but you have to do some of the coding your self, google it!!! There is loads of examples of how to do it, although i have never tried AVI, but i would think it was possible
|
|
|
yup, got the java media framework..
i got it working..
but stuck with a new problem..
i have a table.. ans the listener listens to the row select..
the table has mp3 files..
so when the listener tells me the row, i can find the file and its path..
then create a player to play that music..
but the thing is when i click another row.. the music of the file file does not stop.. and the second file starts to play also.. so 2 files are playing on top of each other..
how do i kill the player, and re-create it.. ideas?
|
|
|
Its hard to say without seeing the source code. But you could have a condition in the while loop or for loop which is playing the song, so that when lets say a boolean called "stop" is equal to true the loop breaks. If you are using threads, then you could stop the particular thread, re-initialise everything and start the new song. As i said its hard to say without seeing your source code, so i have given you a couple of examples i have used in the past.
|
|
|
I found out how to fix this problem if using the play in the main class
but I am using a Jpanel class and not sure how ... i got the code..
this is the method i use in my main class
public void refresh_preview(URL media_file){
URL mediaURL = null;
mediaURL = media_file;
if ( mediaURL != null ) // only display if there is a valid URL
{
lbl_no_media_selected.setText("Loading Media File ... ");
MediaPanel mediaPanel = new MediaPanel( mediaURL );
mediaPanel.setVisible( true );
scrollPane2.setViewportView(mediaPanel);
} // end if
}
|
|
import java.awt.BorderLayout;
import java.awt.Component;
import java.io.IOException;
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, new Boolean(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
|
|
Currently, when i pass a new URL of the media file to the method above, it creates a new player, the video is fine, but I can still hear the audio from my first file..
How do i dispose of it..
then create a new one.. ?
|
|
|
|
|
|
|
// |