Solved

Java Program that plays wma files

Posted on 2004-08-09
15
3,375 Views
Last Modified: 2013-11-23
This Java Program suppose to play wma files. However when I run my class I get errors.

Here is the program

_______________________________________________________________

// Imports
import java.applet.*;
import java.awt.*;
import java.util.Random;

public class MyMusicFiles extends Applet implements Runnable {
  AudioClip clip[] = new AudioClip[5];
  Thread    thread;
  Random    rand = new Random(System.currentTimeMillis());

  public void init() {

      clip[0] = getAudioClip(getDocumentBase(), "c:\Penny Lane.wma");
      clip[1] = getAudioClip(getDocumentBase(), "c:\Like a Rolling Stone.wma");
      clip[2] = getAudioClip(getDocumentBase(), "c:\You Shook Me.wma");
      clip[3] = getAudioClip(getDocumentBase(), "c:\Communication Breakdown.wma");
      clip[4] = getAudioClip(getDocumentBase(), "c:\Whole Lotta Love.wma");
  }

  public void start() {
    if (thread == null) {
      thread = new Thread(this);
      thread.start();
    }
  }

  public void stop() {
    if (thread != null) {
      thread.stop();
      thread = null;
    }
  }

  public void run() {
    while (Thread.currentThread() == thread) {
      // Loop the music sound
      clip[0].loop();

      while (true) {
        // Wait three seconds
        try {
          Thread.sleep(3000); }
        catch (InterruptedException ie) {
                 System.out.println(ie);}
        break;

        // Play an animal sound
        clip[(rand.nextInt() % 3) + 4].play();
      }
    }
  }

  public void paint(Graphics g) {
    Font        font = new Font("TimesRoman", Font.PLAIN, 20);
    FontMetrics fm = g.getFontMetrics(font);
    String      str = new String("On the farm...");
    g.setFont(font);
    g.drawString(str, (size().width - fm.stringWidth(str)) / 2,
      ((size().height - fm.getHeight()) / 2) + fm.getAscent());
  }
}

_______________________________________________________________
This is my MyMusicFiles.html

<HTML>
<HEAD>
<TITLE>A Simple Program</TITLE>
</HEAD>
<BODY>
Here is the output of my program:
<APPLET CODE="MyMusicFiles.class" WIDTH=150 HEIGHT=25>
</APPLET>
</BODY>
</HTML>

________________________________________________________________

When I run it I get these errors

C:\java>appletviewer MyMusicFiles.html
java.lang.NullPointerException
        at MyMusicFiles.run(MyMusicFiles.java:46)
        at java.lang.Thread.run(Thread.java:479)


Any ideas please

I compile the program with the following command

javac -d MyMusicFiles.java

Thanks
0
Comment
Question by:nickmcdonald
  • 6
  • 4
  • 3
15 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 11756106
Not sure if that file type is supported but that should be more like

clip[0] = getAudioClip(getDocumentBase(), "Penny Lane.wma");
0
 

Author Comment

by:nickmcdonald
ID: 11757690
No its still not working please help  me to play wma files through this Java Program.

Thanks
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11757758
Put the files in the same directory as the applet and try

clip[0] = getAudioClip(getCodeBase(), "Penny Lane.wma");
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:nickmcdonald
ID: 11757921
I copy all the music file in the same dir where this program however still having the same problem. Here

C:\java>appletviewer MyMusicFiles.html
java.lang.NullPointerException
        at MyMusicFiles.run(MyMusicFIles.java:46)
        at java.lang.Thread.run(Thread.java:479)


Please need help to play wma files.

THANKS
0
 
LVL 92

Expert Comment

by:objects
ID: 11758708
Applet do not support playing wma files.
0
 

Author Comment

by:nickmcdonald
ID: 11763718
Since Applet do not support to play wma files, is there anyone can tell me how I can change this application so that I don't need to use applet. Therefore program will play wma files?

THANKS!!
0
 

Author Comment

by:nickmcdonald
ID: 11767353
Well I have been having difficulties to play wma files from using the above java program.

Question is  there anyone can tell me how I can implement a Java program that will run in my PC or Network and picked up any music files such as MP3, wma, wva, midi (any kind of audio file) and play them.

This program will scan all the directories either on the PC or Network and run any kind of audio file.

Once it done playing a file then its pick up next audio file that is either in the same directory or in another directory.

The program ends when its done scanning all the directories and done playing all the music files.

Thanks in advance for your kind help!!!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11767421
You will need to obtain libraries for Java that support the various file formats and then integrate them into a signed applet
0
 

Author Comment

by:nickmcdonald
ID: 11767790
THANKS, however I have no clue where to start. Could you please tell explain be with some sample examples and then I can take it and enhance from there.

I would be monumental help.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11767877
You could start by looking on sourceforge for libraries for various file formats:

http://sourceforge.net/search/?type_of_search=soft&exact=1&forum_id=0&group_id=0&atid=0&words=java+mp3+file&Search=Search
0
 
LVL 92

Expert Comment

by:objects
ID: 11768103
You're not going to be able to support every format, suggest you start with the supported formats and build from there :)
0
 

Author Comment

by:nickmcdonald
ID: 11796303
I appreciated that you send me this link however I have no clue where to start. So

Is there anyone who can give me a sample program lets say that will play mp3 files or wma files?

In that case I have something ti start with.

Thanks in advance.

Nick
0
 
LVL 92

Accepted Solution

by:
objects earned 250 total points
ID: 11797952
for mp3 try:
http://www.javazoom.net/javalayer/javalayer.html

am not aware of a wma decoder.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question