Javascript Android webview实时流视频在几分钟后停止播放
我制作了一个关于直播电视流的应用程序。它运行良好,但几分钟后自动停止。我点击屏幕,但它不工作。我已经显示了我的代码。如果有任何需要添加或删除的内容,请告诉我。我已经试过了,但失败了Javascript Android webview实时流视频在几分钟后停止播放,javascript,android,webview,live,Javascript,Android,Webview,Live,我制作了一个关于直播电视流的应用程序。它运行良好,但几分钟后自动停止。我点击屏幕,但它不工作。我已经显示了我的代码。如果有任何需要添加或删除的内容,请告诉我。我已经试过了,但失败了 package ******************************.thelivetv; import android.annotation.SuppressLint; import android.app.Activity; import android.app.AlertDialog; import a
package ******************************.thelivetv;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;
public class MainActivity extends Activity {
InterstitialAd mInterstitialAd;
private InterstitialAd interstitial;
private WebView webView;
String url2 = "file:///android_asset/error.html";
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
webView = new WebView(this);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(webView);
final WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setJavaScriptCanOpenWindowsAutomatically(true);
settings.setPluginState(WebSettings.PluginState.ON);
if (isOnline(MainActivity.this)) {
webView.setWebViewClient(new WebViewClient() {
// autoplay when finished loading via javascript injection
public void onPageFinished(WebView view, String url) {
webView.loadUrl("javascript:(function() { document.getElementsByTagName('video')[0].play(); })()");
}
});
webView.setWebChromeClient(new WebChromeClient());
web.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
webView.loadUrl("http://shafatillullah.com/blackboy/channel9/");
} else {
String message = "Internet connection not available. Try later";
new AlertDialog.Builder(MainActivity.this)
.setMessage(message)
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
})
.show();
}
//full screen ad
final AdRequest adRequest = new AdRequest.Builder().build();
// Prepare the Interstitial Ad
interstitial = new InterstitialAd(MainActivity.this);
// Insert the Ad Unit ID
interstitial.setAdUnitId(getString(R.string.bigscreen));
interstitial.loadAd(adRequest);
// Prepare an Interstitial Ad Listener
interstitial.setAdListener(new AdListener() {
public void onAdLoaded() {
// Call displayInterstitial() function
displayInterstitial();
}
});
//end ad view
}
//Admob ad load
public void displayInterstitial() {
// If Ads are loaded, show Interstitial else show nothing.
if (interstitial.isLoaded()) {
interstitial.show();
}
}
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Log.i("WEB_VIEW_TEST", "error code:" + errorCode);
super.onReceivedError(view, errorCode, description, failingUrl);
view.loadUrl(url2);
}
}
// For full screen View
class FullScreenActivity {
public void invoke() {
// remove title
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
}
@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
@Override
public void onPause() {
super.onPause();
webView.onPause();
}
@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
@Override
public void onResume() {
super.onResume();
webView.onResume();
}
@Override
public void onBackPressed() {
if (webView.canGoBack()) {
webView.goBack();
} else {
super.onBackPressed();
}
}
public static boolean isOnline(Context c) {
boolean result = false;
ConnectivityManager cm = (ConnectivityManager) c.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ninfo = cm.getActiveNetworkInfo();
if (ninfo != null) {
result = ninfo.isConnected();
}
return result;
}
}
我知道这个问题不符合社区的指导原则,但是来吧,帮助一个程序员同事 正如中所讨论的,WebView中的流媒体存在一个问题,因此我认为您最好使用Android的媒体播放器
荣誉 请注意,“请帮助我”和“请编辑我的代码并为我修复它”的问题通常在这里被大量否决,所以当我看到一个上面有+2的问题时,我的直觉告诉我,OP知道的人都在投票。如果你知道有人这样做,请告诉他们不要这样做——我见过有很多人通过欺诈手段获得了声誉,他们的分数在一夜之间就被发现了。所以,这个问题需要进行一些编辑,以使其成为主题。“不起作用”是什么意思?你做了什么调试?我们可以看一下编辑到问题中的崩溃日志吗?您从自己查看堆栈跟踪中学到了什么?要回答您的问题,您应该尽可能清楚并提供帮助。第一步是通过更新评论来澄清评论中提出的问题。