I'm trying to play YouTube video in my application using WebView.
But when I click "Play" the application crashes.
This is my code:
public class Youtube extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a_video_item);
WebView videoView = (WebView)findViewById(R.id.videoView);
TextView tvTitle = (TextView) findViewById(R.id.VideoTitle);
WebSettings videoViewSettings = videoView.getSettings();
videoViewSettings.setJavaScriptEnabled(true);
videoViewSettings.setPluginState(WebSettings.PluginState.ON);
videoView.setWebChromeClient(new WebChromeClient());
String youtubeID ="XSzE2LLFluE";
tvTitle.setText(youtubeID);
videoView.loadUrl("http://www.youtube.com/embed/" + youtubeID +"?autoplay=1&vq=small");
}
}
And this is my XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<WebView
android:id="#+id/videoView"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:layout_below="#+id/VideoTitle"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"/>
<TextView
android:id="#+id/VideoTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="#string/imgdesc"
android:layout_marginRight="10dp"/>
</RelativeLayout>
I'm testing the project with Nexus 5 on Android Studio emulator.
You have to add the two lines of code:
webView.getSettings().setPluginsEnabled(true); webView.getSettings().setJavaScriptEnabled(true);
this will solve the problem. Try it and tell.
I found the issuse.
Beacuse I did the tests on Emulator without Youtube app its crashes.
On a device with Youtube app its working fine.
Now its crasheswhen i click on 'Full screen' button
Related
I am using webview in my application where i am giving it a audio url.
Its working absolutely fine in some devices. see below screenshot:
But in some device its not looking user friendly.
Is there any way to set the size of webview audio player?
Update: Code snippet:
// open DOI activity
Intent podcastIntent = new Intent(mContext, DoiWebActivity.class);
podcastIntent.setAction(Constants.INTENT_VIEW_LINK);
podcastIntent.putExtra(Constants.EXTRA_DOI_LINK_URL, podcasts.get(0).getPodcastLink());
podcastIntent.putExtra(Constants.CALLING_FRAGMENT,Constants.CALLING_FRAGMENT_PODCAST_LISTING);
podcastIntent.putExtra(Constants.EXTRA_PODCAST_NAME,mContext.getString(R.string.podcast));
mContext.startActivity(podcastIntent);
//Called from podcastListingFragment : podcast name
if (bundle != null && bundle.containsKey(Constants.CALLING_FRAGMENT) &&
bundle.containsKey(Constants.EXTRA_PODCAST_NAME)) {
String title = bundle.getString(SilverChairConstants.EXTRA_PODCAST_NAME);
}
if (Utility.isNetworkAvailable(mActivity)) {
/**
* Showing external link.
*/
mWebView.loadUrl(mIntentText);
} else {
networkNotAvailable();
}
xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
tools:context=".fragments.WebFragment">
<WebView
android:layout_below="#+id/toolbar"
android:id="#+id/wv_webcontainer"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!--progress bar-->
<include layout="#layout/silverchair_progress"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignTop="#id/wv_webcontainer"
android:layout_alignBottom="#id/wv_webcontainer"/>
<include layout="#layout/empty_view"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerInParent="true"
/>
</RelativeLayout>
add this line in your code where you are initializing your webview. mWebView.getSettings().setPluginState(WebSettings.PluginState.ON);
If you have already done this I would recommend you to use MediaPlayer for straming.
MediaPlayer mediaPlayer = MediaPlayer.create(this, Uri.parse("http://vprbbc.streamguys.net:80/vprbbc24.mp3"));
mediaPlayer.start();
See this answer for more details about MediaPlayer streaming.
I know that there are some posts here but they are not solving my problem.
The thing is:
I have a Cordova App (android platform). In my activity_main.xml I have declared a CordovaWebView and I want to call loadUrl() method to load a website that is in my assets folder. When I invoke the method the app crashes showing "UNFORTUNATELY, MyApp HAS STOPPED".
STRANGE THINGS I HAVE NOTICED:
If I emulate using a device with API 10 or 16, this WORKS FINE. If I test it with 17 it does NOT WORK.
If I don't use CordovaWebView and directly invoke: this.loadUrl("file:///android_asset/www/index.html"); it works fine, but it's like I am not using the layouts I define (so I don't have my ad banner).
Any ideas about this problem??
Thanks!
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:id="#+id/layout_home"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="#+id/layout_body"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_above="#+id/layout_banner"
android:background="#color/abc_search_url_text_normal">
<org.apache.cordova.CordovaWebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#color/common_signin_btn_dark_text_disabled" />
</LinearLayout>
<RelativeLayout
android:id="#+id/layout_banner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#color/wallet_hint_foreground_holo_dark">
</RelativeLayout>
</RelativeLayout>
MainAcitivy.java
public class MainActivity extends CordovaActivity {
CordovaWebView cwv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
..LOAD ADVERTISEMENT INTO LAYOUT_BANNER...
cwv = (CordovaWebView) findViewById(R.id.webview);
cwv.getSettings().setJavaScriptEnabled(true);
cwv.loadUrl("file:///android_asset/www/index.html");
}
look the doc : https://cordova.apache.org/docs/en/3.5.0/guide_platforms_android_webview.md.html
cwv = (CordovaWebView) findViewById(R.id.cordovaview);
Config.init(this);
cwv.loadUrl(Config.getStartUrl());
you can see a sample here : https://github.com/dam1/sample-android-cordova-webview
Why do you need activity_main.xml and all android stuff, just start a fresh project using cordova command line. You don't need any xml of that sort in standard cordova project.
You've got cordova plugins for displaying admob ads inside cordova web view, see http://plugreg.com/search?q=admob
I am a beginner in android development.
I was trying to display a video with surface view using media codec for which i am successful.
Now I want to add one more video at run time which has to be displayed or hidden as per the wish of the user or to switch between the two.
Can I have some suggestions for the same...
Thanks......
Try this
public class CustomPictureActivity extends Activity {
/** Called when the activity is first created. */
VideoView vd1,vd2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
vd1=(VideoView) findViewById(R.id.v1);
vd2=(VideoView) findViewById(R.id.v2);
vd1.setVideoURI(Uri.parse("/mnt/sdcard/file.mp4"));
vd1.setMediaController(new MediaController(this));
vd1.requestFocus();
vd1.start();
vd2.setVideoURI(Uri.parse("/mnt/sdcard/android.mp4"));
vd2.setMediaController(new MediaController(this));
vd2.requestFocus();
vd2.start();
}
}
Your xml code should be like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<VideoView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:id="#+id/v1"/>
<VideoView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:id="#+id/v2"/>
</LinearLayout>
May this will help you.
It's a simple app I've got and I'd like the button I've made to launch a specific URL via the browser. Could you guys give me a little info to get this going, like I've said I've got the button already to go in my app. Here's the code -- lemme' know if you need anything else
.java File
package reseeveBeta.mpi.dcasey;
import android.app.Activity;
import android.os.Bundle;
public class ReseeveBetaActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
.XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Welcome to Reseeve, tap register to begin account creation" />
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Register" />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textMultiLine"
android:text="If you already have and account, please login below" >
<requestFocus />
</EditText>
</LinearLayout>
This line should open your built-in browser, with the specified url:
startActivity(new Intent(Intent.ACTION_VIEW).setData(Uri.parse("http://www.google.com")));
Your Activity should have parts like this:
//define class variables here
Button btn;
protected void onCreate(Bundle savedInstanceState)
{
//some code of yours
btn=(Button)findViewById(R.id.button1);
btn.setOnClickListener(this);
//more code of yours
}
//whatever else you have in your source code
public void onClick(View v)
{
//handle the click events here, in this case open www.google.com with the default browser
startActivity(new Intent(Intent.ACTION_VIEW).setData(Uri.parse("http://www.google.com")));
}
It might not be 100% accurate syntax, since I did just write this on my own, but you get the idea.
You can do that with Rebol 3, this easily:
REBOL []
load-gui
view [button "Go" on-action [browse http://msn.com]]
That's a fully functioning GUI program, which runs on Android AND on desktop, using the exact same code across all platforms. Take a look at:
http://rebolforum.com/index.cgi?f=printtopic&permalink=Nick25-Aug-2013/10:08:38-7:00&archiveflag=new
Simple create one WebView in xml
<WebView
android:id="#+id/web_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0" />
Here is the Simple java code for that
String URL="www.gtumca.co.cc";
WebView wv=(WebView)findViewById(R.layout.web_view);
onClick()
{
wv.loadUrl(URL);
}
I've been banging my head against a wall this afternoon trying to get a WebView to work. Below is the code in the main class:
public class fkyougoogle extends Activity {
/** Called when the activity is first created. */
WebView webview;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
// WORKS
//webview.loadUrl("http://www.google.com");
// DOESN'T WORK
//webview.loadUrl("http://www.theregister.co.uk");
//webview.loadData("<html><body>hello</body></html>", "text/html", "utf-8");
//webview.loadDataWithBaseURL("fake://", "<html><body>hello</body></html>", "text/html", "utf-8", "http://www.theregister.co.uk/");
}
}
This is Google's "Hello, Webview" example. If I use a WebView and try to access www.google.com then it works fine. If I try to access any other site then it fails including loadData and it just displays a black screen in the emulator. In the end I would like to read from a local file.
is included under the manifest tag and the XML schema is the same as the Hello Webview example.
Am I missing something obvious here? :(
Try changing
android:layout_width="wrap_content"
android:layout_height="wrap_content"
to
android:layout_width="fill_parent"
android:layout_height="fill_parent"
in your main.xml top level LinearLayout
It should look like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<WebView
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
Try UTF-8 instead of utf-8 for your latter two attempts. I have no problem loading http://www.theregister.co.uk using the same code -- try loading it in the built-in Browser app, and if that fails, you're perhaps encountering some sort of firewall/proxy issue.
Here are a few projects demonstrating simple uses of WebView, from one of my books.
I had similar problem of a WebView being completely blank, but it my case it was caused by a missing android.permission.INTERNET uses-permission.