Issue
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;
}
}
Solution
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.
Answered By - Yalla T.
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.