Solved

Java Program that plays wma files

Posted on 2004-08-09
15
3,408 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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 will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

724 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