自定义录像,在重复的关闭开启录像后会调用onPause

juen32 2014-05-08 04:36:28
自定义录像,录制几秒后,关闭,继续重新开始录制。然后这样重复几次后会出现系统自动调用onPause周期。


代码如下
public class SyncVideo_backup extends BaseActivity{

private final String TAG = "SyncVideo_backup";
private VideoCaptureModule videoCaptureModule = null;
private String filePath;
private Timer mreportedtimer;
private final static int FS_TimeTick_ID = 1;
private final static int FS_ReportedEnd_ID = 2;
private final static int UPLOAD_RES = 3;
private final static int SYNC_REQUEST = 4;
private String videoPath,eventId;
private MyStack stack;
private RequestThread rThread;

private @ViewInject(id=R.id.sync_init_text) TextView syncInitText;
private @ViewInject(id=R.id.imgrecordlight) ImageView imgrecordlight;


private Handler mhandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case FS_TimeTick_ID:
if(mreportedtimer!=null)mreportedtimer.cancel();
stopRecord();//结束录像
String reportPath=(String) stack.pop();
if(reportPath!=null)
startReport(reportPath);//开始上报

startRecord();//再次开始上传

break;
case FS_ReportedEnd_ID:
break;
case UPLOAD_RES:
log(msg.obj.toString());
if(msg.arg1 == 2){//平台退出
SyncVideo_backup.this.finish();
}else if(msg.arg1 == 0){
UIHelper.showShortToast(SyncVideo_backup.this, msg.obj.toString());
SyncVideo_backup.this.finish();
}

break;
case SYNC_REQUEST:
if(msg.arg1==0){//请求失败
log(msg.obj.toString());
UIHelper.showShortToast(SyncVideo_backup.this, msg.obj.toString());
SyncVideo_backup.this.finish();
}else if(msg.arg1==1){//请求成功
log("请求实时视频成功");

syncInitText.setVisibility(View.GONE);
//开始录制视频
startRecord();
}
break;
}
super.handleMessage(msg);
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
//设置窗体始终点亮
getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

setContentView(R.layout.activity_synchronized_video);

init();
//发起连接请求
rThread = new RequestThread();
rThread.start();
}

private void init(){

// 如果sd卡存在,取得SD Card路径作为录音的文件位置
if (!FileUtil.HasSDCard()){
UIHelper.showShortToast(this, "存储卡不存在");
finish();
}
else{
File file = new File(FileUtil.GetSDCardPath()+AppContext.FIELD_PATH +"sync");
if(!file.exists()) file.mkdirs();
filePath = FileUtil.GetSDCardPath() + AppContext.FIELD_PATH + "sync/";

videoCaptureModule = new VideoCaptureModule(this);
FrameLayout preview = (FrameLayout)findViewById(R.id.video_preview);
preview.addView(videoCaptureModule);
// setCameraParams((SurfaceView) findViewById(R.id.surface_camera), -1, -1, -1);



}

}

@Override
protected void onPause() {
stopRecord();//保存路径
if (mreportedtimer != null) {
mreportedtimer.cancel();
mreportedtimer = null;
}
// startReport(videoPath);
super.onPause();
new ExitThread().start();
}

@Override
protected void onDestroy() {
System.out.println("onDestroy");
super.onDestroy();
if(rThread!=null) rThread.finish();
}

public void alphaAnimation() {
AlphaAnimation alphaAnimation1 = new AlphaAnimation(0.1f, 1.0f);
alphaAnimation1.setDuration(500);
alphaAnimation1.setRepeatCount(Animation.INFINITE);
alphaAnimation1.setRepeatMode(Animation.REVERSE);
imgrecordlight.startAnimation(alphaAnimation1);
}

public void clearAnimation(){
imgrecordlight.clearAnimation();
}

private void startRecord() {
log("开始录制视频");


videoPath = filePath+System.currentTimeMillis()+".3gp";
/**
* 使用此控件VideoCaptureModule录制视频在 H200手机没问题
* 但在华为和蘑菇手机会报异常
*/
if(!videoCaptureModule.startRecording(videoPath)){
videoCaptureModule.stopRecordingAndRelease();
SyncVideo_backup.this.finish();
}else {
// 设置定时处理
mreportedtimer = new Timer();
mreportedtimer.schedule(new TickTimerTask(), 16 * 1000);
alphaAnimation();//开启动画
}


// 初始化视频camera
// if (prepareVideoRecorder(videoPath)) {
// // 现在可以开始录像
// start();
// // 设置定时处理
// mreportedtimer = new Timer();
// mreportedtimer.schedule(new TickTimerTask(), 16 * 1000);
// alphaAnimation();//开启动画
// } else {
// releaseMediaRecorder();// 准备未能完成,释放camera
// this.finish();
// }


}

private void stopRecord() {
log("停止录制视频");
videoCaptureModule.stopRecordingAndRelease();
if(stack==null)
stack=new MyStack();
stack.push(videoPath);
// stop(); // 停止录像
// releaseMediaRecorder(); // 释放MediaRecorder对象
}

public class TickTimerTask extends TimerTask {
@Override
public void run() {
Message message = new Message();
message.what = FS_TimeTick_ID;
mhandler.sendMessage(message);
}
}




public void startReport(final String path) {
log("开始上传视频:"+path);
try {
Thread thread = new Thread() {
public void run() {
if(!Utils.isEmpty(path)){
File file =new File(path);
if(file.exists()){
double size = FileUtil.FileSize(file.length());
if(size>10) {
String url = Utils.getSpPartitionIP(SyncVideo_backup.this) + "video/uploadvideo?tel="+
Utils.getSpPhone(SyncVideo_backup.this) + "&eventId="+ eventId;
videoUpLoad(path,SyncVideo_backup.this,url);
FileUtil.delFileByRoot(path);
log("删除视频:"+path);
}
}
}
}
};
thread.start();
} catch (Exception e) {
if(!(e instanceof AppException)) AppException.error(e);
}
}

/**
* 视频实时上传
*/
public void videoUpLoad(String path, Context ctx, String url) {
System.out.println("url:"+url);
int count = 0;
File file = new File(path);
byte[] dataByte = null;
if (HttpUtil.isNetworkAvailable(ctx) && file.exists()) {
while (count < 3) {
HttpURLConnection hc = null;
DataOutputStream dos = null;
InputStream is = null;
ByteArrayOutputStream bos = null;
FileInputStream fis = null;
try {
if (HttpUtil.getNetType(ctx) == 1) {// 为wifi
hc = (HttpURLConnection) new URL(url).openConnection();
} else {
String proxyHost = android.net.Proxy.getDefaultHost();// 代理的地址
if (proxyHost != null) {
java.net.Proxy p = new java.net.Proxy(
java.net.Proxy.Type.HTTP,
new InetSocketAddress(proxyHost,
android.net.Proxy.getDefaultPort()));
hc = (HttpURLConnection) new URL(url)
.openConnection(p);
} else {
hc = (HttpURLConnection) new URL(url)
.openConnection();
}
}
hc.setConnectTimeout(10000);
hc.setReadTimeout(30000);
hc.setRequestMethod("POST");
hc.setDoInput(true);
hc.setDoOutput(true);
dos = new DataOutputStream( new BufferedOutputStream(hc.getOutputStream()));
fis = new FileInputStream(file);
byte[] bs = new byte[8192];
int l = 0;
while ((l = fis.read(bs)) != -1) {
dos.write(bs, 0, l);
}

dos.flush();
int resCode = hc.getResponseCode();
log(resCode +"");
if (resCode == HttpURLConnection.HTTP_OK) {
is = hc.getInputStream();
bos = new ByteArrayOutputStream();
int ch = 0;
byte[] bss = new byte[8192];
while ((ch = is.read(bss)) != -1) {
bos.write(bss, 0, ch);
}
dataByte = bos.toByteArray();
String result = new String(dataByte,"UTF-8");
System.out.println("==============="+result);
bss = null;

JSONObject jObj = new JSONObject(result);
int code = Integer.parseInt(jObj.getString("code"));
Message message = new Message();
message.what = UPLOAD_RES;
message.arg1 = code;
message.obj = jObj.getString("message");
mhandler.sendMessage(message);
if(code == 2 || code == 1 || code == 0){
count = 3;
}
} else {
System.out.println("无法连接服务");
}
} catch (SocketException e) {
e.printStackTrace();
System.out.println("超时");
} catch (Exception e) {
System.out.println("与服务器交互时出现异常!");
e.printStackTrace();
} finally {
try {
if(fis!=null){
fis.close();
fis = null;
}
if (dos != null) {
dos.close();
dos = null;
}
if (is != null) {
is.close();
is = null;
}
if (bos != null) {
bos.close();
bos = null;
}
if (hc != null) {
hc.disconnect();
}
} catch (IOException e) {
e.printStackTrace();
}
}
count++;
}
} else {
System.out.println("您的手机不存在网络连接,请进行网络连接的设置!");
}
}

private class RequestThread extends Thread{

private boolean isFinish = false;
public void finish(){
this.isFinish = true;
this.interrupt();
}

public void run(){

log("发起实时视频请求");
// String url = "http://192.168.1.42/video/canuploadvideo?tel=13313830712&eventId=bffecea7ff8944d0894b191d2fee76fd";
eventId = Utils.createEventId();//事件ID
try {
Map<String,Object> result = AppContext.syncRequestCon(Utils.getSpPartitionIP(SyncVideo_backup.this),
Utils.getSpPhone(SyncVideo_backup.this), eventId);
if(isFinish) return;
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
String code = String.valueOf(result.get("code"));
if ("1".equals(code)) {
Message message = new Message();
message.what = SYNC_REQUEST;
message.arg1 = 1;
mhandler.sendMessage(message);
}else{
Message message = new Message();
message.what = SYNC_REQUEST;
message.arg1 = 0;
message.obj = String.valueOf(result.get("message"));
mhandler.sendMessage(message);
}
} catch (AppException e) {
e.printStackTrace();
System.out.println("请求连接失败");
Message message = new Message();
message.what = SYNC_REQUEST;
message.arg1 = 0;
message.obj ="请求连接失败";
mhandler.sendMessage(message);
}

}
}
...全文
116 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
cyy277890157 2014-05-19
  • 打赏
  • 举报
回复
同求,,, mark。。。。。。。
Crazy冫火 2014-05-08
  • 打赏
  • 举报
回复
求分.还有求说明-_-MARK
juen32 2014-05-08
  • 打赏
  • 举报
回复
结下,来点回复 ,有回复有分
juen32 2014-05-08
  • 打赏
  • 举报
回复
已解决,多么愚蠢的问题,加班了好几天,就看了下图就解决了 。我去 感慨下 修改别人代码 真是苦力活啊!

80,349

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧