我正在尝试使用套接字从应用程序发送记录。
println(a)
outPutFile是mediaRecorder对象的路径,因此我尝试将其作为常规文件打开,然后将文件以字节为单位发送到服务器。
服务器中的代码是Python:
private Button play,record,stop;
private MediaRecorder myAudioRecorder;
private String outputFile;
private Thread thread;
private String ADDRESSIP = "10.0.0.8";
private int PORT = 5565;
private PrintWriter pw;
Socket s;
private String respone = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
play = (Button)findViewById(R.id.play);
stop = (Button)findViewById(R.id.stop);
record = (Button)findViewById(R.id.record);
stop.setEnabled(true);
play.setEnabled(true);
outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/recording.3gp";
myAudioRecorder = new MediaRecorder();
myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
myAudioRecorder.setOutputFile(outputFile);
record.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
myAudioRecorder.prepare();
myAudioRecorder.start();
} catch (IOException ioe) {
//make something
}
record.setEnabled(false);
stop.setEnabled(true);
Toast.makeText(getApplicationContext(), "Recording started", Toast.LENGTH_LONG).show();
}
});
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myAudioRecorder.stop();
myAudioRecorder.reset();
myAudioRecorder.release();
myAudioRecorder = null;
record.setEnabled(true);
stop.setEnabled(false);
play.setEnabled(true);
Toast.makeText(getApplicationContext(), "Audio Recorder Successfully", Toast.LENGTH_LONG).show();
new Thread(new Runnable(){
public void run(){
//open socket
try
{
s = new Socket(ADDRESSIP , PORT);
System.out.println("connected");
pw = new PrintWriter(s.getOutputStream());
//Have to send 2 for the server so he will know that he needs to open .3mp file
pw.write('2');
pw.flush();
//sending the file to the server here
File myFile = new File(outputFile);
byte [] mybytearray = new byte [(int)myFile.length()];
FileInputStream fis = new FileInputStream(myFile);
pw.write(fis.read());
pw.flush();
pw.close();
}catch (IOException e)
{
e.printStackTrace();
}
}
}).start();
}
});
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MediaPlayer mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(outputFile);
mediaPlayer.prepare();
mediaPlayer.start();
Toast.makeText(getApplicationContext(), "Playing Audio", Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
请帮助我了解如何将数据从应用程序传输到服务器
谢谢