Advertisement
Guest User

Untitled

a guest
Nov 12th, 2014
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.76 KB | None | 0 0
  1. public class MainActivity extends Activity {
  2.     private MediaRecorder mediaRecorder;
  3.     private MySocket mySocket;
  4.  
  5.     @Override
  6.     public void onCreate(Bundle savedInstanceState) {
  7.         super.onCreate(savedInstanceState);
  8.         setContentView(R.layout.main);
  9.         Button button= (Button) findViewById(R.id.button);
  10.         mySocket=new MySocket(getApplicationContext());
  11.  
  12.         button.setOnTouchListener(new View.OnTouchListener() {
  13.             @Override
  14.             public boolean onTouch(View v, MotionEvent event) {
  15.                 switch (event.getAction()){
  16.                     case KeyEvent.ACTION_DOWN:
  17.                         MyMediaRecorder();
  18.                         break;
  19.                     case KeyEvent.ACTION_UP:
  20.                         mediaRecorder.release();
  21.                         SendAsync sendAsync=new SendAsync(getApplicationContext());
  22.                         sendAsync.execute();
  23.                         break;
  24.                 }
  25.                 return false;
  26.             }
  27.         });
  28.     }
  29.  
  30.     public void MyMediaRecorder(){
  31.         mediaRecorder=new MediaRecorder();
  32.         mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
  33.         mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
  34.         mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
  35.         mediaRecorder.setOutputFile(getFilesDir()+"/mic");
  36.         try {
  37.             mediaRecorder.prepare();
  38.             mediaRecorder.start();
  39.         } catch (IOException e) {
  40.             e.printStackTrace();
  41.         }
  42.     }
  43. }
  44.  
  45. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  46.  
  47. public class MySocket {
  48.     public static Socket socket;
  49.     private Thread thread;
  50.     private Context context;
  51.  
  52.     public MySocket(final Context context){
  53.         this.context=context;
  54.  
  55.         thread=new Thread(new Runnable() {
  56.             @Override
  57.             public void run() {
  58.                 try {
  59.                     InetAddress inetAddress = InetAddress.getByName("5.199.209.79");
  60.                     socket=new Socket(inetAddress,8888);
  61.                     while (true){
  62.  
  63.                     }
  64.                 } catch (java.io.IOException e) {
  65.                     e.printStackTrace();
  66.                 }
  67.             }
  68.         });
  69.         thread.start();
  70.     }
  71. }
  72. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  73.  
  74. public class SendAsync extends AsyncTask{
  75.     private Context context;
  76.     private Socket socket;
  77.  
  78.     public SendAsync(Context context){
  79.         this.context=context;
  80.         this.socket=MySocket.socket;
  81.     }
  82.  
  83.     @Override
  84.     protected Object doInBackground(Object[] params) {
  85.         try {
  86.             OutputStream outputStream=socket.getOutputStream();
  87.             File file=new File(context.getFilesDir()+"/mic");
  88.             FileInputStream fileInputStream=new FileInputStream(file);
  89.             byte buf[]=new byte[(int) file.length()];
  90.             fileInputStream.read(buf);
  91.             fileInputStream.close();
  92.             outputStream.write(buf);
  93.             Log.e("Log", String.valueOf(socket.isInputShutdown()));
  94.             Log.e("Log", String.valueOf(socket.isOutputShutdown()));
  95.  
  96.         } catch (java.io.IOException e) {
  97.             e.printStackTrace();
  98.         }
  99.         return null;
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement