0

I'm attempting to create an applet for teaching languages, it plays a sound, then lets you record your voice, then plays back the sound again and then your voice. It has two buttons, one to move on to the next word, and another to repeat the same word (NOTE: repeat button does not exist yet).
However, when I press the button triggering this to happen it plays the sound then sits on the next step where it records indefinetely. I managed to narrow it down to the record(); method call with some crude debugging. When I run the exact same code outside of an applet it works fine, however inside an applet it doesn't. Any help would be apprieciated.

Note:It isn't finished so there may be loose ends around the place.

import java.applet.*;
import java.awt.*;
import javax.swing.*;

/**
 * Class Sound - write a description of the class here
 * 
 * @author (your name) 
 * @version (a version number)
 */
public class LanguageLab extends JApplet
{
    Button nextButton;
    Button againButton;
    int counter=0;
    String[] file={"a.wav","acht.wav"};
    String[] name=new String [2];
    String texts = new String("test");
    long length;
    public void init()
    {
        JRootPane rootPane = this.getRootPane();    
        rootPane.putClientProperty("defeatSystemEventQueueCheck", Boolean.TRUE);
        setLayout(new FlowLayout());
        nextButton=new Button("Next");
        nextButton.setEnabled(true);
        add(nextButton);
        againButton=new Button("Repeat");
        add(againButton);
    }

    public void start()
    {
    
    }
    
    public boolean action(Event e, Object args)
    {
        if(e.target==nextButton)
        {
            texts="1";
            repaint();
            length=play(file[counter]);
            texts="2"; repaint();
            record(length+500);
            texts="3"; repaint();
            play(file[counter]);
            texts="4";repaint();
            playback();
            texts="5";repaint();
            counter++;  
        }
        return true;
    }
    
    public long play(String file)
    {
        long length=AudioPlayer.main(new String[]{file});
        texts="6";repaint();
        return length;
    }
    
    public void record(long time)
    {
        AudioRecorder.main(new String[]{"rec.wav"},time);   
    }
    
    public void playback()
    {
        AudioPlayer.main(new String[]{"rec.wav"});   
    }

    public void stop()
    {
        // provide any code that needs to be run when page
        // is replaced by another page or before JApplet is destroyed 
    }

    public void paint(Graphics g)
    {
        // simple text displayed on applet
        g.setColor(Color.white);
        g.fillRect(0, 0, 200, 100);
        g.setColor(Color.black);
        String temp = new String();
        temp = Long.toString(length);
        g.drawString(texts, 20, 20);
    }

    public void destroy()
    {
        // provide code to be run when JApplet is about to be destroyed.
    }


    public String getAppletInfo()
    {
        // provide information about the applet
        return "Title:   \nAuthor:   \nA simple applet example description. ";
    }


    public String[][] getParameterInfo()
    {
        // provide parameter information about the applet
        String paramInfo[][] = {
                 {"firstParameter",    "1-10",    "description of first parameter"},
                 {"status", "boolean", "description of second parameter"},
                 {"images",   "url",     "description of third parameter"}
        };
        return paramInfo;
    }
}

and the record class it hangs on.

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

import javax.sound.sampled.DataLine;
import javax.sound.sampled.TargetDataLine;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.AudioFileFormat;


/**
 * Write a description of class SimpleAudioRecorder here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class AudioRecorder
extends Thread
{
    private TargetDataLine      m_line;
    private AudioFileFormat.Type    m_targetType;
    private AudioInputStream    m_audioInputStream;
    private File            m_outputFile;



    public AudioRecorder(TargetDataLine line,
                     AudioFileFormat.Type targetType,
                     File file)
    {
        m_line = line;
        m_audioInputStream = new AudioInputStream(line);
        m_targetType = targetType;
        m_outputFile = file;
    }
    
    public void start()
    {
        m_line.start();
        super.start();
    }
    
    public void stopRecording()
    {
        m_line.stop();
        m_line.close();
    }
    
    public void run()
    {
        try
        {
            AudioSystem.write(
            m_audioInputStream,
            m_targetType,
            m_outputFile);
        }
           
        catch(IOException e)
        {
            e.printStackTrace();
        }
    }
    public static void main(String[] args,long time)
    {
        if(args.length !=1 || args[0].equals("-h"))
        {
            printUsageAndExit();
        }
        String  strFilename = args[0];
        File    outputFile = new File(strFilename);
		AudioFormat	audioFormat = new AudioFormat(
			AudioFormat.Encoding.PCM_SIGNED,
			44100.0F, 16, 2, 4, 44100.0F, false);

        
       DataLine.Info	info = new DataLine.Info(TargetDataLine.class, audioFormat);
       TargetDataLine	targetDataLine = null;
       try
       {
           targetDataLine = (TargetDataLine) AudioSystem.getLine(info);
           targetDataLine.open(audioFormat);
        }
        catch (LineUnavailableException e)
        {
            out("unable to get a recording line");
            e.printStackTrace();
            System.exit(1);
        }
        AudioFileFormat.Type targetType=AudioFileFormat.Type.WAVE;
        AudioRecorder	recorder = new AudioRecorder(
                targetDataLine,
                targetType,
                outputFile);
        /*out("Press ENTER to start the recording.");
        try
        {
            System.in.read();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        
        recorder.start();
        out("Recording...");
        
        out("Press ENTER to stop the recording.");
        try
        {
            System.in.read();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        recorder.stopRecording();
        out("Recording stopped.");
        }*/
        recorder.start();
        long t0,t1;
        t0=System.currentTimeMillis();
        do{t1=System.currentTimeMillis();} while (t1-t0<time);
        recorder.stopRecording();
    }
        
    private static void printUsageAndExit()
        {
            out("SimpleAudioRecorder: usage:");
        	out("\tjava SimpleAudioRecorder -h");
        	out("\tjava SimpleAudioRecorder <audiofile>");
        }
        
    private static void out(String strMessage)
        {
            System.out.println(strMessage);
        }
}
1
Contributor
1
Reply
2
Views
7 Years
Discussion Span
Last Post by Room101
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.