繁体   English   中英

为什么在尝试将setOnCompletionListener与videoview一起使用时出现错误?

[英]Why I am I getting errors when I try to use setOnCompletionListener with videoview?

视频播放完毕后,我正在尝试将视频继续进行下一个活动。 我看到其他文章也尝试这样做,并且在尝试实现它们时,我在其余代码中不断遇到错误。 在尝试添加最后一部分之前,我的视频播放正常。 有人看到我的代码有问题吗? 先感谢您。

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.VideoView;


public class ThirdActivity extends Activity {

    ProgressBar mProgressBar;
    VideoView mVideoView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_third);

        String fileName = "android.resource://" + getPackageName() + "/" + R.raw.leftwrist;

        mProgressBar = (ProgressBar) findViewById(R.id.Progressbar);
        mProgressBar.setProgress(0);
        mProgressBar.setMax(100);

        mVideoView = (VideoView) findViewById(R.id.video_view);
        mVideoView.setVideoURI(Uri.parse(fileName));
        new myAsync().execute();
    }

    private class myAsync extends AsyncTask<Void, Integer, Void>
    {
        int duration = 0;
        int current = 0;
        @Override
        protected Void doInBackground(Void... params) {

            mVideoView.start();
            mVideoView.setOnPreparedListener(new OnPreparedListener() {

                public void onPrepared(MediaPlayer mp) {
                    duration = mVideoView.getDuration();
                }
            });

            do {
                current = mVideoView.getCurrentPosition();
                System.out.println("duration - " + duration + " current- "
                        + current);
                try {
                    publishProgress((int) (current * 100 / duration));
                    if(mProgressBar.getProgress() >= 100){
                        break;
                    }
                } catch (Exception e) {
                }
            } while (mProgressBar.getProgress() <= 100);

            return null;
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
            System.out.println(values[0]);
            mProgressBar.setProgress(values[0]);
        }

        mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener(){
            @Override
            public void onCompletion(MediaPlayer mp){
                Intent intent = new Intent(this, FourthActivity.class);
                startActivity(intent);
            }
        });

}

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.third, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_third, container, false);
            return rootView;
        }
    }

}

更改

Intent intent = new Intent(this, FourthActivity.class);

Intent intent = new Intent(ThirdActivity.this, FourthActivity.class);

在您的情况下, this指的是OnCompletionListener而不是活动。

将以下内容从AsyncTask移至onCreate

  mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener(){
            @Override
            public void onCompletion(MediaPlayer mp){
                Intent intent = new Intent(this, FourthActivity.class);
                startActivity(intent);
            }
        });

例如:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_third);

    String fileName = "android.resource://" + getPackageName() + "/" + R.raw.leftwrist;

    mProgressBar = (ProgressBar) findViewById(R.id.Progressbar);
    mProgressBar.setProgress(0);
    mProgressBar.setMax(100);

    mVideoView = (VideoView) findViewById(R.id.video_view);
   mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener(){
            @Override
            public void onCompletion(MediaPlayer mp){
                Intent intent = new Intent(this, FourthActivity.class);
                startActivity(intent);
            }
        });
    mVideoView.setVideoURI(Uri.parse(fileName));
    new myAsync().execute();
}

暂无
暂无

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

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