0

在我的按钮上,我有以下代码:

    Log.i(TAG, "!isFirstVideo: " + isFirstVideo);
ConcatenateVideos concatenateVideos = new ConcatenateVideos();
concatenateVideos.setUris(firstVideoUri, fileUri, VideoRecorderActivity.this);

这是我的异步任务类:

public class ConcatenateVideos extends AsyncTask<String, Void, String> {

private String firstVideoUri, fileUri;
private VideoRecorderActivity videoRecorderActivity;

public void setUris(String firstVideoUri, String fileUri, VideoRecorderActivity videoRecorderActivity) {
    Log.i("VideoRecorderActivity", "set uris");
    this.firstVideoUri = firstVideoUri;
    this.fileUri = fileUri;
    this.videoRecorderActivity = videoRecorderActivity;
    this.execute();
    Log.i("VideoRecorderActivity", "set uris2");
}

@Override
protected String doInBackground(String... params) {
    Log.i("VideoRecorderActivity", "concat do in background");
    FileInputStream videoMain = null, videoToAppend = null;
    try {
        videoMain = new FileInputStream(firstVideoUri);
        videoToAppend = new FileInputStream(fileUri);
    } catch (FileNotFoundException e) {
        Log.i("VideoRecorderActivity", "concat file not found " + e);
        e.printStackTrace();
    }
    if (videoMain != null && videoToAppend != null) {
        concatenateVideos(videoMain, videoToAppend);
        videoRecorderActivity.deleteCancelledRecording(fileUri);
    }
    return null;
}

@Override
protected void onPostExecute(String result) {
    super.onPostExecute(result);
    VideoRecorderActivity.recordButton.setEnabled(true);
    VideoRecorderActivity.okButton.setEnabled(true);
    VideoRecorderActivity.mySurfaceView.setEnabled(true);
    Toast.makeText(videoRecorderActivity, "Video appended succesfully", Toast.LENGTH_SHORT).show();
    Log.i("VideoRecorderActivity", "concat touche enabled record, ok ,surface: " + VideoRecorderActivity.recordButton.isEnabled() + "| " + VideoRecorderActivity.okButton.isEnabled() + "|" + VideoRecorderActivity.mySurfaceView.isEnabled());
}

在 logcat 我得到:

04-11 12:21:42.520: I/VideoRecorderActivity(7927): set uris
04-11 12:21:42.520: I/VideoRecorderActivity(7927): set uris2

但我不明白Log.i("VideoRecorderActivity", "concat do in background");这是 doInBackground 类的第一行,在这两行之间被称为:

Log.i("VideoRecorderActivity", "set uris");
this.firstVideoUri = firstVideoUri;
this.fileUri = fileUri;
this.videoRecorderActivity = videoRecorderActivity;
this.execute();
Log.i("VideoRecorderActivity", "set uris2");

任何想法我做错了什么?

4

4 回答 4

2

你会看见

"set uris2"

在后台任务开始执行之前的日志输出中。不能保证您的方法在您从主线程doInBackground()调用后立即开始执行。execute()它可能会在不久之后开始执行,但不会立即执行。

也许您只需要进一步查看 logcat 缓冲区即可找到您期望的输出?

于 2013-04-11T09:29:56.973 回答
1

您需要启动异步任务:

ConcatenateVideos concatenateVideos = new ConcatenateVideos();
concatenateVideos.setUris(firstVideoUri, fileUri, VideoRecorderActivity.this);
concatenateVideos.execute();   // let's roll

你也不应该打电话给super.onPostExecute();. 这样做没有任何意义。

于 2013-04-11T09:27:42.107 回答
0

调用

new ConcatenateVideos().execute() 

按钮单击和方法

 setUris() 

doInBackground() 方法中的方法。

于 2013-04-11T09:28:55.397 回答
0

当任何服务在该活动的后台运行时。例如,当音乐在后台运行服务时,Async 任务不会在后台解析 XML,因为它的 doInBackground 在那段时间不起作用,并且进度对话框或进度条一直在旋转。这就是为什么我使用 Executor 而不是旧的 asynctask。问题是我有一个下载管理器,它在另一个活动的背景下工作,并从数据库下载所有本地不存在的视频。这与 asynctask 类一起使用,它无法执行这个。

于 2013-04-11T12:43:20.267 回答