我想让视频在视频播完后继续进行下一个活动。我已经看到其他帖子试图做同样的事情,并且在尝试实现它们时,我继续在我的其余代码中得到错误。在尝试添加最后一部分之前,我的视频播放得很好。有没有人看到我的代码有任何问题?提前谢谢。
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;
}
}
}
答案 0 :(得分:0)
更改
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();
}