我是Android开发的新手,所以不要苛刻
我正在创建一个包含WebView的Android应用程序。WebView具有许多直播流的链接。
阅读了各种帖子后,我知道在WebView本身中播放视频确实很困难和复杂,因为WebView没有/支持视频流的许多功能
我的问题是:如何在用户在WebView中选择特定链接时打开MxPlayer并播放流?
我只是在我自己的设备上测试它,所以想将其特定于MX播放器,因为我已经安装了
我尝试了以下内容:
public class WebPageLoader extends Activity {
final Activity activity = this;
WebView webView;
private String pknamepro;
String homePage = "";
String specificWebURL = "";
public WebPageLoader(){
pknamepro = "com.mxtech.videoplayer.pro";
}
private void startMx(String url)
{
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setComponent(new ComponentName("com.mxtech.videoplayer.pro", "com.mxtech.videoplayer.pro.ActivityScreen"));
intent.setData(Uri.parse(url));
intent.putExtra("secure_uri", true);
startActivity(intent);
return;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webpageloader);
webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setSupportZoom(false);
webView.getSettings().setBuiltInZoomControls(false);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.setScrollbarFadingEnabled(true);
webView.getSettings().setLoadsImagesAutomatically(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDatabaseEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
activity.setTitle("Loading...");
activity.setProgress(progress * 100);
if(progress == 100)
activity.setTitle(R.string.app_name);
}
});
webView.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
Toast.makeText(getApplicationContext(), "Internet connection down" + description, Toast.LENGTH_SHORT).show();
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
if (url.contains("rtmp")|| url.contains(specificWebURL) || url.startsWith("rtmpe")|| url.startsWith("udp")) {
startMx(url);
return true;
}
view.loadUrl(url);
return true;
}
});
webView.loadUrl(homePage);
}
protected void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
// Save the state of the WebView
webView.saveState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
// Restore the state of the WebView
webView.restoreState(savedInstanceState);
}
public void onBackPressed() {
if(webView.canGoBack() == true){
webView.goBack();
}else{
webView.goBack();
}
}
,如您所见,我已经通过检查URL并尝试硬编码URL而尝试过,但两项工作都没有使用。
似乎没有执行上述任何检查,并且试图在WebView IT中播放视频,因为每当我单击任何链接时,一个新页面都会打开,说"错误加载播放器:无播放来源找到"而不是在我的设备上打开MxPlayer。
如果其他人有与我的问题相同的问题,下面是我的解决方案。
Intent myIntent;
PackageManager pm = getPackageManager();
try{
myIntent = pm.getLaunchIntentForPackage("com.mxtech.videoplayer.pro");
myIntent.setComponent(new ComponentName("com.mxtech.videoplayer.pro", "com.mxtech.videoplayer.ActivityScreen"));
myIntent.setDataAndType(Uri.parse(url), "application/x-mpegURL");
myIntent.putExtra("secure_uri", true);
myIntent.putExtra(EXTRA_DECODE_MODE, (byte)2);
if (null != myIntent)
this.startActivity(myIntent);
}
catch (ActivityNotFoundException e)
{
Toast.makeText(getApplicationContext(),
" stream not working ",
Toast.LENGTH_LONG).show();
}