samedi 18 avril 2015

the android app block when i call mediarecorder.stop

i use MediaRecorder as Google API provides(http://ift.tt/1j37gpT),it run well.but i have to put the video to the server real time. so i use LocalSocket instead of the file path. First i define the LocalSocket



LocalServerSocket lss;
LocalSocket receiver;
LocalSocket sender;


and init the LocalSocket on the function of onCreate:



private void initLocalSocket() {
receiver = new LocalSocket();
try {
lss = new LocalServerSocket("H264");
receiver.connect(new LocalSocketAddress("H264"));
receiver.setReceiveBufferSize(500000);
receiver.setSendBufferSize(500000);
sender = lss.accept();
sender.setReceiveBufferSize(500000);
sender.setSendBufferSize(500000);
} catch (IOException e1) {
e1.printStackTrace();
Log.e("", "localSocket error:" + e1.getMessage());
}
}


and replace the setOutputFile:



mMediaRecorder.setOutputFile(sender.getFileDescriptor());


then start the thread to charge of the data after setOutputFile:



new Thread(new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub
try {
@SuppressWarnings("resource")
Socket socket=new Socket(IP_ADDR,PORT);
InputStream is=receiver.getInputStream();
byte[]buffer=new byte[500000];
OutputStream os=socket.getOutputStream();
int length=0;
while(true)
{
length=is.read(buffer);
os.write(buffer);

}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();


then problem comes when i stop the recoding, it blocks, gives me nothing. I put it to the new thread and even the try, but it doesn't give me any Exception error(i try the RuntimeException, IOException), this is the code:



new Thread(new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub
try {
Log.d(TAG,"i will stop the media recorder");
mMediaRecorder.stop();
Log.d(TAG,"i will release the media recorder");
releaseMediaRecorder();
Log.d(TAG,"release media recorder successful");
mCamera.lock();
} catch (RuntimeException e) {
// TODO: handle exception
e.printStackTrace();
}
}
}).start();


i can just see the logcat "i will stop the media recorder" and then stop, it doesn't throw any exception.


Aucun commentaire:

Enregistrer un commentaire