2013-11-27 95 views
0

我有一个applet,当我从eclipse运行时运行完美,但是当我将这个.java文件添加为.class时,它运行不正常。甚至没有彻底地发生任何错误。小程序没有运行

我尝试添加该class文件就像

<html> 
<head> 
<title>Sample Applet</title> 
</head> 
<body> 

<applet code=Server.class width=120 height=120></applet> 

</body> 
</html> 

简要说明这个类

此类接收音频作为一个字节传送到扬声器。

applet代码:

import java.applet.Applet; 
import java.io.ByteArrayInputStream; 
import java.io.IOException; 
import java.net.DatagramPacket; 
import java.net.DatagramSocket; 
import java.net.SocketException; 

import javax.sound.sampled.AudioFormat; 
import javax.sound.sampled.AudioInputStream; 
import javax.sound.sampled.AudioSystem; 
import javax.sound.sampled.DataLine; 
import javax.sound.sampled.FloatControl; 
import javax.sound.sampled.SourceDataLine; 

public class Server extends Applet{ 

    AudioInputStream audioInputStream; 
    AudioInputStream ais; 
    AudioFormat format; 
    boolean status = true; 
    int port = 50005; 
    int sampleRate = 8000; 

    @Override 
    public void start(){ 

     try { 
      DatagramSocket serverSocket = new DatagramSocket(50005); 

      /** 
      * Formula for lag = (byte_size/sample_rate)*2 
      * Byte size 9728 will produce ~ 0.45 seconds of lag. Voice slightly broken. 
      * Byte size 1400 will produce ~ 0.06 seconds of lag. Voice extremely broken. 
      * Byte size 4000 will produce ~ 0.18 seconds of lag. Voice slightly more broken then 9728. 
      */ 

      byte[] receiveData = new byte[5000]; 

      format = new AudioFormat(sampleRate, 16, 1, true, false); 

      while (status == true) { 
       DatagramPacket receivePacket = new DatagramPacket(receiveData, 
         receiveData.length); 

       serverSocket.receive(receivePacket); 

       ByteArrayInputStream baiss = new ByteArrayInputStream(
         receivePacket.getData()); 

       ais = new AudioInputStream(baiss, format, receivePacket.getLength()); 
       toSpeaker(receivePacket.getData()); 
      } 
     } 
     catch (SocketException e) 
     { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     catch (IOException e) 
     { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     super.start(); 
    } 


// public static void main(String args[]) throws Exception { 
// 
// 
// 
// } 

    public void toSpeaker(byte soundbytes[]) { 
     try { 

      DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, format); 
      SourceDataLine sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo); 

      sourceDataLine.open(format); 

      FloatControl volumeControl = (FloatControl) sourceDataLine.getControl(FloatControl.Type.MASTER_GAIN); 
      volumeControl.setValue(6.0206f); 

      sourceDataLine.start(); 
      sourceDataLine.open(format); 

      sourceDataLine.start(); 

      System.out.println("format? :" + sourceDataLine.getFormat()); 

      sourceDataLine.write(soundbytes, 0, soundbytes.length); 
      System.out.println(soundbytes.toString()); 
      sourceDataLine.drain(); 
      sourceDataLine.close(); 
     } catch (Exception e) { 
      System.out.println("Not working in speakers..."); 
      e.printStackTrace(); 
     } 
    } 
} 
+4

我不会点击链接。 – Maroun

+1

@MarounMaroun点击广告看魔术。什么都没发生:D –

+2

@sᴜʀᴇsʜᴀᴛᴛᴀ我做了,突然间我在一个废弃的岛屿醒来。 – Maroun

回答

0

<applet>标签没有在HTML 5支持莫非是这个问题?另外,applet标记的代码属性的值必须在引号看看这里的链接:http://www.w3schools.com/tags/tag_applet.asp

嵌入的小应用程序的新方法如下所示:https://eyeasme.com/Shayne/HTML5_APPLETS/

+0

感谢您的回复 – Anirban

+0

没有HTML5是不是问题 – Anirban

+0

*“''标签在HTML 5中不受支持。”*它也不支持在HTML 4.01中,但这并不会阻止浏览器制造商(明智地)支持HTML版本,它不是定义。 –