Code example for LocalSocket

Methods: connect, getFileDescriptor

0
        }); 
		th.start();
         
		try { Thread.sleep(1000); } catch (Exception e) {}
		 
		ls = new LocalSocket();
        try { 
			ls.connect(new LocalSocketAddress(SOCKET_ADDRESS));
		} catch (IOException e) {
			//stop(); 
		} 
        camera = Camera.open();
        
 
	} 
	 
    public void start() { 
    	if (prepareVideoRecorder()) { 
 
            mMediaRecorder.start();
            captureButton.setText("Stop");
            isRecording = true;
        } else { 
            releaseMediaRecorder(); 
        }  
    } 
     
    public void stop() { 
        if (isRecording) {
        	//constructorVideo(); 
            mMediaRecorder.stop();
            releaseMediaRecorder(); 
            camera.lock();
 
            captureButton.setText("Start");
            isRecording = false;
        } 
    } 
     
    @Override 
    protected void onPause() { 
        super.onPause(); 
        releaseMediaRecorder();       // if you are using MediaRecorder, release it first 
        releaseCamera();              // release the camera immediately on pause event 
    } 
	 
	 
	public void onClick(View v) {
		if(started) {
			//destroyMe(); 
			started = false;
			finish(); 
		} 
		else { 
			onStartCommand(); 
			started = true;
		} 
		/* 
        if (isRecording) { 
            mMediaRecorder.stop(); 
            releaseMediaRecorder(); 
            camera.lock(); 
 
            captureButton.setText("Capture"); 
            isRecording = false; 
        } else { 
            if (prepareVideoRecorder()) { 
 
                mMediaRecorder.start(); 
                captureButton.setText("Stop"); 
                isRecording = true; 
            } else { 
                releaseMediaRecorder(); 
            } 
        }*/ 
		 
	} 
    
	 
    public void listen() { 
        try { 
            LocalServerSocket server = new LocalServerSocket(SOCKET_ADDRESS);
 
            LocalSocket receiver = server.accept();
            if (receiver != null) {
                InputStream input = receiver.getInputStream();
                 
                byte[] buffer = new byte[1024];
                // simply for java.util.ArrayList 
                boolean rec = true;
                while(rec) {
	                int read = input.read(buffer);
	                if(read == -1) {
	                	rec = false;
	                	Log.e(TAG,"-1 read in socket");
	                	break; 
	                } 
	                if(!isRecording)
	                	rec= false;
	                if(!headerfound) {
	                	int i = ByteArrayFinder.find(buffer, pattern);
	                	if(i != -1) {
	                		headerfound = true;
	                		handleData(tmpch, header);
	                		handleData(tmpch, pattern);
	                		try { 
	    	                tmp = new byte[read-(i+pattern.length)];
	    	                System.arraycopy(buffer, i+pattern.length, tmp, 0, read-(i+pattern.length));
	    	                handleData(tmpch, tmp);
	    	                Log.i(TAG,"Paquet: "+count);
	    	                count++;
	                		} catch(Exception e) {}
	                	} 
	                } 
	                else { 
		                tmp = new byte[read];
		                System.arraycopy(buffer, 0, tmp, 0, read);
		                handleData(tmpch, tmp);
    	                Log.i(TAG,"Paquet: "+count);
    	                count++;
	                } 
	                //out.write(buffer, 0, readed); 
                } 
                headerfound = false;
            } 
        } catch (IOException e) {
            Log.e(getClass().getName(), e.getMessage());
        } 
    } 
	 
    private boolean prepareVideoRecorder(){ 
        mMediaRecorder = new MediaRecorder();
 
        // Step 1: Unlock and set camera to MediaRecorder 
        camera.unlock();
        mMediaRecorder.setCamera(camera);
 
        // Step 2: Set sources 
        mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
        mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
 
        // Step 3: Set a CamcorderProfile (requires API Level 8 or higher) 
        //mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH)); 
  
         
        // Step 3: Set output format and encoding (for versions prior to API Level 8) 
        mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
        mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263);
         
        // Step 4: Set output file 
        mMediaRecorder.setOutputFile(ls.getFileDescriptor());
         
        // Step 5: Set the preview output 
        //mMediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface()); 
        mMediaRecorder.setPreviewDisplay(holder.getSurface());