在Applet程序中使用Runnable接口?

发布于 2024-08-03 06:07:16 字数 3290 浏览 2 评论 0原文

代码

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 文件。 我哪里做错了?

谢谢克里希纳

i am on developing Sound Recording applet.Here is the code

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());
        }
    }
  }

this program works fine for play back.
But while recording it should simultaneously write into the sample.wav file.
where am i doing wrong?

thanks

krishna

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瘫痪情歌 2024-08-10 06:07:16

您需要将文件代码包装在特权代码块中,默认情况下,您在沙箱中获得文件 io 权限,您需要通过访问控制器来调用该调用。

这是一个例子。

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

You need to wrap the file code in a privileged block of code, By default your in a sandbox to get file i.o. permissions you need to invoke the call through the accesscontroller.

Here is an example.

 final String location = "D:\\krishna\\sample.wav"
 File f = (File) AccessController.doPrivileged(new PrivilegedAction()
    {
        public Object run()
        {
            File outputFile = new File(location);
            return outputFile;
        }
    });
清醇 2024-08-10 06:07:16

尝试从小程序写入文件 - 它是沙盒的。

Trying to write to a file from an applet - it's sandboxed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文