简体   繁体   English

如何将音频数据从Java Applet发送到Rails控制器

[英]How to send audio data from Java Applet to Rails controller

I have to send the audio data in byte array obtain by recording from java applet at the client side to rails server at the controller in order to save. 为了保存,我必须将通过从客户端的java applet录制获得的字节数组形式的音频数据发送到控制器的rails服务器。

So, what encoding parameters at the applet side be used and in what form the audio data be converted like String or byte array so that rails correctly recieve data and then I can save that data at the rails in the file. 因此,可以使用applet端的哪些编码参数,以及以哪种形式转换音频数据(例如String或byte数组),从而使rails正确接收数据,然后可以将该数据保存在文件中的rails上。 As currently the audio file made by rails controller is not playing. 目前,rails控制器制作的音频文件未播放。 It is the following ERROR : 它是以下错误:

  LAVF_header: av_open_input_stream() failed

while playing with the mplayer. 与mplayer一起玩时。

Here is the sample Java Code im using in which im reading audio data from the audio file: 这是示例Java代码im,在其中使用im从音频文件中读取音频数据:

package networksocket;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JApplet;
import java.net.*;
import java.io.*;
import java.awt.event.*;
import java.awt.*;
import java.sql.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.util.Properties;
import javax.swing.plaf.basic.BasicSplitPaneUI.BasicHorizontalLayoutManager;
import sun.awt.HorizBagLayout;
import sun.awt.VerticalBagLayout;
import sun.misc.BASE64Encoder;
/**
 *
  * @author mukand
 */
public class Urlconnection extends JApplet implements ActionListener
{

/**
 * Initialization method that will be called after the applet is loaded
 * into the browser.
 */


public BufferedInputStream in;
public BufferedOutputStream out;
public String line;
public FileOutputStream file;
public int bytesread;
public int toread=1024;
byte b[]= new byte[toread];
public String f="FINISH";
public String match;
public File fileopen;
public JTextArea jTextArea;
public Button refreshButton;
public HttpURLConnection urlConn;
public URL url;
OutputStreamWriter wr;
BufferedReader rd;

@Override
public void init() {
    // TODO start asynchronous download of heavy resources
    //textField= new TextField("START");
    //getContentPane().add(textField);
    JPanel p = new JPanel();
    jTextArea= new JTextArea(1500,1500);
    p.setLayout(new GridLayout(1,1, 1,1));
    p.add(new JLabel("Server Details"));
    p.add(jTextArea);
    Container content = getContentPane();
    content.setLayout(new GridBagLayout()); // Used to center the panel
    content.add(p);
    jTextArea.setLineWrap(true);

    refreshButton = new java.awt.Button("Refresh");
    refreshButton.reshape(287,49,71,23);
    refreshButton.setFont(new Font("Dialog", Font.PLAIN, 12));
    refreshButton.addActionListener(this);
    add(refreshButton);
    Properties properties = System.getProperties();
    properties.put("http.proxyHost", "netmon.iitb.ac.in");
    properties.put("http.proxyPort", "80");

}
@Override
public void actionPerformed(ActionEvent e)
{
    try
    {
        url = new URL("http://localhost:3000/audio/audiorecieve");
        urlConn = (HttpURLConnection)url.openConnection();
        //String login = "mukandagarwal:rammstein$";
        //String encodedLogin = new BASE64Encoder().encodeBuffer(login.getBytes());
        //urlConn.setRequestProperty("Proxy-Authorization",login);
        urlConn.setRequestMethod("POST");

       // urlConn.setRequestProperty("Content-Type",
       //"application/octet-stream");
        //urlConn.setRequestProperty("Content-Type","audio/mpeg");//"application/x-www- form-urlencoded");
        //urlConn.setRequestProperty("Content-Type","application/x-www- form-urlencoded");
        //urlConn.setRequestProperty("Content-Length", "" +
          // Integer.toString(urlParameters.getBytes().length));
        urlConn.setRequestProperty("Content-Language", "UTF-8");
        urlConn.setDoOutput(true);
        urlConn.setDoInput(true);
        byte bread[]=new byte[2048];
        int iread;
        char c;
        String data=URLEncoder.encode("key1", "UTF-8")+ "=";
        //String data="key1=";
        FileInputStream fileread= new FileInputStream("//home//mukand//Hellion.ogg");//Dogs.mp3");//Desktop//mausam1.mp3");
        while((iread=fileread.read(bread))!=-1)
        {
            //data+=(new String());
            /*for(int i=0;i<iread;i++)
            {
                //c=(char)bread[i];
                System.out.println(bread[i]);

            }*/
          data+= URLEncoder.encode(new String(bread,iread), "UTF-8");//new String(new String(bread));//
        //    data+=new String(bread,iread);
        }
        //urlConn.setRequestProperty("Content-Length",Integer.toString(data.getBytes().length));
        System.out.println(data);
        //data+=URLEncoder.encode("mukand", "UTF-8");
        //data += "&" + URLEncoder.encode("key2", "UTF-8") + "=" + URLEncoder.encode("value2", "UTF-8");
        //data="key1=";
        wr = new OutputStreamWriter(urlConn.getOutputStream());//urlConn.getOutputStream();
        //if((iread=fileread.read(bread))!=-1)
          //  wr.write(bread,0,iread);
        wr.write(data);
        wr.flush();

        fileread.close();
        jTextArea.append("Send");

        // Get the response
        rd = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
        while ((line = rd.readLine()) != null) {
            jTextArea.append(line);
        }
        wr.close();
        rd.close();
            //jTextArea.append("click");

    }
    catch (MalformedURLException ex) {
        Logger.getLogger(Urlconnection.class.getName()).log(Level.SEVERE, null, ex);
    }
    catch (IOException ex) {
            Logger.getLogger(Urlconnection.class.getName()).log(Level.SEVERE, null, ex);
        }

}
@Override
public void start()
{
}

@Override
public void stop()
{

}
@Override
public void destroy()
{

}

// TODO overwrite start(), stop() and destroy() methods

}

Here is the Rails controller function for recieving: 这是用于接收的Rails控制器功能:

 def audiorecieve
 puts "///////////////////////////////////////******RECIEVED*******////"
 puts params[:key1]#+" "+params[:key2]
 data=params[:key1]
 #request.env('RAW_POST_DATA')
 file=File.new("audiodata.ogg", 'w')
 file.write(data)
 file.flush
 file.close
 puts  "////**************DONE***********//////////////////////"
 end

Please reply quickly 请快速回复

Base64 encode the data. Base64对数据进行编码。 Send it as a string, receive it on the Rails side and decode it back to the original format. 以字符串形式发送,在Rails端接收,然后解码回原始格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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