Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MainActivity extends Activity {
- private MediaRecorder mediaRecorder;
- private MySocket mySocket;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Button button= (Button) findViewById(R.id.button);
- mySocket=new MySocket(getApplicationContext());
- button.setOnTouchListener(new View.OnTouchListener() {
- @Override
- public boolean onTouch(View v, MotionEvent event) {
- switch (event.getAction()){
- case KeyEvent.ACTION_DOWN:
- MyMediaRecorder();
- break;
- case KeyEvent.ACTION_UP:
- mediaRecorder.release();
- SendAsync sendAsync=new SendAsync(getApplicationContext());
- sendAsync.execute();
- break;
- }
- return false;
- }
- });
- }
- public void MyMediaRecorder(){
- mediaRecorder=new MediaRecorder();
- mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
- mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
- mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
- mediaRecorder.setOutputFile(getFilesDir()+"/mic");
- try {
- mediaRecorder.prepare();
- mediaRecorder.start();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- public class MySocket {
- public static Socket socket;
- private Thread thread;
- private Context context;
- public MySocket(final Context context){
- this.context=context;
- thread=new Thread(new Runnable() {
- @Override
- public void run() {
- try {
- InetAddress inetAddress = InetAddress.getByName("5.199.209.79");
- socket=new Socket(inetAddress,8888);
- while (true){
- }
- } catch (java.io.IOException e) {
- e.printStackTrace();
- }
- }
- });
- thread.start();
- }
- }
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- public class SendAsync extends AsyncTask{
- private Context context;
- private Socket socket;
- public SendAsync(Context context){
- this.context=context;
- this.socket=MySocket.socket;
- }
- @Override
- protected Object doInBackground(Object[] params) {
- try {
- OutputStream outputStream=socket.getOutputStream();
- File file=new File(context.getFilesDir()+"/mic");
- FileInputStream fileInputStream=new FileInputStream(file);
- byte buf[]=new byte[(int) file.length()];
- fileInputStream.read(buf);
- fileInputStream.close();
- outputStream.write(buf);
- Log.e("Log", String.valueOf(socket.isInputShutdown()));
- Log.e("Log", String.valueOf(socket.isOutputShutdown()));
- } catch (java.io.IOException e) {
- e.printStackTrace();
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement