Java

Moderators: zibadian
Number of threads: 7836
Number of posts: 18235

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
help me.. Posted by archanaanbu on 7 Feb 2011 at 11:07 PM
Hi,
i need to play audio file once my text file is opened. for eg, i have text file wit content in it as "hi hello". i have same audio file wit content "hi hello". so now if i open the text file using file chooser, it should play the corresponding audio file as hi hello. am doing in java wit netbeans ide. i used the following code to do this but am getting error in the code. please tel me wat i have to change in the code. thanks.
code:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.media.*;

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

public MediaPlayerDemo()
{
super( "Demonstrating the Java Media Player" );

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 );

// user clicked Cancel button on dialog
if ( result == JFileChooser.CANCEL_OPTION )
file = null;
else
file = fileChooser.getSelectedFile();
}

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

removePreviousPlayer();

try {
// create a new player and add listener
player = Manager.createPlayer( file.toURL() );
player.addControllerListener( new EventHandler() );
player.start(); // start player
}
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( visual );

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

public static void main(String args[])
{
MediaPlayerDemo app = new MediaPlayerDemo();

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

// inner class to handler events from media player
private class EventHandler implements ControllerListener {
public void controllerUpdate( ControllerEvent e ) {
if ( e instanceof RealizeCompleteEvent ) {
Container c = getContentPane();

// load Visual and Control components if they exist
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();
}
}
}
}




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.