0

I've been trying to get html5 video to play in 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;
        }


    }
Panama Jack
  • 24,158
  • 10
  • 63
  • 95

1 Answers1

0

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):

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

You should create a VideoView with android:visiblity="gone" in the layout xml.

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 & Fullscreen & Orientation changes - Android has a answer how to resume playback at the same position after a orientation change.

Community
  • 1
  • 1
Yalla T.
  • 3,707
  • 3
  • 23
  • 36
  • Thanks, I tried your first condition but it still brought up the "Complete Action using" window. With browser options etc. How can I suppress that and just show the toast? – Panama Jack Apr 18 '13 at 23:19
  • Well if there are options available(applications that register to handle the intent to view video files), then it will show the dialog ofcourse. But maybe i'm not understanding correctly what you mean. – Yalla T. Apr 18 '13 at 23:24
  • I see why it still promoted the window, because Google Chrome can still play the video in the browser. Hmm, I really only want an actually media player to play it, because Chrome will explose the URL to the media file in the address bar. Any suggestions. – Panama Jack Apr 18 '13 at 23:24
  • okay, you can play around a bit with intent.resolveActivity(getPackageManager()) and see what it returns. I dont know what happens if there are more options available - if it returns a list or whatnot but you may be able to check the packagename of the returned activity and not only ban the null case but also if it returns a browser, like com.google.android.chrome – Yalla T. Apr 18 '13 at 23:28
  • by the way, I would have another suggestion to handle your initial problem with playing the video. Instead of starting a new Intent to play it, why not make the webview temporarly hidden, and create a Videoview to cover the screen and play the video in there ? – Yalla T. Apr 18 '13 at 23:32
  • Oh wow, didn't really know about videoview.(very new to android). I like that. Do you have code example of how to hide the webview, show the video and show webview if they back out of the video or its done? I just found this link on videoview. http://android-coding.blogspot.com/2011/03/simple-example-using-videoview-to-play.html – Panama Jack Apr 18 '13 at 23:37
  • I added it to my answer. It's not complete, but i'm sure you will figure out the details. – Yalla T. Apr 18 '13 at 23:50
  • Ok thanks last question, It doesn't like this line `myVideoView.setMediaController(new MediaController(this));`. It tells me the constructor MediaController is undefined. The import is there for MediaControler. – Panama Jack Apr 19 '13 at 00:02
  • I think I got it.Thanks for all the help – Panama Jack Apr 19 '13 at 00:19