简体   繁体   中英

How to record voice on browser using applet in java

I am creating a web application in which recording is done through applet. When i run my program on applet viewer using eclipse , it records my voice and saves it into my computer but when i run the same using html file on browser it opens up the applet but doesn't record my voice.

Even i have signed my project jar file but this didn't make any difference. It always throw an exception like this java.security.AccessControlException: access denied (javax.sound.sampled.AudioPermission record) .

Here is sample code :

public class AudioRecorder extends JApplet {

private static final long serialVersionUID = 1L;
AudioFormat audioFormat;
TargetDataLine targetDataLine;
final JButton captureBtn = new JButton("Capture");
final JButton stopBtn = new JButton("Stop");    
final JPanel btnPanel = new JPanel();   
AudioFileFormat.Type[] fileTypes;   

@Override
public void init() {
    // TODO Auto-generated method stub
    super.init();
    new AudioRecorder();        
}

public AudioRecorder() {
    captureBtn.setEnabled(true);
    stopBtn.setEnabled(false);  

    captureBtn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            captureBtn.setEnabled(false);
            stopBtn.setEnabled(true);               
            captureAudio();
            }
        }
    );

    stopBtn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            captureBtn.setEnabled(true);
            stopBtn.setEnabled(false);                              
            stopAudio();                
            }           
        }
    );

    getContentPane().add(captureBtn);
    getContentPane().add(stopBtn);
    getContentPane().setLayout(new FlowLayout());       
    setSize(300, 120);
    setVisible(true);
}

private void captureAudio() {       
    try {           
        audioFormat = getAudioFormat();
        DataLine.Info dataLineInfo = new DataLine.Info(
                TargetDataLine.class, audioFormat);
        targetDataLine = (TargetDataLine) AudioSystem.getLine(dataLineInfo);
        new CaptureThread().start();
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(0);
    }
}

private void stopAudio() {      
        targetDataLine.stop();
        targetDataLine.close();                     
}

private AudioFormat getAudioFormat() {
    float sampleRate = 8000.0F;
    // 8000,11025,16000,22050,44100
    int sampleSizeInBits = 16;
    // 8,16
    int channels = 1;
    // 1,2
    boolean signed = true;
    // true,false
    boolean bigEndian = false;
    // true,false
    return new AudioFormat(sampleRate, sampleSizeInBits, channels, signed,
            bigEndian);
}

class CaptureThread extends Thread {
    public void run() {         
         AudioFileFormat.Type fileType = AudioFileFormat.Type.WAVE;             
         File audioFile = new File("audio." + fileType.getExtension());         
        try {
            targetDataLine.open(audioFormat);
            targetDataLine.start();
            AudioSystem.write(new AudioInputStream(targetDataLine),
                    fileType, audioFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

}

Here is HTML Page source code :

<HTML>
<HEAD>
</HEAD>
<BODY>
<div>
<APPLET CODE="AudioRecorder.class" WIDTH="800" HEIGHT="500">
<param name="permissions" value="sandbox">
</APPLET>
</div>
</BODY>
</HTML>

Please help me out where the actual problem is. Thanks in advance.

Thanks guys, i got some ideas from here and have resolved the issue today evening and now it's working perfectly.

I : I mentioned recorded sound path to save it to D drive of My Computer [ Note : This is hard-coded location just for testing purpose].

II : I have gone through the procedure to sign my project and granted required permissions using java docs http://docs.oracle.com/javase/tutorial/security/toolsign/ [This procedure is most important to fix this AudioPermission issue].

III : To create a separate policy file specially for my project using Policy Tool and keystore alias, i added FilePermission for all files and for all operations ie read,write,execute,delete . Once again, i added javax.sound.sampled.AudioPermission record, both have been added with signedBy my project's keystore alias name. [For more details go through last few steps provided in the link given above]

IV : I created a new HTML file, removed sandbox permission parameter tag from html file and included ARCHIVE="signedjar.jar" as an attribute in APPLET tag. So, the new HTML file code is -

<HTML>
<HEAD>
</HEAD>
<BODY>
<div>
<APPLET CODE="AudioRecorder.class" ARCHIVE="signedjar.jar" WIDTH="800" HEIGHT="500">
</APPLET>
</div>
</BODY>
</HTML>

[This one was used to load html directly on browser after everything was done successfully, so i kept it safe for future use and moved on to next step. Again an important thing, i kept html file, signed jar and newly created policy file into same folder. You can change directory or place these files on different location if you wish but in my case i was fixing the issue that's why i did so.]

V : I deleted cache files from temporary internet files option present in Java Control Panel [Go to Control Panel > Java > From Temporary internet files, click on settings > Delete files]

VI : I run html file again on browser and then clicked record button and finally it worked as expected.

I hope it will help others who are still facing this problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM