Video plays only once in Webview of Android - android

I am succeeded to play streamed Youtube video from HTML5 content in Webview in Android but now problem is that video plays only first time. After that VideoView only goes to end of the video file.
I tried clearing cache as suggested here but no luck.
What could be the possible solution for this problem?
Please try following code to run Video, this has been using some suggestions given on stackoverflow.com
WebView webView = (WebView) findViewById(R.id.product_details_webview);
WebSettings webSettings = webView.getSettings();
webSettings.setPluginState(WebSettings.PluginState.ON);
webSettings.setJavaScriptEnabled(true);
webSettings.setUseWideViewPort(true);
webSettings.setLoadWithOverviewMode(true);
webView.setWebChromeClient(new chromeClient());
webView.setWebViewClient(new WebViewClient(){
});
webView.loadUrl(url);
public class chromeClient extends WebChromeClient implements OnCompletionListener,
OnErrorListener{
private WebView wv;
private VideoView mVideoView;
private LinearLayout mContentView;
private FrameLayout mCustomViewContainer;
private WebChromeClient.CustomViewCallback mCustomViewCallback;
FrameLayout.LayoutParams COVER_SCREEN_GRAVITY_CENTER = new
FrameLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER);
#Override
public void onShowCustomView(View view, CustomViewCallback callback) {
if (view instanceof FrameLayout) {
wv = (WebView)findViewById(R.id.product_details_webview);
mCustomViewContainer = (FrameLayout) view;
mCustomViewCallback = callback;
mContentView = (LinearLayout)findViewById(R.id.linearlayout1);
if (mCustomViewContainer.getFocusedChild() instanceof VideoView) {
mVideoView = (VideoView) mCustomViewContainer.getFocusedChild();
// frame.removeView(video);
mContentView.setVisibility(View.GONE);
mCustomViewContainer.setVisibility(View.VISIBLE);
setContentView(mCustomViewContainer);
mVideoView.setOnCompletionListener(this);
mVideoView.setOnErrorListener(this);
mVideoView.start();
}
}
}
public void onHideCustomView() {
if (mVideoView == null){
return;
}else{
// Hide the custom view.
mVideoView.setVisibility(View.GONE);
// Remove the custom view from its container.
mCustomViewContainer.removeView(mVideoView);
mVideoView = null;
mCustomViewContainer.setVisibility(View.GONE);
mCustomViewCallback.onCustomViewHidden();
// Show the content view.
mContentView.setVisibility(View.VISIBLE);
}
}
public void onCompletion(MediaPlayer mp) {
mp.stop();
mCustomViewContainer.setVisibility(View.GONE);
onHideCustomView();
setContentView(mContentView);
}
public boolean onError(MediaPlayer arg0, int arg1, int arg2) {
setContentView(mContentView);
return true;
}
}

Add in your chrome client :
#Override public void onPrepared(MediaPlayer mp) {
customViewCallback.onCustomViewHidden();
}
where customViewCallback = callback;

You got a video working inside a WebView with HTML5? Congrats! I failed trying to do that :(
To fix my issue I ended up leaving a place holder in the HTML and handling the video from native code using the MediaPlayer class.

I had this problem as well.
I had to add a seek(0).
Seems like the next video played from exactly where the last video ended.
So I had to go to the beginning of the video before playing.

Related

Android Webview Permission denied error when try to video call

I am using a webview to load a url which has a feature to video call.
When i try to initiate a video call. I get the following error "Permission denied. Please refresh the page and allow access to your camera and microphone" .
If i give android runtime permission of CAMERA and RECORD_AUDIO. The video call will be initiated and the video will be transmitted to the computer also. But on the mobile side, I cannot view my video as well as the receiver's video. It shows the getDefaultVideoPoster() only.
I have already tried using WebChromeClient and WebViewClient but still no luck.
Here is my code:
private WebView webView;
webView = findViewById(R.id.webview);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setSaveFormData(true);
webSettings.setSupportZoom(false);
webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
webSettings.setPluginState(WebSettings.PluginState.ON);
webSettings.setUseWideViewPort(true);
webSettings.setLoadWithOverviewMode(true);
webView.setWebViewClient(new MyWebViewClient());
webView.setWebChromeClient(new WebChromeClient() {
#Override
public void onPermissionRequest(final PermissionRequest request) {
myRequest = request;
for (String permission : request.getResources()) {
switch (permission) {
case "android.webkit.resource.AUDIO_CAPTURE": {
askForPermission(request.getOrigin().toString(), Manifest.permission.RECORD_AUDIO, MY_PERMISSIONS_REQUEST_RECORD_AUDIO);
break;
}
case Manifest.permission.RECORD_AUDIO: {
askForPermission(request.getOrigin().toString(), Manifest.permission.RECORD_AUDIO, MY_PERMISSIONS_REQUEST_RECORD_AUDIO);
break;
}
case Manifest.permission.CAMERA: {
askForPermission(request.getOrigin().toString(), Manifest.permission.CAMERA, MY_PERMISSIONS_REQUEST_CAMERA);
break;
}
}
}
}
#Nullable
#Override
public Bitmap getDefaultVideoPoster() {
Log.i(TAG, "getDefaultVideoPoster");
return drawableToBitmap(getResources().getDrawable( R.drawable.ic_temperature));
//return super.getDefaultVideoPoster();
}
#Override
public void onShowCustomView(View view, CustomViewCallback callback) {
super.onShowCustomView(view, callback);
Log.i(TAG, "onShowCustomView");
if (view instanceof FrameLayout){
FrameLayout frame = (FrameLayout) view;
if (frame.getFocusedChild() instanceof VideoView){
VideoView video = (VideoView)
frame.getFocusedChild();
frame.removeView(video);
video.start();
}
}
}
});
webView.getSettings().setMediaPlaybackRequiresUserGesture(false);
The above line code worked for me

Android WebView - Youtube videos not playing within the application

I have an activity which is displaying a web page using a WebView. Within that page, there is a link to a YouTube video (so it's not a video I can or need to embed).
The problem is that the video won't play - When i click on the play button, a error page appears saying
" Webpage not available
The webpage at vnd.youtube:SVf8Ghl6d8xx
might be temporarily down and blah blah blah !!!"
Please only answer if you know what i want! I have gone through almost all related posts on stackoverflow so there is no need of references to other posts/question.
MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// add the custom view to the action bar
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2)
webView.getSettings().setPluginState(WebSettings.PluginState.ON); //Helps flash run on a device
webView.getSettings().setBuiltInZoomControls(true);
webView.setWebViewClient(new WebViewClient ());
webView.setWebChromeClient(new WebChromeClient());
webView.getSettings().setUseWideViewPort(true);
//webView.getSettings().setUseWideViewPort(false);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.loadUrl(url);
}
The following code make it run in the youtube app but i want the videos to run in my app :(
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// add the custom view to the action bar
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2)
webView.getSettings().setPluginState(WebSettings.PluginState.ON);
webView.setWebViewClient(new WebViewClient () {
# Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("vnd.youtube")){
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
}
else return false;
}
});
webView.setWebChromeClient(new WebChromeClient());
webView.getSettings().setUseWideViewPort(true);
//webView.getSettings().setUseWideViewPort(false);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.loadUrl(url);
}
Go to Google Developer Console and select or create a new project.
On the left sidebar, select APIs under APIs & auth and turn the status ON for YouTube Data API v3.
On the left sidebar, select Credentials and Create new key under Public API access.
When popup comes asking you to choose platform, select Android Key.
Paste the SHA-1 key and your project’s package name separated by semicolon(;).
Click on create. Now you should see the API KEY on the dashboard.
download YouTubeAPI jar from here
https://developers.google.com/youtube/android/player/downloads/
Create a layout like this
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.youtube.player.YouTubePlayerView
android:id="#+id/youtube_player"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/black"
android:layout_centerInParent="true" />
</RelativeLayout>
Your activity should look something like this
public class CustomYouTubePlayer extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener{
private String API_KEY="your key";
private String VIDEO_ID;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/** attaching layout xml **/
setContentView(R.layout.youtube_player);
/** Initializing YouTube player view **/
VIDEO_ID = getIntent().getExtras().getString(FinalVariables.YOUTUBE_ID);
YouTubePlayerView youTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtube_player);
youTubePlayerView.initialize(API_KEY, this);
}
#Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player, boolean wasRestored) {
player.setShowFullscreenButton(false);
/** add listeners to YouTubePlayer instance **/
player.setPlayerStateChangeListener(playerStateChangeListener);
player.setPlaybackEventListener(playbackEventListener);
/** Start buffering **/
if (!wasRestored) {
player.loadVideo(VIDEO_ID);
}
}
private YouTubePlayer.PlaybackEventListener playbackEventListener = new YouTubePlayer.PlaybackEventListener() {
#Override
public void onBuffering(boolean arg0) {
}
#Override
public void onPaused() {
}
#Override
public void onPlaying() {
}
#Override
public void onSeekTo(int arg0) {
}
#Override
public void onStopped() {
}
};
private YouTubePlayer.PlayerStateChangeListener playerStateChangeListener = new YouTubePlayer.PlayerStateChangeListener() {
#Override
public void onAdStarted() {
}
#Override
public void onError(YouTubePlayer.ErrorReason arg0) {
}
#Override
public void onLoaded(String arg0) {
}
#Override
public void onLoading() {
}
#Override
public void onVideoEnded() {
finish();
}
#Override
public void onVideoStarted() {
}
};
}
Just pass the video id and that's all simple as that..
Edit
if this is your YouTube link https://www.youtube.com/watch?v=rql_F8H3h9E
then your video_id=rql_F8H3h9E
Extract your video id from YouTube link and sent to this activity as extra variable.
The Best Way you could embed youtube videos in the app is by using Youtube API
The YouTube Android Player API enables you to incorporate video
playback functionality into your Android applications. The API defines
methods for loading and playing YouTube videos (and playlists) and for
customizing and controlling the video playback experience.
For this you will convert the iFrame code of your embed YouTube video to a string and load it to webview as a string in your application.
For example,
String frameVideo = "<html><body>Youtube video .. <br> <iframe width="320" height="315" src="https://www.youtube.com/embed/lY2H2ZP56K4" frameborder="0" allowfullscreen></iframe></body></html>";
then load it to your webview after all the normal webview settings
WebView displayVideo = (WebView)findViewById(R.id.webView);
displayVideo.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
});
WebSettings webSettings = displayVideo.getSettings();
webSettings.setJavaScriptEnabled(true);
displayVideo.loadData(frameVideo, "text/html", "utf-8");
For more information refer this links 1 2 3

Playing youtube videos smoothly in web view

I am building an app where a user can see a YouTube video and can see the images for which link is coming from the sever. To play YouTube video embed key's value is coming from server as
`<div id="_cvp_11204"><span>
</span></div>
<script type="text/javascript">
(function(){var a;a=new XMLHttpRequest;a.onreadystatechange=function(){rs=a.readyState;if(4==rs&&200==a.status){var c=JSON.parse(a.responseText),b;for(b in c.payload)if(c.payload.hasOwnProperty(b)){var d=c.payload[b];document.getElementById("cvp"+b).innerHTML=d.view}}};a.open("GET","http://dummyurl.com/media.ids=?11134 ));a.send()})(window);
</script>`
and i am able to play the video also but i am facing few issue in terms of performance.
1) sometimes voices starts but video starts showing after some time.
2) Sometime controls doesn't show up.
3) How can i play the video directly in fullscreen mode?Currently to play the video in full screen mode i have to click on the max control to play it.
As is the activity class for webview
public class MainActivity extends Activity {
protected FrameLayout webViewPlaceholder;
protected WebView webView;
private FrameLayout mContentView;
private MyWebChromeClient mWebChromeClient = null;
private View mCustomView;
private FrameLayout mCustomViewContainer;
private WebChromeClient.CustomViewCallback mCustomViewCallback;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.out.println("Oncreate is getting called ---------------------------");
// Initialize the UI
if(savedInstanceState == null){
initUI();
}
}
protected void initUI()
{
// Retrieve UI elements
webViewPlaceholder = ((FrameLayout)findViewById(R.id.webViewPlaceholder));
mContentView = webViewPlaceholder;
// Initialize the WebView if necessary
if (webView == null)
{
System.out.println("webView == null -----------------------------------");
// Create the webview
webView = new WebView(this);
webView.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setAllowContentAccess(true);
webSettings.setDomStorageEnabled(true);
webSettings.setRenderPriority(RenderPriority.HIGH);
webSettings.setUseWideViewPort(false);
webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
//--------------------------------------------------------
if (Build.VERSION.SDK_INT < 8) {
webSettings.setPluginsEnabled(true);
} else {
webSettings.setPluginState(PluginState.ON);
}
mWebChromeClient = new MyWebChromeClient();
webView.setWebChromeClient(mWebChromeClient);
// Load the URLs inside the WebView, not in the external web browser
//webView.setWebViewClient(new WebViewClient());
// Load a page
loadWebView("asfas");
}
// Attach the WebView to its placeholder
webViewPlaceholder.addView(webView);
}
private void loadWebView(final String url) {
String s = ReadFromfile("link.txt", MainActivity.this);
s = "<html><body>"+s+"</body></html>";
webView.loadDataWithBaseURL("",Html.fromHtml(s).toString(),"text/html", "UTF-8",null);
}
public String ReadFromfile(String fileName, Context context) {
StringBuilder ReturnString = new StringBuilder();
InputStream fIn = null;
InputStreamReader isr = null;
BufferedReader input = null;
try {
fIn = context.getResources().getAssets()
.open(fileName, context.MODE_WORLD_READABLE);
isr = new InputStreamReader(fIn);
input = new BufferedReader(isr);
String line = "";
while ((line = input.readLine()) != null) {
ReturnString.append(line);
}
} catch (Exception e) {
e.getMessage();
} finally {
try {
if (isr != null)
isr.close();
if (fIn != null)
fIn.close();
if (input != null)
input.close();
} catch (Exception e2) {
e2.getMessage();
}
}
return ReturnString.toString();
}
#Override
public void onConfigurationChanged(Configuration newConfig)
{
if (webView != null)
{
// Remove the WebView from the old placeholder
System.out.println("web view is removed from the holder -------------------------");
webViewPlaceholder.removeView(webView);
}
super.onConfigurationChanged(newConfig);
// Load the layout resource for the new configuration
setContentView(R.layout.activity_main);
// Reinitialize the UI
initUI();
}
#Override
protected void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
System.out.println("onSaveInstanceState -------------------------");
// Save the state of the WebView
webView.saveState(outState);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
System.out.println("onRestoreInstanceState -------------------------");
// Restore the state of the WebView
webView.restoreState(savedInstanceState);
}
private class MyWebChromeClient extends WebChromeClient {
FrameLayout.LayoutParams LayoutParameters = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT);
#Override
public void onShowCustomView(View view, CustomViewCallback callback) {
// if a view already exists then immediately terminate the new one
if (mCustomView != null) {
callback.onCustomViewHidden();
return;
}
mContentView.setVisibility(View.GONE);
mCustomViewContainer = new FrameLayout(MainActivity.this);
mCustomViewContainer.setLayoutParams(LayoutParameters);
mCustomViewContainer.setBackgroundResource(android.R.color.black);
view.setLayoutParams(LayoutParameters);
// Sometimes getting remove view first it parent excepetion on moto devices
//webViewPlaceholder.removeView(webView);
mCustomViewContainer.addView(view);
mCustomView = view;
mCustomViewCallback = callback;
mCustomViewContainer.setVisibility(View.VISIBLE);
setContentView(mCustomViewContainer);
}
#Override
public void onHideCustomView() {
if (mCustomView == null) {
return;
} else {
// Hide the custom view.
mCustomView.setVisibility(View.GONE);
// Remove the custom view from its container.
mCustomViewContainer.removeView(mCustomView);
mCustomView = null;
mCustomViewContainer.setVisibility(View.GONE);
mCustomViewCallback.onCustomViewHidden();
// Show the content view.
mContentView.setVisibility(View.VISIBLE);
setContentView(mContentView);
}
}
}
}
And in Manifest also I have set the hardwareaccelarated = true.
here is the video when it starts with controls
and here when i click on maximize button video gets full screen which i want without clicking on max button but video should directly play in fullscreen mode.
So mainly, how I can speed up and increase the performance of my WebView?

does WebChromeClient unsupport php iframe tag?

i load an url given by other system in webview.but it doesn't display all contents,it leaves a blank window in the page.i check the source code,it has a frameset tag and some iframe tags.
att src is correct.why it can't load content from src?does webchromeclient support php iframe tag?
<frameset id ="dispframeset" rows="*,6%" frameborder="NO" border="0" framespacing="0">
<frame id="articleFrame" src="/csp/kbs/showKngContent.action?inRecycle=&kngTblFlag=0&kngId=20130228152142812001&dispId=&articleFlag=true&relativeKngFlag=true&buttonFlag=true&coluKngType=2&kngPath=&coluKngName=&kngPointId=&kngPointName=&kngPointPath=&showType=1&backBtnFlag=&dispTmpPreview=&channelId=0&curChannelId=&currentChannelId=&isBackOrGoahead=&clickingLogFlag=" name="articleFrame" scrolling="no" resize/>
<frame id="articleButtonFrame" src="/csp/kbs/showKngButton.action?kngId=20130228152142812001&dispId=&kngTblFlag=0&buttonFlag=true&showType=1&channelId=0" name="articleButtonFrame" scrolling="auto" resize/>
</frameset>
by the way,the web app works well in IE browser,but sucks in chrome.
thk in advance.
As of my concern WebChromeClient supports iframe tag. In my case I have given the iframe tag to webview in the following way.
<iframe width=\"480\" height=\"270\" src=\"http://staging.snagfilms.com/modules/html5player.jsp?filmId=ed9195a0-a748-11e0-a92a-0026bb61d036&w=500\" frameborder=\"0\" allowfullscreen></iframe>
above is the iframe tag , I am getting from server
myWebView = (CustomFacedWebView) findViewById(R.id.web_wrap_browser);
myWebView.getSettings().setPluginsEnabled(true);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.getSettings().setPluginState(PluginState.ON);
chromeClient = new MyChromeClient();
myWebView.setWebChromeClient(chromeClient);
myWebView.loadUrl(URL)
webRoot = (ViewGroup) findViewById(R.id.web_root_view);
systemRoot = (ViewGroup) webRoot.getParent();
private final class MyChromeClient extends WebChromeClient implements OnCompletionListener, OnErrorListener {
CustomViewCallback fullscreenCallback;
VideoView htmlVideoView;
#Override
public void onShowCustomView(final View view, final CustomViewCallback callback) {
super.onShowCustomView(view, callback);
fullscreenCallback = callback;
systemRoot.removeAllViews();
setContentView(view);
fullScreen = true;
............
}
#TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
#Override
public void onShowCustomView(final View view, final int requestedOrientation,
final CustomViewCallback callback) {
super.onShowCustomView(view, requestedOrientation, callback);
systemRoot.removeAllViews();
setContentView(view);
setRequestedOrientation(requestedOrientation);
fullscreenCallback = callback;
}
#Override
public void onHideCustomView() {
super.onHideCustomView();
if(htmlVideoView != null) {
htmlVideoView.stopPlayback();
}
if(webRoot != null) {
systemRoot.removeAllViews();
setContentView(webRoot);
}
try{
if(fullscreenCallback != null)
fullscreenCallback.onCustomViewHidden();
} catch(Exception e) {
e.printStackTrace();
}
fullScreen = false;
}
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
onHideCustomView();
return false;
}
#Override
public void onCompletion(MediaPlayer mp) {
onHideCustomView();
}
}

How to Play HTML5 video and YouTube Video within Android WebView?

I am trying to play html5 video and youtube video within android webview, but can't display video on android webview screen. and play this video.
I have used below code snippet...
layout xml file naming: test.xml contains below code snipet:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:id="#+id/rltvLayoutTest"
android:layout_height="fill_parent">
<WebView android:id="#+id/webViewTest" android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
and Activity Class name: Test.java contains code given below:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
WebView mWebView = (WebView)findViewById(R.id.webViewTest);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setAllowFileAccess(true);
mWebView.getSettings().setPluginsEnabled(true);
mWebView.setWebChromeClient(new chromeClient());
mWebView.setWebViewClient(new WebViewClient(){
});
mWebView.loadUrl("http://broken-links.com/tests/video/");
}
public class chromeClient extends WebChromeClient implements OnCompletionListener, OnErrorListener{
private WebView wv;
private VideoView mVideoView;
private LinearLayout mContentView;
private FrameLayout mCustomViewContainer;
private WebChromeClient.CustomViewCallback mCustomViewCallback;
FrameLayout.LayoutParams COVER_SCREEN_GRAVITY_CENTER = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT, Gravity.CENTER);
#Override
public void onShowCustomView(View view, CustomViewCallback callback) {
if (view instanceof FrameLayout) {
wv = (WebView)findViewById(R.id.webViewTest);
mCustomViewContainer = (FrameLayout) view;
mCustomViewCallback = callback;
mContentView = (LinearLayout)findViewById(R.id.rltvLayoutTest);
if (mCustomViewContainer.getFocusedChild() instanceof VideoView) {
mVideoView = (VideoView) mCustomViewContainer.getFocusedChild();
// frame.removeView(video);
mContentView.setVisibility(View.GONE);
mCustomViewContainer.setVisibility(View.VISIBLE);
setContentView(mCustomViewContainer);
mVideoView.setOnCompletionListener(this);
mVideoView.setOnErrorListener(this);
mVideoView.start();
}
}
}
public void onHideCustomView() {
if (mVideoView == null)
return;
// Hide the custom view.
mVideoView.setVisibility(View.GONE);
// Remove the custom view from its container.
mCustomViewContainer.removeView(mVideoView);
mVideoView = null;
mCustomViewContainer.setVisibility(View.GONE);
mCustomViewCallback.onCustomViewHidden();
// Show the content view.
mContentView.setVisibility(View.VISIBLE);
}
public void onCompletion(MediaPlayer mp) {
mp.stop();
mCustomViewContainer.setVisibility(View.GONE);
onHideCustomView();
setContentView(mContentView);
}
public boolean onError(MediaPlayer arg0, int arg1, int arg2) {
setContentView(mContentView);
return true;
}
}
I used html5webview to solve this problem.Download and put it into your project then you can code just like this.
private HTML5WebView mWebView;
String url = "SOMEURL";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mWebView = new HTML5WebView(this);
if (savedInstanceState != null) {
mWebView.restoreState(savedInstanceState);
} else {
mWebView.loadUrl(url);
}
setContentView(mWebView.getLayout());
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mWebView.saveState(outState);
}
To make the video rotatable, put android:configChanges="orientation" code into your Activity
for example (Androidmanifest.xml)
<activity android:name=".ui.HTML5Activity" android:configChanges="orientation"/>
and override the onConfigurationChanged method.
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
you haven't added mCustomViewContainer to your view hierarchy
if you are running it on tablet... you add hardwareaccelerated= true in your manifest file and it will certainly work.. also change the sdk version to 11
It worked for me. May help you as well play html videos

Categories

Resources