Results 1 to 10 of 10

Thread: help me

  1. #1
    archanaanbu is offline Member
    Join Date
    Feb 2011
    Posts
    21
    Rep Power
    0

    Unhappy help me

    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. in this code am getting error as
    1) import javax.media does not exists, in netbeans -import javax.media.*;
    2) in method createPalyer it shows cannot find symbol ," method add control listener"- player.addControllerListener( new EventHandler() );
    3) in class Event handler, it shows cannot find symbol "class RealizeCompleteEvent" -if ( e instanceof RealizeCompleteEvent ) {
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.net.URL;
    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);
                }
             }
          );
       }
    
        private URL file() {
            throw new UnsupportedOperationException("Not yet implemented");
        }
    
       // inner class to handler events from media player
       private class EventHandler implements ControllerListener {
            private Object event;
          public void controllerUpdate( ControllerEvent e ) {
              // <editor-fold defaultstate="collapsed" desc="comment">
              if (event instanceof RealizeCompleteEvent) {
                  // </editor-fold>
                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();
             }
          }
       }
    }
    Last edited by archanaanbu; 02-08-2011 at 08:06 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You'll want to edit your post above so you can paste code tags above and below your code block:

    [cod&#101;] above the code block

    [/cod&#101;] below the code block

    Next you'll want to show the actual error messages, and indicate with a LOUD comment in the code which line causes the error. Then we'll better be able to help you.

  3. #3
    archanaanbu is offline Member
    Join Date
    Feb 2011
    Posts
    21
    Rep Power
    0

    Default

    hi, i did as u said. please help me now

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by archanaanbu View Post
    hi, i did as u said. please help me now
    Of course it goes without saying that the code must be properly formatted to begin with. You wouldn't hand in all left-justified code for a class assignment or test or for a code review. The same etiquette applies here.

    Also, you should post the full error message, and again post a comment on the line that the error message occurs.

    Next time's a charm. Luck

  5. #5
    archanaanbu is offline Member
    Join Date
    Feb 2011
    Posts
    21
    Rep Power
    0

    Default

    i did it.. see now:(

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by archanaanbu View Post
    i did it.. see now:(
    I know you're trying, but all I see is unreadable code where all the lines are left-justified, are lined up on the first column, and no standard Java indentation to make it easier to identify code blocks. Which do you think is easier to read and undersand, left justified code?

    Java Code:
    public class TestPriorityQueue {
    public static void main(String[] args) {
    long[] noteTimes = {1103L, 0L, 500L, 0L, 220L, 1021212812012L};
    PriorityQueue<RecordedNote> noteQueue = new PriorityQueue<RecordedNote>(10,
    new Comparator<RecordedNote>() {
    @Override
    public int compare(RecordedNote o1, RecordedNote o2) {
    Long time1 = o1.getTime();
    Long time2 = o2.getTime();
    return time1.compareTo(time2);
    }
    });
    for (int i = 0; i < noteTimes.length; i++) {
    RecordedNote note = new RecordedNote(noteTimes[i]);
    System.out.println(note);
    noteQueue.add(note);
    }
    System.out.println();
    while (noteQueue.size() > 0) {
    System.out.println(noteQueue.remove());
    }
    }
    }


    or formatted code?

    Java Code:
        public class TestPriorityQueue {
            public static void main(String[] args) {
                long[] noteTimes = {1103L, 0L, 500L, 0L, 220L, 1021212812012L};
                PriorityQueue<RecordedNote> noteQueue = new PriorityQueue<RecordedNote>(10,
                   new Comparator<RecordedNote>() {
                       @Override
                       public int compare(RecordedNote o1, RecordedNote o2) {
                           Long time1 = o1.getTime();
                           Long time2 = o2.getTime();
                           return time1.compareTo(time2);
                       }
                   }
                );
                for (int i = 0; i < noteTimes.length; i++) {
                    RecordedNote note = new RecordedNote(noteTimes[i]);
                    System.out.println(note);
                    noteQueue.add(note);
                }
                System.out.println();
                while (noteQueue.size() > 0) {
                    System.out.println(noteQueue.remove());
                }
            }
        }
    Last edited by Fubarable; 02-08-2011 at 07:55 AM.

  7. #7
    archanaanbu is offline Member
    Join Date
    Feb 2011
    Posts
    21
    Rep Power
    0

    Default

    ya modified.

  8. #8
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    283
    Rep Power
    7

    Default

    As far as I know, java media framework is not a part of standard java, you need the required library files, or else probably you can have a look on sun.audio package although that is undocumented.

    Java Tip 24: How to play audio in applications - JavaWorld
    Swastik

  9. #9
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    283
    Rep Power
    7

  10. #10
    archanaanbu is offline Member
    Join Date
    Feb 2011
    Posts
    21
    Rep Power
    0

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •