Playing music with Java? (Not server-side.)

Discussion in 'Plugin Development' started by killgoblen, Jul 1, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    killgoblen

    Before you say anything, I know that I can't play music from the server without a client mod. I am no trying to. I am making a separate Java applet that will do things based on information from the server.

    What I want to know is what is the best way to play music with Java? I want to loop through a song, but be able to stop in the middle of a song and switch to another if needed.

    Thanks for any help!
     
  2. Offline

    obnoxint

  3. Offline

    killgoblen

    Thanks for that, but I can't get it to work. Here is my code:

    Code:
    import java.applet.Applet;
    import javax.media.*;
    
    import java.io.*;
    import java.net.URL;
    import java.net.MalformedURLException;
    import javax.sound.sampled.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    @SuppressWarnings({ "serial", "unused" })
    public class AreaMusic extends Applet implements ActionListener{
        Button button = new Button("Go!");
        TextField player = new TextField(20);
        TextArea output = new TextArea("Type your name in the box and click go!", 4, 60, TextArea.SCROLLBARS_NONE);
        Canvas line = new Canvas();
        public void init(){
            line.setSize(10000, 1);
            line.setBackground(getBackground());
            add(player);
            add(button);
            button.addActionListener(this);
            add(line);
            add(output);
        }
    
        @Override
        public void actionPerformed(ActionEvent event) {
            output.setText(player.getText());
            File song = new File("/Users/Timmy/Desktop/AreaMusic/AreaMusic/livinonaprayer.mp3");
            try {
                audioPlayer songPlayer = new audioPlayer(song);
                songPlayer.play();
            } catch (NoPlayerException e) {
                e.printStackTrace();
            } catch (CannotRealizeException e) {
                e.printStackTrace();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    import java.applet.Applet;
    import javax.media.*;
    
    import java.io.*;
    import java.net.URL;
    import java.net.MalformedURLException;
    import javax.sound.sampled.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    @SuppressWarnings("unused")
    public class audioPlayer {
        public Player audioPlayer = null;
        public audioPlayer(File file) throws NoPlayerException, CannotRealizeException, MalformedURLException, IOException{
            audioPlayer = Manager.createRealizedPlayer(file.toURI().toURL());
            //audioPlayer = Manager.createPlayer(file.toURI().toURL());
            //audioPlayer.prefetch();
            //audioPlayer.realize();
    
        }
        public void play() {
            audioPlayer.start();
        }
        public void stop() {
            audioPlayer.stop();
            audioPlayer.close();
        }
    
    }
    


    And when I do that, I get this error:

    Code:
    Failed to configure: com.sun.media.PlaybackEngine@6ab30913
      Bad header in the media: Couldn't detect stream type
    
    Error: Unable to realize com.sun.media.PlaybackEngine@6ab30913
    javax.media.CannotRealizeException
        at javax.media.Manager.blockingCall(Manager.java:2005)
        at javax.media.Manager.createRealizedPlayer(Manager.java:528)
        at audioPlayer.<init>(audioPlayer.java:16)
        at AreaMusic.actionPerformed(AreaMusic.java:33)
        at java.awt.Button.processActionEvent(Button.java:392)
        at java.awt.Button.processEvent(Button.java:360)
        at java.awt.Component.dispatchEventImpl(Component.java:4735)
        at java.awt.Component.dispatchEvent(Component.java:4565)
        at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:679)
        at java.awt.EventQueue.access$000(EventQueue.java:85)
        at java.awt.EventQueue$1.run(EventQueue.java:638)
        at java.awt.EventQueue$1.run(EventQueue.java:636)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
        at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
        at java.awt.EventQueue$2.run(EventQueue.java:652)
        at java.awt.EventQueue$2.run(EventQueue.java:650)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:649)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
    
     
  4. Offline

    AlbireoX

  5. Offline

    killgoblen

    Thanks, but the whole reason I am doing this is to avoid using client mods.

    And I'll have to check out that website...
     
  6. Offline

    Afforess

    So you want players to install a separate program on their computers, because you don't want to use a client mod? I'm not sure I see the logic in that decision.
     
  7. Offline

    killgoblen

    Please take a look at the OP. A Java Applet is a java program that resides on a website, and does NOT need to be downloaded to a computer to run.
     
  8. Offline

    wizjany

    Technically the applet is downloaded and run on the computer, but it's much cleaner than forcing players to mod their clients.
     
  9. Offline

    ItsHarry

    Can't you just use:
    Code:
    player = Manager.createPlayer(new URL(getCodeBase(),"BillyJean.mp3"));
    player.start();
    ??
     
Thread Status:
Not open for further replies.

Share This Page