本文介绍了之后用户presses的Andr​​oid的WebView停不回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我玩的WebView内YouTube视频。我能够发挥它,但是当人离开屏幕,我无法停止播放音频。

I am playing a YouTube video inside a WebView. I am able to play it, but when the person leaves the screen, I am not able to stop the audio from playing.

我试过里面的onDestroy和的onPause各种事情,或者把他们从完全,但他们似乎并没有发挥作用。

I tried various things inside onDestroy and onPause, or taking them out entirely, but they didn't seem to make a difference.

会有人知道为什么音频继续播放和应用程序被关闭后,即使没有停止,用户打开其他应用程序?

Would anyone know why the audio keeps playing and doesn't stop even after the app is turned off and the user opens other apps?

下面是我的code全班:

Here is my code for the whole class:

import com.flurry.android.FlurryAgent;

import utils.SendEmail;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebSettings.PluginState;
import android.widget.Button;

public class YoutubeActivity extends Activity
{
    WebView webview = null;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        webview = new WebView(this);

            setContentView(webview);

            webview.getSettings().setAppCacheEnabled(false);
            webview.getSettings().setJavaScriptEnabled(true);
            webview.setInitialScale(1);
            webview.getSettings().setPluginState(PluginState.ON);

            WebSettings webSettings = webview.getSettings();

            webSettings.setLoadsImagesAutomatically(true);
            webSettings.setLoadWithOverviewMode(true);
            webSettings.setBuiltInZoomControls(true);        
            webSettings.setUseWideViewPort(true);

            webview.setWebViewClient(new WebViewClient() {
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    view.loadUrl(url);              
                    return false;
                }
            });        


            webview.setWebChromeClient(new WebChromeClient(){});

            webSettings.setDomStorageEnabled(true);
            webSettings.setAppCacheEnabled(true);
                        webSettings.setAppCachePath(getApplicationContext().getFilesDir().getAbsolutePath() + "/cache");            

            webSettings.setDatabaseEnabled(true);
            webSettings.setDatabasePath(getApplicationContext().getFilesDir().getAbsolutePath() + "/databases");

            SharedPreferences prefs = 
                    PreferenceManager.getDefaultSharedPreferences( YoutubeActivity.this);
            String url_to_watch = prefs.getString( "url_to_watch" , null ); 


            webview.loadUrl(url_to_watch);
    }



    @Override
    public void onPause()
    {
       super.onPause();

       try
       {
           if ( webview != null )
           {
               webview.clearCache(true); 
               webview.getSettings().setAppCacheEnabled(false);
               webview.stopLoading();
               webview.destroy();           
               sendEmail ("in pause " , "");
               webview = new WebView(this);
           }

           this.finish();
       }
       catch ( Exception e )
       {

       }
    }


    @Override
    public void onDestroy()
    {
       super.onDestroy();

       try
       {
           if ( webview != null )
           {
               webview = new WebView(this); // To try to reset the webview - didn't work.
           }
       }
       catch ( Exception e )
       {

       }    
    }

    @Override
    public void onStop()
    {
       super.onStop();

       FlurryAgent.onEndSession(this);

       try
       {
           if ( webview != null )
           {
               webview.clearView();
               webview.getSettings().setAppCacheEnabled(false);
               webview.stopLoading();
               webview.destroy();

               sendEmail ("in stop " , "");
           }
       }
       catch ( Exception e )
       {

       }

       this.finish();
    }   

    @Override
    protected void onResume() 
    {
        super.onResume();

        try
        {
            webview.onResume();
        }
        catch ( Exception  e )
        {

        }
    }

    @Override
    protected void onRestart() 
    {
        super.onRestart();
    }    


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) 
    {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) 
        {
            webview.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }   

    // Subject , body
    public void sendEmail( String subject , String body )
    {
        String[] params = new String[] { "http://www.problemio.com/problems/send_email_mobile.php", subject, body };

        SendEmail task = new SendEmail();
        task.execute(params);               
    }  


    //@Override 
    public void onPageFinished(WebView view, String url) 
    { 
        //super.onPageFinished(view, url); 
        view.clearCache(true); 
    }

    public void onBackPressed ( )
    {   
        final AlertDialog.Builder linkedin_builder = new AlertDialog.Builder(this);

             linkedin_builder.setMessage("" +
                    "Go back to the home screen?")
                .setCancelable(false)            
                .setNegativeButton("YES", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) 
                    {
                        if ( webview != null )
                        {
                            webview.destroy();
                        }

                        Intent myIntent = new Intent(YoutubeActivity.this, MainActivity.class);
                        YoutubeActivity.this.startActivity(myIntent);
                    }
                })              
                .setPositiveButton("NO", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) 
                    {
                        dialog.cancel();                  
                    }
                })
;
         AlertDialog alert = linkedin_builder.create();
         alert.show();      
    }


    @Override
    protected void onStart()
    {
        super.onStart();
        FlurryAgent.onStartSession(this, "4VYNFK3V6RCZ53CZ3J32");
    }    
}

在什么可能停止播放音频,什么可能导致它有什么想法?
谢谢!

Any thoughts on what might stop the audio from playing and what may be causing it?Thank you!

推荐答案

您应该拨打:

webView.loadUrl("about:blank");

这会破坏所有的音频/视频以及Javasript对象和停止的WebView所有正在运行的功能。

It will destroy all audio/video as well as Javasript objects and stop all running functions on webview

这篇关于之后用户presses的Andr​​oid的WebView停不回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 13:03