Code Music Streaming in Background with Android App - android

I have built a website that streams mp3 playlists using javascript and created an android app that uses WebView to embed the site. Unfortunately the music stops when the phone sleeps. Is it possible to code the app to maintain the javascript executing to keep music playing while running in background? I would like it to function similar to Pandora as an example but not sure if this is possible using WebView.

try this, i test this code so keep music playing while running in background, maybe help you:
private WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView)findViewById(R.id.webview);
// Enable javascript
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
#SuppressWarnings("deprecation")
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
final Uri uri = Uri.parse(url);
return customUriHanlder(uri);
}
#RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
final Uri uri = request.getUrl();
return customUriHanlder(uri);
}
private boolean customUriHanlder(final Uri uri) {
Log.i("TAG", "Uri =" + uri);
final String host = uri.getHost();
final String scheme = uri.getScheme();
// you can set your specific condition
if (true) {
// Returning false means that you are going to load this url in the webView itself
return false;
} else {
// Returning true means that you need to handle what to do with the url
// open web page in a Browser
final Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
return true;
}
}
});
// Load the webpage
webView.loadUrl("https://google.com/");
}
#Override
public void onPause() {
super.onPause();
webView.onResume();
}
#Override
public void onResume() {
super.onResume();
webView.onResume();
}
#Override
protected void onDestroy() {
super.onDestroy();
webView.onResume();
}

Related

PDF file inside webview click not working in few devices

I need to load a PDF file within my webview on a button click within it. I have used google docs URL for loading the same. It works perfectly in S8 edge(v9.0) but it's not working in S6 edge(v6.0.1). It is not calling the shouldOverrideUrlLoading method even. Not able to understand what is the exact reason for it.
My code:
private final String googleDocs = "http://docs.google.com/gview?embedded=true&url=";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
if (request.getUrl().toString().endsWith("pdf")) {
String pdfUrl = googleDocs + request.getUrl().toString();
view.loadUrl(pdfUrl);
} else {
view.loadUrl(request.getUrl().toString());
}
return true;
}
});
WebSettings webSettings = binding.webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.loadUrl(url);
}

WebView's onPause() not Working

I have WebView inside an activity that extends AppCompatActivity, I'm trying to stop Audio playback inside webview when the Activity is closed/swapped but it's not working, the sound keeps playing all the time, below in my code
public class PostDetailsActivity extends AppCompatActivity{
WebView mArticleText;
...
void loadDatatoWebView(String html){
String base64 = Base64.encodeToString(html.getBytes(), Base64.DEFAULT);
mArticleText.loadData(base64, "text/html; charset=utf-8", "base64");
mArticleText.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url != null) {
// open links in external browser
view.getContext().startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
} else {
return false;
}
}
});
}
#Override
protected void onPause() {
super.onPause();
mArticleText.pauseTimers();
mArticleText.onPause();
}
#Override
protected void onResume() {
super.onResume();
mArticleText.onResume();
}
}
I tried adding WebView.loadUrl("about:blank")and it solved the playback problem but introduced some UX problems, the webview had to load again when the activity is resumed ant causes an annoying delay as rendering could take some time.

play webview RTMP stream in android

i am a new to android development so dont be harsh
I am creating an android application that contains a webview. The webview has many links to live stream.
After reading various posts, i understand that it is really difficult and complicated (as i have tried) to get the video played in webview itself as webview doesn't have/ support many features for video streaming
My question is: HOW DO I OPEN UP MXPLAYER AND PLAY THE STREAM WHENEVER THE USER SELECTS A SPECIFIC LINK IN THE WEBVIEW?
I am just testing this on my own device so want to keep it specific to mx player as i already have that installed
I have tried the following:
public class WebPageLoader extends Activity {
final Activity activity = this;
WebView webView;
private String pknamepro;
String homePage = "";
String specificWebURL = "";
public WebPageLoader(){
pknamepro = "com.mxtech.videoplayer.pro";
}
private void startMx(String url)
{
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setComponent(new ComponentName("com.mxtech.videoplayer.pro", "com.mxtech.videoplayer.pro.ActivityScreen"));
intent.setData(Uri.parse(url));
intent.putExtra("secure_uri", true);
startActivity(intent);
return;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webpageloader);
webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setSupportZoom(false);
webView.getSettings().setBuiltInZoomControls(false);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.setScrollbarFadingEnabled(true);
webView.getSettings().setLoadsImagesAutomatically(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDatabaseEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
activity.setTitle("Loading...");
activity.setProgress(progress * 100);
if(progress == 100)
activity.setTitle(R.string.app_name);
}
});
webView.setWebViewClient(new WebViewClient() {
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
Toast.makeText(getApplicationContext(), "Internet connection down" + description, Toast.LENGTH_SHORT).show();
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
if (url.contains("rtmp")|| url.contains(specificWebURL) || url.startsWith("rtmpe")|| url.startsWith("udp")) {
startMx(url);
return true;
}
view.loadUrl(url);
return true;
}
});
webView.loadUrl(homePage);
}
protected void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
// Save the state of the WebView
webView.saveState(outState);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
// Restore the state of the WebView
webView.restoreState(savedInstanceState);
}
public void onBackPressed() {
if(webView.canGoBack() == true){
webView.goBack();
}else{
webView.goBack();
}
}
As you can see, i have tried it by checking the url as well as tried to hard code the url but neither work.
It seems as if it doesn't do any of the above checks and tries to play the video in webview it self because whenever i click on any of the links, a new page opens saying "Error loading player: No playable sources found" instead of opening mxplayer on my device.
In case someone else has the same question as mine, below is my solution that is working for me.
Intent myIntent;
PackageManager pm = getPackageManager();
try{
myIntent = pm.getLaunchIntentForPackage("com.mxtech.videoplayer.pro");
myIntent.setComponent(new ComponentName("com.mxtech.videoplayer.pro", "com.mxtech.videoplayer.ActivityScreen"));
myIntent.setDataAndType(Uri.parse(url), "application/x-mpegURL");
myIntent.putExtra("secure_uri", true);
myIntent.putExtra(EXTRA_DECODE_MODE, (byte)2);
if (null != myIntent)
this.startActivity(myIntent);
}
catch (ActivityNotFoundException e)
{
Toast.makeText(getApplicationContext(),
" stream not working ",
Toast.LENGTH_LONG).show();
}

Can't play this video error in android WebView using RTSP stream

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);
}
});

webview to open sites of certain domain

I'm trying to make an app that opens all web pages of a certain site, for example www.yahoo.com, in the webview, but all other web pages in the defaultbrowser. Here is the code that I am using but can not quite get it work.
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("http://www.yahoo.com")) {
// This is my web site, so do not override; let my WebView load
// the page
return false;
}
// 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;
}
}
It looks to me that this code should work, but when i load yahoo it still goes to an outside browser. Any help would be appreciated. Thanks!
Here is a different way to do it.
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
String urlHost = Uri.parse(url).getHost();
switch (urlHost) {
case "yahoo.com":
return false;
case "www.yahoo.com":
return false;
case "m.yahoo.com":
return false;
default:
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
getHost() usually returns just the domain name, or sometimes with www. prefixed to it. It never contains an http:// protocol AFAIK. Try using:
if ((Uri.parse(url).getHost().equals("yahoo.com")) || (Uri.parse(url).getHost().equals("www.yahoo.com")) || (Uri.parse(url).getHost().equals("m.yahoo.com"))) {
//Your code
Try this it should help you
private WebView mWebview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_yahoo);
mWebview = new WebView(this);
mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript
final Activity activity = this;
mWebview.setWebViewClient(new WebViewClient() {
#SuppressWarnings("deprecation")
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
}
#TargetApi(android.os.Build.VERSION_CODES.M)
#Override
public void onReceivedError(WebView view, WebResourceRequest req, WebResourceError rerr) {
// Redirect to deprecated method, so you can use it in all SDK versions
onReceivedError(view, rerr.getErrorCode(), rerr.getDescription().toString(), req.getUrl().toString());
}
});
mWebview .loadUrl("http://www.yahoo.com");
setContentView(mWebview );
}
}

Categories

Resources