android学习--语音聊天二(发送与接收)

测试发送语音文件:

package com.example.sendfile;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;


import android.app.Activity;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends Activity {

private Button send,start,stop;
private boolean isSDcard;
//提示用
private TextView tip;

private EditText sendToIp;

private MediaRecorder mymediarecorder;
private File myfile;

private int sendtime = 0;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

tip = (TextView)this.findViewById(R.id.tip);

sendToIp = (EditText)this.findViewById(R.id.sendToIp);

start = (Button)findViewById(R.id.start);
start.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(!Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)){
Toast.makeText(MainActivity.this, "请插入内存卡", Toast.LENGTH_LONG).show();
return ;
}
try {
//实例化mediarecorder对象
mymediarecorder = new MediaRecorder();
//设置麦克风
mymediarecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
//设置文件输出格式
mymediarecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
//设置音频文件的编码
mymediarecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
//设置输出文件的路径
myfile = File.createTempFile("TempMediarecorder", ".amr", Environment.getExternalStorageDirectory());
mymediarecorder.setOutputFile(myfile.getAbsolutePath());
//准备
mymediarecorder.prepare();
//开始
mymediarecorder.start();
System.out.println("开始---->");
start.setText("speaking...");
start.setEnabled(false);
stop.setEnabled(true);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

});

stop = (Button)findViewById(R.id.stop);
stop.setEnabled(false);
stop.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(myfile != null){
mymediarecorder.stop();
mymediarecorder.release();
mymediarecorder = null;
System.out.println("停止---->");
start.setEnabled(true);
stop.setEnabled(false);
start.setText("start");
}
}

});

send = (Button)this.findViewById(R.id.send);
send.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
if(myfile.exists()){
Send send = new Send();
new Thread(send).start();
}else{
System.out.println("还没录音");
//Toast.makeText(MainActivity.this, "还没录音", Toast.LENGTH_SHORT);
}
}

});
}

//用于提示的handler
Handler handler = new Handler() {


@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
if(msg.what == 1) {
sendtime++;
tip.setText(sendtime + "次发送成功!!!");
}
}

};

class Send implements Runnable {


@Override
public void run() {
System.out.println("开始发送....");
String Ip = sendToIp.getText().toString();
try {
InputStream is = new FileInputStream(new File(myfile.getAbsolutePath()));
DatagramSocket socket = new DatagramSocket(50000);
byte[] buf = new byte[10240*5];
int len = is.read(buf);
DatagramPacket packet = new DatagramPacket(buf, len, new InetSocketAddress(Ip, 50001));
socket.send(packet);
is.close();
System.out.println("发送完毕");
Message message = new Message();
message.what = 1;
handler.sendMessage(message);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}


}


测试语音的接收:

package com.example.receviefile;


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;


import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;


public class MainActivity extends Activity {

private Button recevie,play;

private File myfile;

private MediaPlayer player;



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

recevie = (Button)this.findViewById(R.id.recevie);
recevie.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Recevie recevie = new Recevie();
new Thread(recevie).start();
}

});

play = (Button)this.findViewById(R.id.play);
play.setEnabled(false);
play.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
player = new MediaPlayer();
//重置播放器
player.reset();
//得到音频文件
try {
player.setDataSource(myfile.getAbsolutePath());
//准备播放
player.prepare();
//开始播放
player.start();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

});

}

class Recevie implements Runnable {


@Override
public void run() {
if(Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)){
System.out.println("开始接收");
try {
DatagramSocket socket = new DatagramSocket(50001);
byte[] buf = new byte[10240*5];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
myfile = File.createTempFile("TempMediarecorders", ".amr", Environment.getExternalStorageDirectory());
OutputStream output = new FileOutputStream(myfile);
output.write(buf);
output.flush();
output.close();
play.setEnabled(true);
System.out.println("接收完毕");
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
System.out.println("请插入SDCard");
}
}

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}


}



以上两个程序要添加的权限:

发送端:

    <!-- SDcard写入数据的权限 -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <!-- 录音权限 -->
    <uses-permission android:name="android.permission.RECORD_AUDIO"/>
    <!-- SDcard中创建和删除文件权限 -->
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
    <!-- 网络权限 -->
    <uses-permission android:name="android.permission.INTERNET" />

接收端:  

    <!-- SDcard写入数据的权限 -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <!-- SDcard中创建和删除文件权限 -->
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
    <!-- 网络权限 -->
    <uses-permission android:name="android.permission.INTERNET" />



你可能感兴趣的:(android,socket,UDP,语音聊天)