I'm having problems using my AudioPlayer class. I have a class that calls it which simply contains the following:

public class Test
{
    public void test()
    {
        AudioPlayer ap1 = new AudioPlayer();
        ap1.printUsageAndExit();
        ap1.main({"a.wav"});
    }
}

I recieve the error on line 6, when I call

ap1.main({"a.wav"});

Note: When I use my audio player directly it has no problems, I am having problems using it from another class.
The audio player is a bit more complicated but follows for reference.

import java.io.File;
import java.io.IOException;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;

public class AudioPlayer
{
    private static final int EXTERNAL_BUFFER_SIZE=128000;
    
    public static void main(String[] args)
    {
        if (args.length != 1)
        {
            printUsageAndExit();
        }
        
        String strFilename = args[0];
        File soundFile = new File(strFilename);
        AudioInputStream audioInputStream=null;
        
        try
        {
            audioInputStream=AudioSystem.getAudioInputStream(soundFile);
        }
        catch(Exception e)
        {
            e.printStackTrace();
            System.exit(1);
        }
        AudioFormat audioFormat =audioInputStream.getFormat();
        SourceDataLine line = null;
        DataLine.Info info = new DataLine.Info(SourceDataLine.class,audioFormat);
        
        try
        {
            line = (SourceDataLine) AudioSystem.getLine(info);
            line.open(audioFormat);
        }
        catch (LineUnavailableException e)
		{
			e.printStackTrace();
			System.exit(1);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			System.exit(1);
		}
		
		line.start();
		
		int nBytesRead =0;
		byte[] abData=new byte[EXTERNAL_BUFFER_SIZE];
		while(nBytesRead != -1)
		{
		    try
		    {
		        nBytesRead=audioInputStream.read(abData,0,abData.length);
		    }
		    catch (IOException e)
			{
				e.printStackTrace();
			}
			if (nBytesRead >= 0)
			{
				int	nBytesWritten = line.write(abData, 0, nBytesRead);
			}
		}
		line.drain();
		line.close();
		System.exit(0);
        }
        
        public static void printUsageAndExit()
        {
            out("SimpleAudioPlayer: usage:");
            out("\tjava SimpleAudioPlayer <soundfile>");
            System.exit(1);
        }
        
        private static void out(String strMessage)
        {
            System.out.println(strMessage);
        }
  
}

Thanks in advance in anybody has a solution!

1. static method invoke in static form: CLASS . STATIC_METHOD_NAME ( PARAMETERS );
2. static definition of String array: new String[]{"a.wav"} // array length=1

public void test() {
    AudioPlayer.main(new String[]{"a.wav"});
}

Always post code of error.

Ah, cheers, that fixed it, I had tried using it as a static method, but I didn't realise you had to create the variable in the method call. Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.