简体   繁体   English

活动中的android Asynctask调用方法

[英]android Asynctask call method in Activity

I am downloading a video from a ftp-server via AsyncTask and save it on the device. 我正在通过AsyncTask从ftp服务器下载视频并将其保存在设备上。 After the download (onPostExecute) i want to play the video directly in the videoview in my activity, but the problem is can't call my playVideo method since it is not static and instead I have tried to call it in the onResume() method, with the get() method, but the progressbar in my ASynctask doesn't show up then and the app later crashes. 下载(onPostExecute)后,我想在活动中直接在videoview中播放视频,但是问题是无法调用我的playVideo方法,因为它不是静态的,而是尝试在onResume()方法中调用它,使用get()方法,但那时ASynctask中的进度条没有显示,随后应用崩溃。 Any suggestion? 有什么建议吗?

Thank you in Advance 先感谢您

public class PlayStreamedVideo extends Activity { 公共类PlayStreamedVideo扩展Activity {

final String TAG = "PlayStreamedVideo";
private VideoView videoView;
private MediaController mediaController;
private DownloadVideo download;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.guessvideo);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    download = new DownloadVideo(PlayStreamedVideo.this, SAVE_PATH);
    download.execute();

}
    @Override
    protected void onResume(){
        try {
            while(download.get() == false){
                Log.d(TAG, "1");
            }
            playVideo();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ExecutionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        super.onResume();   
    }

public void playVideo() {
    try {
        videoView = (VideoView) findViewById(R.id.streamedVideoSurface);
        mediaController = new MediaController(this);
        videoView.setVideoURI(Uri.parse(SAVE_PATH));
        videoView.setMediaController(mediaController);
        videoView.start();
    } catch (Exception e) {
        // Log.e(TAG, "error: " + e.getMessage(), e);
    }
}

In my opinion you should't call that function in the onResume method. 我认为您不应在onResume方法中调用该函数。 You could pass to the asynctask a pointer to your activity and then call your play function on post-execute: 您可以将指向您活动的指针传递给asynctask,然后在执行后调用play函数:

download = new DownloadVideo(PlayStreamedVideoActivity, SAVE_PATH);

What are you passing as first parameter is the context and not the instance rappresenting your activity. 作为第一个参数传递的是上下文,而不是代表您活动的实例。

Then call the function into the onPostExecute like this: 然后像这样将函数调用到onPostExecute中:

PlayStreamedVideoActivity.playVideo();

Remember to modify your asynctask constructor acoordingly. 记住要相应地修改asynctask构造函数。

I think it is easier to declare videoView as a member of PlayStreamedVideo class. 我认为将videoView声明为PlayStreamedVideo类的成员会更容易。 Put the declarations to onCreate(): 将声明放入onCreate():

    videoView = (VideoView) findViewById(R.id.streamedVideoSurface);
    mediaController = new MediaController(this);
    videoView.setMediaController(mediaController);

Then just call from onPostExecute(): 然后只需从onPostExecute()调用:

    videoView.setVideoURI(Uri.parse(SAVE_PATH));
    videoView.start();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM