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.