Solved

Java Program that plays wma files

Posted on 2004-08-09
15
3,388 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Question has a verified solution.

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

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

726 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