gpt4 book ai didi

java - 按下主页按钮后如何在Android中播放mediaPlayer并在收到电话时恢复播放视频?

转载 作者:行者123 更新时间:2023-11-30 02:45:33 25 4
gpt4 key购买 nike

你能告诉我当我按下模拟器或手机的主页按钮时如何在Android中播放mediaPlayer吗?我想要它,以便当它接到电话以停止视频并能够再次恢复视频时。我应该如何在此代码中执行此操作?

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.MediaController;
import android.widget.VideoView;

public class VideoActivity2 extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video2);

VideoView videoView = (VideoView)this.findViewById(R.id.videoView2);
videoView.setVideoURI(Uri.parse("android.resource://"+
getPackageName() +"/"R.raw.song1));
videoView.setMediaController(new MediaController(this));
videoView.requestFocus();
videoView.start();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.video_activity2, menu);
return true;
}

public void startVideoActivity3(View v) {
Intent intent = new Intent(VideoActivity2.this, VideoActivity3.class);
startActivity(intent);
}
public void startVideoActivity(View v) {
Intent intent = new Intent(VideoActivity2.this, VideoActivity.class);
startActivity(intent);
}
public void startMainActivity(View v) {
Intent intent = new Intent(VideoActivity2.this,

MainActivity.class);intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
if (getIntent().getBooleanExtra("EXIT", false)) {
finish();
}
startActivity(intent);
}

最佳答案

我从不使用 VideoView 类,但根据文档,这应该有效:

@Override
public void onPause() {
super.onPause();
VideoView videoView = (VideoView)findViewById(R.id.videoView2);
videoView.pause();
}

@Override
public void onResume() {
super.onResume();
VideoView videoView = (VideoView)findViewById(R.id.videoView2);
videoView.resume();
}

关于java - 按下主页按钮后如何在Android中播放mediaPlayer并在收到电话时恢复播放视频?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25102382/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com