我正在创建一个可以通过手机网站播放视频的应用。现在我有一个问题,当用户按下主页按钮时,视频会自动暂停。如何防止应用暂停视频?
代码:
public class MainActivity extends Activity {
private WebView mWebView;
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
// Force links and redirects to open in the WebView instead of in a browser
mWebView.setWebViewClient(new WebViewClient());
// Enable Javascript
mWebView.getSettings().setAppCacheEnabled(false);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setInitialScale(1);
mWebView.getSettings().setPluginState( WebSettings.PluginState.ON);
WebSettings webSettings = mWebView.getSettings();
webSettings.setLoadsImagesAutomatically(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setUseWideViewPort(true);
// Use remote resource
mWebView.loadUrl("http://some.website");
// Stop local links and redirects from opening in browser instead of WebView
mWebView.setWebViewClient(new MyAppWebViewClient());
// mWebView.loadUrl("javascript:(function() { document.getElementsByClassName('ytp-large-play-button ytp-button')[0].click(); })()");
mWebView.setWebViewClient(new MyAppWebViewClient() {
// autoplay when finished loading via javascript injection
public void onPageFinished(WebView view, String url) { mWebView.loadUrl("javascript:(function() { document.getElementById('play').click(); })()"); }
});
// Use local resource
// mWebView.loadUrl("file:///android_asset/www/index.html");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
mWebView.getSettings().setMediaPlaybackRequiresUserGesture(false);
}
}
感谢您的建议
答案 0 :(得分:0)
我解决了这个问题,即为音乐播放创建MediaService。