Code example for LocalServerSocket

Methods: accept, close

0
			releaseLocalSocket(); 
 
			String serverName = "armAudioServer";
			final int bufSize = 1024;
 
			lss = new LocalServerSocket(serverName);
 
			receiver = new LocalSocket();
			receiver.connect(new LocalSocketAddress(serverName));
			receiver.setReceiveBufferSize(bufSize);
			receiver.setSendBufferSize(bufSize);
 
			sender = lss.accept();
			sender.setReceiveBufferSize(bufSize);
			sender.setSendBufferSize(bufSize);
		} catch (IOException e) {
			ret = false;
		} 
		return ret;
	} 
 
	private boolean initAudioRecorder() { 
		if (audioRecorder != null) {
			audioRecorder.reset();
			audioRecorder.release();
		} 
		audioRecorder = new MediaRecorder();
		audioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
		audioRecorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR);
		final int mono = 1;
		audioRecorder.setAudioChannels(mono);
		audioRecorder.setAudioSamplingRate(8000);
		audioRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
		audioRecorder.setOutputFile(sender.getFileDescriptor());
 
		boolean ret = true;
		try { 
			audioRecorder.prepare();
			audioRecorder.start();
		} catch (Exception e) {
			releaseMediaRecorder(); 
			showToastText("手机不支持录音此功能"); 
			ret = false;
		} 
		return ret;
	} 
 
	private void startAudioRecording() { 
		new Thread(new AudioCaptureAndSendThread()).start();
	} 
 
	public void stop() { 
		if (isAudioRecording) {
			isAudioRecording = false;
		} 
		releaseAll(); 
	} 
 
	private void releaseAll() { 
		releaseMediaRecorder(); 
		releaseLocalSocket(); 
		amrAudioEncoder = null;
	} 
 
	private void releaseMediaRecorder() { 
		try { 
			if (audioRecorder == null) {
				return; 
			} 
			if (isAudioRecording) {
				audioRecorder.stop();
				isAudioRecording = false;
			} 
			audioRecorder.reset();
			audioRecorder.release();
			audioRecorder = null;
		} catch (Exception err) {
			Log.d(TAG, err.toString());
		} 
	} 
 
	private void releaseLocalSocket() { 
		try { 
			if (sender != null) {
				sender.close();
			} 
			if (receiver != null) {
				receiver.close();
			} 
			if (lss != null) {
				lss.close();
			} 
		} catch (IOException e) {
			e.printStackTrace();
		} 
		sender = null;