Android uses WebView to browse a webpage with sound or video. After WebView is disabled, the solution does not stop sound or video.

Source: Internet
Author: User

Android uses WebView to browse a webpage with sound or video. After WebView is disabled, the solution does not stop sound or video.

I recently used Eclipse to develop an Android mobile app. In fact, one function is to use the WebView control provided by Android to load Web pages. Development is smooth and browsing is normal. However, a special note is that the webpage is loaded with sound or video. After the Activity that includes WebView is completed. The sound or video is still playing in the background, and the sound does not stop. After opening the Activity again, the sound or video will be repeatedly played.

This problem is absolutely intolerable, so it does not matter if you search for it online. There are more than one author who has encountered this problem. After checking the problem, we found that the problem should belong to the Android bug and it is not easy to modify. For webpages that contain videos, you can solve the problem as follows:

Protected void onPause () {super. onPause (); if (Build. VERSION. SDK_INT> = Build. VERSION_CODES.HONEYCOMB) {webView. onPause (); // pause the video being played on the webpage }}

However, this code is powerless for sound. After searching online for a long time, there are also many solutions, such:

web.pauseTimers();  web.stopLoading();  web.loadData("", "text/html", "utf-8");

Intent i = new Intent("com.android.music.musicservicecommand"); i.putExtra("command", "pause"); mContext.sendBroadcast(i);

web.loadData("about:blank");

The above methods have been tested by the author and are not easy to use.


After many tests, I found that a rule is that before the Activity is finished, what is the page address that is re-loaded by the Activity and what page is displayed after the Activity is opened next time (because the author uses: web. loadData, will it shut down the previously loaded page? Which one will be loaded next time ?). Then I tested it and loaded the Baidu page before closing it. I found that the next time I opened it, the Baidu page was actually displayed. In this case, the error occurs and the current page address is directly reloaded. Isn't it OK. You can try it. The sound or video is stopped. The next time you open the video, the page can be loaded normally. It's really easy.

The solution is as follows:

    @Override    protected void onPause ()    {        m_WebView.reload ();        super.onPause ();    }


M_WebView is the WebView control in the Activity.

Related Article

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.