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
Related
I want to tell you I never try android development before, and this is the first time I'm doing an android app I build a website using ASP.NET and I want to run that site within the android app web view.
So followed a few tutorials and I did so far.
Here when I redirect to some external sites In my site, I open them on another new tab on the browser, here I want to open it on the mobile phone's default browser instead of the opening inside the web view.
need a help to doing that.
This is my app MainActivity code.
public class MainActivity extends AppCompatActivity {
WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView=findViewById(R.id.webview);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("http://www.mycustom:5500/test/");
WebSettings websettings = webView.getSettings();
websettings.setJavaScriptEnabled(true);
}
private class MyWebView extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
return super.shouldOverrideUrlLoading(view, request);
}
}
#Override
public void onBackPressed() {
if (webView.isFocused() && webView.canGoBack()){
webView.goBack();
}else{
super.onBackPressed();
}
}
}
I am trying to implement a progress bar at the top of my web-view while the page loads. Just like chrome.
How can i do this?
i have a swipe refresh view in a container.
I've looked at a lot of answers here and tried to implement them, but had no luck.
I just want to implement a simple progress bar (like chrome) every time the web-view loads.
public class MainActivity extends Activity {
WebView browser;
SwipeRefreshLayout mySwipeRefreshLayout;
private ViewTreeObserver.OnScrollChangedListener mOnScrollChangedListener;
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// find the WebView by name in the main.xml of step 2
browser=(WebView)findViewById(R.id.webview);
// Enable javascript
browser.getSettings().setJavaScriptEnabled(true);
// Set WebView client
browser.setWebChromeClient(new WebChromeClient());
browser.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// open in Webview
if (url.contains("example.com") ){
// Can be clever about it like so where myshost is defined in your strings file
// if (Uri.parse(url).getHost().equals(getString(R.string.myhost)))
return false;
}
// open rest of URLS in default browser
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
public void onPageFinished(WebView view, String url) {
mySwipeRefreshLayout.setRefreshing(false);
}
});
// Load the webpage
browser.loadUrl("https://example.com/");
mySwipeRefreshLayout = this.findViewById(R.id.swipeContainer);
mySwipeRefreshLayout.setOnRefreshListener(
new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
browser.reload();
}
}
);
}
i make apps of my Wordpress websites for android and i use WebView in my apps, i want to show Interstitial Ads in my app but when users click on Next Page Button Which is coded in my website, my next page button is like this
<?php next_posts_link( '<span class="nextpost"> '.__('Next', 'wp-mobile-edition').' ›</span>' );
i want to show Interstitial ad when user click on next button.
how it can be ? thanks
Try below code
#SuppressLint("SetJavaScriptEnabled")
public class MainActivity extends Activity {
private WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView)findViewById(R.id.webView1);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setLoadsImagesAutomatically(true);
mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
mWebView.setWebViewClient(new MyBrowser());
mWebView.loadUrl("file:///android_asset/index.html");
}
private class myWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
view.addJavascriptInterface(new Object()
{
#JavascriptInterface
public void performClick() throws Exception
{
Toast.makeText(MainActivity.this, "Login clicked", Toast.LENGTH_LONG).show();
// implement your logic here for show Add
}
}, "login");
return true;
}
}
}
I want to show the URl of a video in a TOAST, each time the user click on a video. I get this toast shown only once when I Launch an app first time. Here is my Code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mywebview=(WebView)findViewById(R.id.webView);
mywebview.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
Toast.makeText(MainActivity.this,url, Toast.LENGTH_SHORT).show();
return true;
}
});
mywebview.getSettings().setJavaScriptEnabled(true);
mywebview.loadUrl("http://www.youtube.com");
}
My App has a WebView which loads a simple html. However, this html links to a rtsp live video stream and the WebView is able to load it, but when I click on one video it shows as loading the player but after some time getting message "Can't play this video.. When I open the rtsp link in the native Android browser, it loads and works fine so I know it's not the video stream being incompatible. Is there something within WebView which can be enabled to allow the rtsp video stream to be played? I tried this in both emulator and original device.
Thanks!
public class MainActivity extends Activity
{
private Button button;
public void onCreate(Bundle savedInstanceState)
{
final Context context = this;
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.buttonUrl);
button.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0)
{
Intent intent = new Intent(context, WebViewActivity.class);
startActivity(intent);
}
});
}
}
public class WebViewActivity extends Activity
{
private WebView webView;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://myurl.com/");
}
}
I was able to redirect the stream to a video player using:
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("rtsp")) {
Uri uri = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
return true;
}
return super.shouldOverrideUrlLoading(view, url);
}
});