2009-08-27 4 views
-1

私は録音applet.Hereの開発に午前は、このプログラムは、プレイバックのために正常に動作コードアプレットプログラムでRunnable Interfaceを使用していますか?

import java.applet.*; 
import java.awt.*; 
import java.awt.event.*; 
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; 

/*<applet code="PlaySoundApplet.class" height=400 width=400></applet> */ 
public class PlaySoundApplet extends Applet implements ActionListener,Runnable 
{ 
    private volatile Thread PlaySound = null; 
    TargetDataLine  m_line; 
    AudioFileFormat.Type m_targetType; 
    AudioInputStream m_audioInputStream; 
    File   m_outputFile; 
    Button play,stop,Record; 
    AudioClip audioClip; 
    PlaySoundApplet recorder; 
     Runnable r; 

    public PlaySoundApplet(TargetDataLine line,AudioFileFormat.Type targetType,File file) 
    { 
     m_line=line; 
     m_targetType = targetType; 
     m_outputFile = file; 
     m_audioInputStream = new AudioInputStream(line); 
    } 
    public PlaySoundApplet() 
    { 

    } 


    public void init() 
    { 
    recorder = null; 
    play = new Button(" Play in Loop "); 
    add(play); 
    play.addActionListener(this); 
    stop = new Button(" Stop "); 
    add(stop); 
    stop.addActionListener(this); 
    Record = new Button("Record"); 
    add(Record); 
    Record.addActionListener(this); 
    audioClip = getAudioClip(getCodeBase(), "sample.wav"); 
    } 

    public void actionPerformed(ActionEvent ae) 
    { 
    Button source = (Button)ae.getSource(); 
    if (source.getLabel() == " Play in Loop ") 
    { 
     audioClip.play(); 
    } 
    else if(source.getLabel() == " Stop ") 
    { 
     audioClip.stop(); 
    } 
    else if(source.getLabel() == "Record") 
    { 
     System.out.println("debug 1"); 
     String strFilename = "D:\\krishna\\sample.wav"; 
     File outputFile = new File(strFilename); 
     System.out.println("debug 2"); 
     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) 
     { 
      System.out.println("unable to get a recording line"); 
      e.printStackTrace(); 
      System.exit(1); 
     } 
     AudioFileFormat.Type targetType = AudioFileFormat.Type.WAVE; 
     System.out.println("debug 3"); 
     PlaySoundApplet another = new PlaySoundApplet(targetDataLine,targetType,outputFile); 
     new Thread(another).start(); 
     } 
    } 
    public void run() 
    { 
    try 
     { 
     System.out.println("debug 5"); 
     AudioSystem.write(m_audioInputStream,m_targetType,m_outputFile); 
     } 
     catch(Exception e) 
     { 
      System.out.println(e.getMessage()); 
     } 
    } 
    } 

です。 しかし、録音中に同時にsample.wavファイルに書き込む必要があります。 どこが間違っていますか?

おかげアプレットからのファイルへの書き込みしようと

クリシュナ

+0

アプレットが署名し、それがファイルsuystemにアクセスすることができます右のセキュリティ権限を持っている場合アンディは – krishnakumar

答えて

0

特権コードブロックにファイルコードをラップする必要があります。デフォルトでは、ファイルi.oを取得するためにサンドボックス内にあります。 accesscontrollerを介して呼び出しを呼び出す必要があるアクセス許可。

ここは例です。

final String location = "D:\\krishna\\sample.wav" 
File f = (File) AccessController.doPrivileged(new PrivilegedAction() 
    { 
     public Object run() 
     { 
      File outputFile = new File(location); 
      return outputFile; 
     } 
    }); 
0

- それはサンドボックス化されます。

+1

白アプレットからのファイルの書き込みのおかげで、不可能ではありません。 – Jesper

+0

私はこのアプレットに署名しました – krishnakumar

関連する問題