问题描述
我想提出一个录像机,我想有多少时间做了用户的持续时间有记录的视频。时间会向上计数。
为了计算时间,即时通讯使用线程和计算开始,当用户点击开始按钮,它会停止,当用户点击停止按钮。
定时器线程IM指从这里教程。然而,在这里我的问题是不工作的定时器,我想线程没有做的工作。下面是我的logcat。
下面是我的code为主要活动
公共类Videotest1Activity扩展活动器具
SurfaceHolder.Callback,OnInfoListener,OnErrorListener {
摄像头摄像头;
VideoView videoView;
SurfaceHolder持有人;
TextView的味精;
按钮initBtn,startBtn,stopBtn,playBtn,ST prevBtn;
MediaRecorder记录;
字符串outputFileName;
static final的字符串变量=RecordVideo;
INT maxDuration = 7000; // 7秒
INT帧率= 1; // 15
字符串服务器IP =172.19.117.12;
INT serverPort = 2000;
Socket套接字;
INT mCount;
TimerThread mTimer;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_videotest1);
videoView =(VideoView)findViewById(R.id.videoView1);
initBtn =(按钮)findViewById(R.id.initialize);
startBtn =(按钮)findViewById(R.id.button1);
stopBtn =(按钮)findViewById(R.id.button3);
味精=(TextView中)findViewById(R.id.textView1);
playBtn =(按钮)findViewById(R.id.reviewBtn);
ST prevBtn =(按钮)findViewById(R.id.st prevBtn);
mTimer =新TimerThread();
mTimer.setOnAlarmListener(mSTimer_OnAlarm);
mTimer.setPeriod(100);
}
公共无效buttonTapped(查看视图){
开关(view.getId()){
案例R.id.initialize:
initRecorder();
打破;
案例R.id.button1:
beginRecording();
打破;
案例R.id.button3:
STO precording();
打破;
案例R.id.reviewBtn:
playRecording();
打破;
案例R.id.st prevBtn:
stopPlayback();
打破;
}
}
@覆盖
公共无效onerror的(MediaRecorder先生,诠释了什么,整型附加){
Log.e(TAG,记录错误);
STO precording();
Toast.makeText(这一点,录音已达极限,2500).show();
}
@覆盖
公共无效onInfo(MediaRecorder先生,诠释了什么,整型附加){
Log.i(TAG,录音事件);
如果(什么== MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED){
Log.i(TAG,......达到最大值时长);
STO precording();
Toast.makeText(这一点,录音限制信息,2500).show();
}
}
@覆盖
公共无效surfaceChanged(SurfaceHolder为arg0,诠释ARG1,诠释ARG2,诠释ARG3){
}
@覆盖
公共无效surfaceCreated(SurfaceHolder为arg0){
Log.v(TAG,在surfaceCreated);
尝试{
camera.set previewDisplay(保持器);
camera.start preVIEW();
}赶上(IOException异常E){
Log.v(TAG,无法启动preVIEW);
e.printStackTrace();
}
initBtn.setEnabled(真正的);
startBtn.setEnabled(真正的);
stopBtn.setEnabled(真正的);
}
@覆盖
公共无效surfaceDestroyed(SurfaceHolder为arg0){
}
保护无效onResume(){
Log.v(TAG,在onResume);
super.onResume();
initBtn.setEnabled(假);
startBtn.setEnabled(假);
stopBtn.setEnabled(假);
playBtn.setEnabled(假);
ST prevBtn.setEnabled(假);
如果(!initCamera())
完();
}
公共布尔initCamera(){
尝试{
相机= Camera.open();
Camera.Parameters camParam = camera.getParameters();
camera.lock();
支架= videoView.getHolder();
holder.addCallback(本);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
//线程的线程=新主题(新hantarThread());
//thread.start();
}赶上(RuntimeException的重){
Log.v(TAG,无法初始化相机);
re.printStackTrace();
返回false;
}
返回true;
}
公共无效initRecorder(){
如果(录像机== NULL){
Toast.makeText(这一点,记录空,2500).show();
}
其他{
Toast.makeText(此,记录有,2500).show();
}
outputFileName = Environment.getExternalStorageDirectory()+/videooutput.mp4;
文件OUTPUTFILE =新的文件(outputFileName);
如果(outputFile.exists())
outputFile.delete(); // KNP nk的DLT?
尝试{
camera.stop preVIEW();
camera.unlock();
记录=新MediaRecorder();
recorder.setCamera(照相机);
recorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setVideoSize(176,144);
recorder.setVideoFrameRate(帧率); // 15
recorder.setVideoEn codeR(MediaRecorder.VideoEn coder.MPEG_4_SP);
recorder.setAudioEn codeR(MediaRecorder.AudioEn coder.AMR_NB);
recorder.setMaxDuration(maxDuration);
recorder.set previewDisplay(holder.getSurface());
recorder.setOutputFile(outputFileName);
。录音机prepare();
Log.v(TAG,MediaRecorder初始化);
initBtn.setEnabled(假);
startBtn.setEnabled(真正的);
}赶上(例外五){
Log.v(TAG,MediaRecorder失败);
e.printStackTrace();
}
}
公共无效beginRecording(){
mCount = 0;
mTimer.start();
recorder.setOnInfoListener(本);
recorder.setOnErrorListener(本);
recorder.start();
msg.setText(记录);
startBtn.setEnabled(假);
stopBtn.setEnabled(真正的);
}
公共无效STO precording(){
如果(录像机!= NULL){
recorder.setOnErrorListener(空);
recorder.setOnInfoListener(空);
尝试{
recorder.stop();
mTimer.stop();
}赶上(IllegalStateException异常E){
Log.e(TAG,得到了非法);
}
releaseRecorder();
msg.setText();
releaseCamera();
startBtn.setEnabled(假);
stopBtn.setEnabled(假);
playBtn.setEnabled(真正的);
}
}
私人无效releaseCamera(){
如果(相机!= NULL){
尝试{
camera.reconnect();
}赶上(IOException异常E){
e.printStackTrace();
}
camera.release();
摄像头= NULL;
}
}
私人无效releaseRecorder(){
如果(录像机!= NULL){
recorder.release();
记录= NULL;
}
}
私人无效playRecording(){
的MediaController MC =新的MediaController(本);
videoView.setMediaController(MC);
videoView.setVideoPath(outputFileName);
videoView.start();
ST prevBtn.setEnabled(真正的);
}
私人无效stopPlayback(){
videoView.stopPlayback();
}
// ----时间线---
OnAlarmListener mSTimer_OnAlarm =新OnAlarmListener(){
@覆盖
公共无效及其活动警报(TimerThread源){
mCount ++;
msg.setText(计数=+ mCount);
如果(mCount == 100)source.stop();
}
};
// ----结束时间线---
这是我的$ C $下TimerThread.java
包com.example.videotest1;
进口android.os.Handler;
/ **
*实现一个简单的定时器类。
*计时器不会创建一个单独的线程,所以在您的活动没有多线程问题。
*定时器使用到期时一个事件侦听器回调(报警)。
* @author马腾彭宁斯2011年9月26
* /
公共类TimerThread {
//对于定时器的周期(毫秒)的最小值。
公共静态最终诠释MinPeriod = 10;
//当定时器过期时(警报),以调用回调接口定义。
公共接口OnAlarmListener {
/ **
*回调的计时器源已过期(报警)。
*参数源定时器过期(当多个计时器使用同一个侦听)
* /
无效及其活动警报(TimerThread源);
}
保护INT mPeriod = 100; //什么是定时器的周期(毫秒)
保护布尔mEnabled = FALSE; //在计时器启动?
保护OnAlarmListener mOnAlarmListener = NULL; //监听器(回调)的报警事件
保护处理程序mHandler =新的处理程序(); //处理程序线程的队列中,让我们派(定时)消息到队列
受保护的可运行mMessage =新的Runnable(){//该消息被公布在消息队列
@覆盖公共无效的run()
{
//调用监听器(当它被设置)。
如果(mOnAlarmListener!= NULL)mOnAlarmListener.OnAlarm(TimerThread.this);
//级联,即后一个新的延迟的邮件自己。
//注:听众可以调用停止(),这消除了mMessage消息形成队列。
//然而,不存在这样的信息,它刚
被取出的队列和当前正在执行。
//所以检查定时器级联(发布新延迟的邮件)之前,仍处于启用状态。
如果(mEnabled)mHandler.postDelayed(mMessage,mPeriod);
}
};
/ **
*设置定时器的周期。计时器必须其开始生成告警事件之前,单独启用。
* @参数毫秒(毫秒)定时器的周期
* /
公共无效setPeriod(INT MS){
如果(MS< MinPeriod)抛出新抛出:IllegalArgumentException(STimer.setPeriod称为过小周期(+ MS +<+ MinPeriod +));
mPeriod =毫秒;
}
/ **
*返回计时器的当前时间。
返回:以毫秒为单位的定时器的当期
* /
公众诠释getPeriod(){
返回mPeriod;
}
/ **
*启用或禁用定时器产生报警事件的每一个getPeriod毫秒。
*参数启用定时器的新状态
* /
公共无效的setEnabled(布尔启用){
如果(启用!= mEnabled){
//启用的状态真正改变
如果(启用){
//后的第一条消息(这将发布的下一条消息等)
mHandler.postDelayed(mMessage,mPeriod);
} 其他 {
//删除所有消息,在队列
mHandler.removeCallbacks(mMessage);
}
//记录新的状态
mEnabled =启用;
}
}
/ **
*返回启用定时器/禁用状态的电流。
返回:当前启用定时器/禁用状态
* /
公共布尔getEnabled(){
返回mEnabled;
}
/ **
*注册回调每个定时器时间周期到期时间(警报)被调用。
*参数升将调用回侦听器对象。
* /
公共无效setOnAlarmListener(OnAlarmListener L){
mOnAlarmListener =升;
}
//启动定时器,即简写的setEnabled(真)。
公共无效启动(){
的setEnabled(真正的);
}
//停止计时器,也就是一个简写的setEnabled(假)。
公共无效停止(){
的setEnabled(假);
}
}
和最后一个是我OnAlarmListener.java
公共类OnAlarmListener实现TimerThread.OnAlarmListener {
公共无效及其活动警报(TimerThread源){
// TODO自动生成方法存根
}
考虑使用CountDownTimer()...
新CountDownTimer(1000,1000){
公共无效onTick(长millisUntilFinished){
mTextField.setText(秒时:+ millisUntilFinished / 1000);
}
公共无效onFinish(){
mTextField.setText(完成了!);
}
}。开始();
http://developer.android.com/reference/android/os/ CountDownTimer.html
I am making a video recorder and i want to have the time duration of how much time did the user have recorder the video. The time will counts upwards.
in order to calculate the time, im using thread and the calculation start when the user click the start button and it will stop when the user click the stop button.
for the timer thread im referring the tutorial from here. however my problem here is the timer is not working and i think the thread is not doing its job. below is my logcat.
Here's my code for the main activity
public class Videotest1Activity extends Activity implements
SurfaceHolder.Callback, OnInfoListener, OnErrorListener{
Camera camera;
VideoView videoView;
SurfaceHolder holder;
TextView msg;
Button initBtn, startBtn, stopBtn, playBtn, stprevBtn;
MediaRecorder recorder;
String outputFileName;
static final String TAG = "RecordVideo";
int maxDuration = 7000;//7sec
int frameRate = 1;//15
String serverIP = "172.19.117.12";
int serverPort = 2000;
Socket socket;
int mCount;
TimerThread mTimer;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_videotest1);
videoView = (VideoView) findViewById(R.id.videoView1);
initBtn = (Button) findViewById(R.id.initialize);
startBtn = (Button) findViewById(R.id.button1);
stopBtn = (Button) findViewById(R.id.button3);
msg = (TextView) findViewById(R.id.textView1);
playBtn = (Button) findViewById(R.id.reviewBtn);
stprevBtn = (Button) findViewById(R.id.stprevBtn);
mTimer= new TimerThread();
mTimer.setOnAlarmListener(mSTimer_OnAlarm);
mTimer.setPeriod(100);
}
public void buttonTapped(View view){
switch(view.getId()){
case R.id.initialize:
initRecorder();
break;
case R.id.button1:
beginRecording();
break;
case R.id.button3:
stopRecording();
break;
case R.id.reviewBtn:
playRecording();
break;
case R.id.stprevBtn:
stopPlayback();
break;
}
}
@Override
public void onError(MediaRecorder mr, int what, int extra) {
Log.e(TAG, "Record error");
stopRecording();
Toast.makeText(this, "Recording limit reached", 2500).show();
}
@Override
public void onInfo(MediaRecorder mr, int what, int extra) {
Log.i(TAG, "recording event");
if(what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED){
Log.i(TAG, "...max duration reached");
stopRecording();
Toast.makeText(this, "Recording limit info", 2500).show();
}
}
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
}
@Override
public void surfaceCreated(SurfaceHolder arg0) {
Log.v(TAG, "in surfaceCreated");
try{
camera.setPreviewDisplay(holder);
camera.startPreview();
}catch(IOException e){
Log.v(TAG, "Could not start the preview");
e.printStackTrace();
}
initBtn.setEnabled(true);
startBtn.setEnabled(true);
stopBtn.setEnabled(true);
}
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
}
protected void onResume(){
Log.v(TAG, "in onResume");
super.onResume();
initBtn.setEnabled(false);
startBtn.setEnabled(false);
stopBtn.setEnabled(false);
playBtn.setEnabled(false);
stprevBtn.setEnabled(false);
if(!initCamera())
finish();
}
public boolean initCamera(){
try{
camera = Camera.open();
Camera.Parameters camParam = camera.getParameters();
camera.lock();
holder = videoView.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
//Thread thread = new Thread(new hantarThread());
//thread.start();
}catch(RuntimeException re){
Log.v(TAG, "Could not initialize the camera");
re.printStackTrace();
return false;
}
return true;
}
public void initRecorder(){
if(recorder == null){
Toast.makeText(this, "record null", 2500).show();
}
else{
Toast.makeText(this, "record have", 2500).show();
}
outputFileName = Environment.getExternalStorageDirectory() + "/videooutput.mp4";
File outputFile = new File(outputFileName);
if(outputFile.exists())
outputFile.delete();//knp nk dlt?
try{
camera.stopPreview();
camera.unlock();
recorder = new MediaRecorder();
recorder.setCamera(camera);
recorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setVideoSize(176, 144);
recorder.setVideoFrameRate(frameRate);//15
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setMaxDuration(maxDuration);
recorder.setPreviewDisplay(holder.getSurface());
recorder.setOutputFile(outputFileName);
recorder.prepare();
Log.v(TAG, "MediaRecorder initialized");
initBtn.setEnabled(false);
startBtn.setEnabled(true);
}catch(Exception e){
Log.v(TAG, "MediaRecorder failed");
e.printStackTrace();
}
}
public void beginRecording(){
mCount = 0;
mTimer.start();
recorder.setOnInfoListener(this);
recorder.setOnErrorListener(this);
recorder.start();
msg.setText("Recording");
startBtn.setEnabled(false);
stopBtn.setEnabled(true);
}
public void stopRecording(){
if(recorder != null){
recorder.setOnErrorListener(null);
recorder.setOnInfoListener(null);
try{
recorder.stop();
mTimer.stop();
}catch(IllegalStateException e){
Log.e(TAG, "Got Illegal");
}
releaseRecorder();
msg.setText("");
releaseCamera();
startBtn.setEnabled(false);
stopBtn.setEnabled(false);
playBtn.setEnabled(true);
}
}
private void releaseCamera(){
if(camera != null){
try{
camera.reconnect();
}catch(IOException e){
e.printStackTrace();
}
camera.release();
camera = null;
}
}
private void releaseRecorder(){
if(recorder != null){
recorder.release();
recorder = null;
}
}
private void playRecording(){
MediaController mc = new MediaController(this);
videoView.setMediaController(mc);
videoView.setVideoPath(outputFileName);
videoView.start();
stprevBtn.setEnabled(true);
}
private void stopPlayback(){
videoView.stopPlayback();
}
//----time thread---
OnAlarmListener mSTimer_OnAlarm= new OnAlarmListener() {
@Override
public void OnAlarm(TimerThread source) {
mCount++;
msg.setText("Count="+mCount);
if( mCount==100) source.stop();
}
};
//----end time thread---
here is my code for TimerThread.java
package com.example.videotest1;
import android.os.Handler;
/**
* A class that implements a simple timer.
* The timer does not create a separate thread, so there are no multi-threading issues in your activity.
* The timer uses an event listener as callback when it expires ("alarm") .
* @author Maarten Pennings 2011 September 26
*/
public class TimerThread {
// The minimal value for the period of a timer (in milliseconds).
public static final int MinPeriod = 10;
// Interface definition for a callback to be invoked when a timer expires ("alarm").
public interface OnAlarmListener {
/**
* Callback that timer 'source' has expired ("alarm").
* @param source Timer that expired (when multiple timers use same listener)
*/
void OnAlarm( TimerThread source );
}
protected int mPeriod= 100; // what is the period of the timer (in milliseconds)
protected boolean mEnabled= false; // is the timer enabled?
protected OnAlarmListener mOnAlarmListener= null; // the listener (callback) for the alarm events
protected Handler mHandler= new Handler(); // handler to the thread's queue, allows us to send (timed) messages to that queue
protected Runnable mMessage= new Runnable() { // The message being posted on the message queue
@Override public void run()
{
// Call the listener (when it is set).
if( mOnAlarmListener!=null ) mOnAlarmListener.OnAlarm(TimerThread.this);
// Cascade, i.e. post a new delayed message to ourselves.
// NOTE: The listener could have called stop(), this removes the mMessage message form the queue.
// However, there is no such message, it has just
been taken out of the queue and is currently being executed.
// So check if the timer is still enabled before cascading (posting a new delayed message).
if( mEnabled ) mHandler.postDelayed(mMessage, mPeriod);
}
};
/**
* Set the period of the timer. The timer must be separately enabled before its starts generating alarm events.
* @param ms the period of the timer in milliseconds
*/
public void setPeriod( int ms ) {
if( ms<MinPeriod ) throw new IllegalArgumentException("STimer.setPeriod called with too small period ("+ms+"<"+MinPeriod+")" );
mPeriod= ms;
}
/**
* Returns the current period of the timer.
* @return the current period of the timer in milliseconds
*/
public int getPeriod( ) {
return mPeriod;
}
/**
* Enables or disables the timer for generating an alarm event every getPeriod milliseconds.
* @param enabled the new state of the timer
*/
public void setEnabled( boolean enabled ) {
if( enabled!=mEnabled ) {
// The enabled state really changes
if( enabled ) {
// Post the first message (which will post the next message and so on)
mHandler.postDelayed(mMessage, mPeriod);
} else {
// Remove any message that is in the queue
mHandler.removeCallbacks(mMessage);
}
// Record the new state
mEnabled= enabled;
}
}
/**
* Returns the current enabled/disabled state of the timer.
* @return the current enabled/disabled state of the timer
*/
public boolean getEnabled() {
return mEnabled;
}
/**
* Register a callback to be invoked each time the timer period expires ("alarm").
* @param l the listener object that will be called-back.
*/
public void setOnAlarmListener( OnAlarmListener l ) {
mOnAlarmListener= l;
}
// Starts the timer, i.e. a shorthand for setEnabled(true).
public void start( ) {
setEnabled(true);
}
// Stops the timer, i.e. a shorthand for setEnabled(false).
public void stop( ) {
setEnabled(false);
}
}
and last is my OnAlarmListener.java
public class OnAlarmListener implements TimerThread.OnAlarmListener {
public void OnAlarm(TimerThread source) {
// TODO Auto-generated method stub
}
Consider using a CountDownTimer() ...
new CountDownTimer(1000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();
http://developer.android.com/reference/android/os/CountDownTimer.html
这篇关于显示时间,而视频录制的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!