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.