Results 1 to 10 of 10
  1. #1
    yourdaddy is offline Member
    Join Date
    May 2010
    Posts
    4
    Rep Power
    0

    Question Sound file player

    Hey, I'm writing a program that plays music files. I have gotten it to read in files fine using JFileChooser, but how can I actually get it to play them?

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by yourdaddy View Post
    Hey, I'm writing a program that plays music files. I have gotten it to read in files fine using JFileChooser, but how can I actually get it to play them?
    What you've done so far, can you show us here?

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,422
    Rep Power
    20

    Default

    JFileChooser is used for selecting, not reading, files, so your statement is probably based on an incorrect assumption. To get better help sooner, post a SSCCE* that clearly demonstrates your problem.
    * SSCCE : Java Glossary

    And post your code using code tags.

    db

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  5. #5
    yourdaddy is offline Member
    Join Date
    May 2010
    Posts
    4
    Rep Power
    0

    Default

    Alright thank you. sorry for not answering any sooner.

    I have the file chooser working and I can import the file and it show it in a text field. And know i wanna be able to play that file. You could tell me what i should look into on java api. Any tips help.

    This is the code i have so far


    Java Code:
    package javaapplicationover9000;
    
    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.SwingUtilities;
    
    
    
    public class Main extends JPanel
                                 implements ActionListener {
        static private final String newline = "\n";
        JButton openButton, saveButton;
        JTextArea log;
        JFileChooser fc;
    
        public Main() {
            super(new BorderLayout());
            log = new JTextArea(5,20);
            log.setMargin(new Insets(5,5,5,5));
            log.setEditable(false);
            JScrollPane logScrollPane = new JScrollPane(log);
            fc = new JFileChooser();
            openButton = new JButton("Open a File...",
                                     createImageIcon("http://www.java-forums.org/images/Open16.gif"));
            openButton.addActionListener(this);
            saveButton = new JButton("Save a File...",
                                     createImageIcon("http://www.java-forums.org/images/Save16.gif"));
            saveButton.addActionListener(this);
            JPanel buttonPanel = new JPanel();
            buttonPanel.add(openButton);
            buttonPanel.add(saveButton);
            add(buttonPanel, BorderLayout.PAGE_START);
            add(logScrollPane, BorderLayout.CENTER);
        }
    
        public void actionPerformed(ActionEvent e) {
    
            
            if (e.getSource() == openButton) {
                int returnVal = fc.showOpenDialog(Main.this);
    
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    File file = fc.getSelectedFile();
                   
                    log.append("Opening: " + file.getName() + "." + newline);
                } else {
                    log.append("Open command cancelled by user." + newline);
                }
                log.setCaretPosition(log.getDocument().getLength());
    
           
            } else if (e.getSource() == saveButton) {
                int returnVal = fc.showSaveDialog(Main.this);
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    File file = fc.getSelectedFile();
                   
                    log.append("Saving: " + file.getName() + "." + newline);
                } else {
                    log.append("Save command cancelled by user." + newline);
                }
                log.setCaretPosition(log.getDocument().getLength());
            }
        }
    
     
        protected static ImageIcon createImageIcon(String path) {
            java.net.URL imgURL = Main.class.getResource(path);
            if (imgURL != null) {
                return new ImageIcon(imgURL);
            } else {
                System.err.println("Couldn't find file: " + path);
                return null;
            }
        }
    
    
        private static void createAndShowGUI() {
           
            JFrame frame = new JFrame("FileChooserDemo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
     
            frame.add(new Main());
    
       
            frame.pack();
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
           
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    
                    UIManager.put("swing.boldMetal", Boolean.FALSE);
                    createAndShowGUI();
                }
            });
        }
    }
    Last edited by Eranga; 05-22-2010 at 01:36 PM. Reason: code tags added

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Please use code tags when you are posting here in the forum. Unformated codes are really hard to read. If you don't know how to do it, please check on my forum signature. You can see a link to relevant page.

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Regarding your question, read about the followings.

    java.io.BufferedReader
    import java.io.File;
    import java.io.FileReader;

    Other than that you've to use exceptions as well, but not that much interest at the initial step. Give a try and let us know.

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    I think it's better to write a sample code first of all, before implement in your application.

  9. #9
    yourdaddy is offline Member
    Join Date
    May 2010
    Posts
    4
    Rep Power
    0

    Default

    Thank you very much that helped a lot. I appriciate people spending time to answer my questions.

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by yourdaddy View Post
    Thank you very much that helped a lot. I appriciate people spending time to answer my questions.
    That's the idea of a forum. Helping each other to solve there problems.

    Do you found the solution for your question? If so it's time to mark the thread solved. :)

Similar Threads

  1. sound player
    By jperson in forum New To Java
    Replies: 1
    Last Post: 05-02-2010, 06:09 AM
  2. Replies: 0
    Last Post: 11-04-2009, 10:09 PM
  3. MP3 Player idea
    By vinoth in forum New To Java
    Replies: 3
    Last Post: 08-18-2009, 02:10 AM
  4. SWT Flash Player
    By forthe in forum SWT / JFace
    Replies: 0
    Last Post: 07-29-2008, 09:48 AM
  5. uploading sound file in java
    By po0oker in forum Advanced Java
    Replies: 8
    Last Post: 11-04-2007, 12:00 AM

Posting Permissions

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