简体   繁体   English

Android-检查是否未安装媒体播放器并显示祝酒词

[英]Android - Check if a Media player is not installed and show a toast

I've been trying to get html5 video to play in webview. 我一直在尝试让html5视频在webview中播放。 I actually got that to work but then I had orientation issues. 我实际上使它起作用,但随后出现了定向问题。 I decided to let a media player handle the file so that it can take care of orientation etc. This seems to work great. 我决定让媒体播放器处理该文件,以便它可以处理方向等。这似乎很好用。 However, I do I modify the code so that if a player isn't available it gives a toast and not cause my app to crash? 但是,我会修改代码,以便在没有播放器的情况下举杯庆祝,并且不会导致我的应用程序崩溃? any suggestions? 有什么建议么?

private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (Uri.parse(url).getHost().equals("my.site.com")) {
                return false;
            }else if(url.endsWith(".mp4")) {
                Intent intent = new Intent("android.intent.action.VIEW", Uri.parse(url));
                intent.setDataAndType(Uri.parse(url), "video/*");
                view.getContext().startActivity(intent);
                return true;
            }

            // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(intent);
            return true;
        }


    }

If you want to check if the current hardware is able to handle the Intent: 如果要检查当前硬件是否能够处理该意图:

   if(intent.resolveActivity(getPackageManager()) != null){
// start mediaplayer
}else{
//toast no mediaplayer available
}

If you want to check for a specific MediaPlayer ( in this example Google Play Movies): 如果要检查特定的MediaPlayer(在此示例中为Google Play电影):

if(isMediaPlayerAvailable()){
    // start mediaplayer
    }else{
    //toast no mediaplayer available
    }


public static boolean isMediaPlayerAvailable(Context context) {
        String mVersion;
        try {
            mVersion = context.getPackageManager().getPackageInfo(
                    "com.google.android.videos", 0).versionName;
              Log.d("MediaPlayer", "Installed: " + mVersion);
              return true;
        } catch (NameNotFoundException e) {
              Log.d("MediaPlayer", "Not installed");
              return false;
        }
       }

Different approach: Hide Webview, play video in VideoView 不同的方法:隐藏Webview,在VideoView中播放视频

You should create a VideoView with android:visiblity="gone" in the layout xml. 您应该在布局xml中使用android:visiblity="gone"创建一个VideoView。

else if(url.endsWith(".mp4")) {

view.setVisibility(View.GONE);

   VideoView myVideoView = (VideoView)findViewById(R.id.myvideoview);
   myVideoView.setVisibility(View.VISIBILE);
   myVideoView.setVideoURI(Uri.parse(url));
   myVideoView.setMediaController(new MediaController(this));
   myVideoView.requestFocus();
   myVideoView.start();
            }

You should also look into the different Listeners you can add to a VideoView to revert the Visibility changes when the Video is done playing or is unable to load. 您还应该查看可以添加到VideoView中的不同侦听器,以在视频播放完毕或无法加载时还原可视性更改。

VideoView & Fullscreen & Orientation changes - Android has a answer how to resume playback at the same position after a orientation change. VideoView,全屏和方向更改-方向更改后,Android提供了一个如何在相同位置恢复播放的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM