當我按下後退按鈕並且正在播放視頻時,我想關閉VideoView。當我按下返回按鈕時關閉VideoView中的視頻
這裏我的代碼:
public class MainActivity extends AppCompatActivity {
public Integer[] mVideo = {
R.raw.clesella, R.raw.simonino,
R.raw.ennio, R.raw.zip,
R.raw.pippo, R.raw.monituri
};
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new VideoAdapter(this));
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final VideoView mVideoView2 = (VideoView) findViewById(R.id.video);
String uriPath = "android.resource://"+ getPackageName()+"/"+mVideo[position];
Uri uri2 = Uri.parse(uriPath);
mVideoView2.setVideoURI(uri2);
mVideoView2.requestFocus();
mVideoView2.setVisibility(view.VISIBLE);
gridview.setVisibility(view.GONE);
mVideoView2.start();
mVideoView2.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
//write your code after complete video play
mVideoView2.setVisibility(View.GONE);
gridview.setVisibility(View.VISIBLE);
}
});
mVideoView2.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent motionEvent) {
if (mVideoView2.isPlaying()) {
mVideoView2.pause();
return false;
}
else {
mVideoView2.start();
return false;
}
}
});
}
});
}
}
正如你所看到的,我從原始文件夾中的視頻,我填一個GridView。當我點擊該項目時,視頻開始播放。我想讓VideoView GONE和GridView像我在setOnCompletetionListener中一樣可見,但是當視頻正在播放時,我按下後退按鈕。
感謝您的幫助。確切地說,我必須把這部分代碼放在哪裏?對於這個問題抱歉,我現在開始使用android開發。 –
你應該把它放在你的'MainActivity'中。 剛剛開始輸入'onBack'並使用Ctrl +空格鍵......並且發生了魔法;) –
您的活動中的任何位置。 –