Android uses WebView to browse a Web page with sound or video, after turning off webview, sound or video does not stop the solution

Source: Internet
Author: User
Tags blank page

The author recently developed the Android mobile app app using Eclipse, and there's actually a feature to load the Web page with the WebView control that comes with the Android system. Development is smooth and browsing is normal. However, one of the more special is that there is a sound or video in the loaded Web page. After the activity that includes WebView is finished. The sound or video is still playing in the background and the sound does not stop. Once the activity is opened again, the sound or video will be played repeatedly.

This problem is absolutely not tolerable, so the Internet search, this search does not matter, the original encountered this problem of netizens more than the author alone. After a few checks found that the original problem should be attributed to the Android bug, not very good modification. For Web pages that contain video, you can do this in the following ways:

protected void OnPause () {    super.onpause ();     if (Build.VERSION.SDK_INT >= build.version_codes. Honeycomb) {         //  pause the video being played in the webpage     }}

But this code is powerless for sound. Search on the internet for a long time, there are many solutions, such as:

 web.pausetimers ();  Web.stoploading (); Web.loaddata (  <a></a>  , "   text/html  , "  utf-8  ); 
New Intent ("com.android.music.musicservicecommand"); I.putextra ("command""pause"); Mcontext.sendbroadcast (i);
Web.loaddata ("about:blank");

The above method, after the author test, are not easy to use.

Later, after many tests, found a rule, that is, before the finish, the activity is re-loaded page address is what, the next time after opening, also show what page (because the author uses: Web.loaddata ("About:blank"); Method Test, Found the next time you open, the display is a blank page, began to think that the page did not load successfully, and then inadvertently brainwave, will not be closed before loading the page, the next time you open or load Which? )。 Later tested, close before loading a bit Baidu page, found the next time it opened, it is really displayed Baidu Baidu page. Then will wrong, directly reload the current page address, not OK. A try, really can. The sound or video stops, and the next time you open it, the page will load normally. It's all a waste of effort.

The solution is as follows:

@Override protected void OnPause () {    m_webview.reload ();    Super.onpause ();}

M_webview is the WebView control in the activity.

Android uses WebView to browse a Web page with sound or video, after turning off webview, sound or video does not stop the solution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.