Reputation: 119
I have an Android application with 4 tabs and I am trying to load a different webview into each tab. One of the webviews contains an audio stream. I am having 2 problems:
The Audio stream page will not load. The fragment loads to a blank white page for the audio stream webview.
ListenLiveFragment.java
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment`
View v= inflater.inflate(R.layout.fragment_listen_live, container, false);
web_v=(WebView)v.findViewById(R.id.webView);
WebSettings web_sett=web_v.getSettings();
web_sett.setJavaScriptEnabled(true);
web_v.setWebViewClient(new MyWebClient());
web_v.loadUrl("http://stream.us.gslb.liquidcompass.net/WPWXFMMP3");
return inflater.inflate(R.layout.fragment_listen_live, container, false);
//return v;
}
private class MyWebClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
web_v.loadUrl(url);
return true;
}
}
fragment_listen_live.xml
<FrameLayout 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"
tools:context="com.peekatu.maintemplate.ListenLiveFragment">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/webView"
android:layout_gravity="center" />
</FrameLayout>
HTML
<html>
<head>
<meta name="viewport" content="width=device-width">
</head>
<body>
<video controls="" autoplay="" name="media">
<source src="http://stream.us.gslb.liquidcompass.net/WPWXFMMP3" type="audio/mpeg">
</video>
</body>
</html>
2.My 2nd question is how do I stop the webviews from reloading every time the fragment is paused. Thank you.
Upvotes: 0
Views: 1435
Reputation: 1192
You can quickly try with
MediaPlayer mp = new MediaPlayer();
mp.setDataSource(URL_OF_FILE);
mp.prepare();
mp.start();
Or
MediaPlayer mediaPlayer = MediaPlayer.create(this, Uri.parse("SOURCE URL"));
mediaPlayer.start();
You have a better implementation here which loads the content in background without blocking the UI.
Streaming Audio from A URL in Android using MediaPlayer?
A first approach could be to tell the ViewPager to keep loaded these fragments increasing the size, in this case if you have four tabs
viewPager.setOffscreenPageLimit(4);
Or save and restore the state of each fragment
Upvotes: 1