Verifying mediaplayer's reset function through an instance does not block the main thread

Source: Internet
Author: User

For example, in the following example, you can click to play the next song frequently to verify whether the reset function of mediaplayer is blocked.

The verification data is as follows: in the 32 seconds from 20:26:04. 673 to 20:26:36. 168, clicking 170 next to play the next song has been successful. The ANR problem caused by the main thread blocking has not occurred.

In this example, all local songs are played, so no blocking occurs. If an online song is played, it needs to be exchanged with the network and played frequently. ANR may occur. To play a online song, you must use the asynchronous prepareasync function instead of synchronous prepare, to avoid blocking.

(1) The log for the first playback is as follows:

06-24 20:26:04. 673 26426 26426 D mainactivity :-------------------------------
06-24 20:26:04. 678 26426 26426 D mainactivity: play next Music
06-24 20:26:04. 678 26426 26426 D mainactivity: Before Reset
06-24 20:26:04. 683 26426 26426 D mainactivity: After Reset
06-24 20:26:04. 683 26426 26426 D mainactivity: Before setdatasource:/mnt/sdcard/music/Bai 'an/MAI Tian catcher-bai 'an
06-24 20:26:04. 763 26426 26426 D mainactivity: After setdatasource to prepare
06-24 20:26:04. 768 26426 26426 D mainactivity: onprepared
06-24 20:26:04. 898 26426 26426 D mainactivity :-------------------------------

(2) The last 6 playback logs are as follows:

06-24 20:26:35. 013 26426 26426 D mainactivity :-------------------------------
06-24 20:26:35. 013 26426 26426 D mainactivity: play next Music
06-24 20:26:35. 013 26426 26426 D mainactivity: Before Reset
06-24 20:26:35. 018 26426 26426 D mainactivity: After Reset
06-24 20:26:35. 018 26426 26426 D mainactivity: Before setdatasource:/mnt/sdcard/music/download/rest of life together-Liu Dehua. AAC
06-24 20:26:35. 068 26426 26426 D mainactivity: After setdatasource to prepare
06-24 20:26:35. 073 26426 26426 D mainactivity: onprepared
06-24 20:26:35. 213 26426 26426 D mainactivity :-------------------------------
06-24 20:26:35. 213 26426 26426 D mainactivity: play next Music
06-24 20:26:35. 213 26426 26426 D mainactivity: Before Reset
06-24 20:26:35. 218 26426 26426 D mainactivity: After Reset
06-24 20:26:35. 218 26426 26426 D mainactivity: Before setdatasource:/mnt/sdcard/music/Bai 'an/MAI Tian catcher-bai 'an
06-24 20:26:35. 298 26426 26426 D mainactivity: After setdatasource to prepare
06-24 20:26:35. 308 26426 26426 D mainactivity: onprepared
06-24 20:26:35. 403 26426 26426 D mainactivity :-------------------------------
06-24 20:26:35. 403 26426 26426 D mainactivity: play next Music
06-24 20:26:35. 403 26426 26426 D mainactivity: Before Reset
06-24 20:26:35. 403 26426 26426 D mainactivity: After Reset
06-24 20:26:35. 403 26426 26426 D mainactivity: Before setdatasource:/mnt/sdcard/music/download/Happy birthday -liu ruoying
06-24 20:26:35. 463 26426 26426 D mainactivity: After setdatasource to prepare
06-24 20:26:35. 463 26426 26426 D mainactivity: onprepared
06-24 20:26:35. 593 26426 26426 D mainactivity :-------------------------------
06-24 20:26:35. 593 26426 26426 D mainactivity: play next Music
06-24 20:26:35. 593 26426 26426 D mainactivity: Before Reset
06-24 20:26:35. 598 26426 26426 D mainactivity: After Reset
06-24 20:26:35. 598 26426 26426 D mainactivity: Before setdatasource:/mnt/sdcard/music/download/stay with Liu Dehua. AAC
06-24 20:26:35. 678 26426 26426 D mainactivity: After setdatasource to prepare
06-24 20:26:35. 683 26426 26426 D mainactivity: onprepared
06-24 20:26:35. 983 26426 26426 D mainactivity :-------------------------------
06-24 20:26:35. 983 26426 26426 D mainactivity: play next Music
06-24 20:26:35. 983 26426 26426 D mainactivity: Before Reset
06-24 20:26:35. 983 26426 26426 D mainactivity: After Reset
06-24 20:26:35. 983 26426 26426 D mainactivity: Before setdatasource:/mnt/sdcard/music/Bai 'an/MAI Tian catcher-bai 'an
06-24 20:26:36. 068 26426 26426 D mainactivity: After setdatasource to prepare
06-24 20:26:36. 073 26426 26426 D mainactivity: onprepared
06-24 20:26:36. 168 26426 26426 D mainactivity :-------------------------------
06-24 20:26:36. 168 26426 26426 D mainactivity: play next Music
06-24 20:26:36. 168 26426 26426 D mainactivity: Before Reset
06-24 20:26:36. 173 26426 26426 D mainactivity: After Reset
06-24 20:26:36. 173 26426 26426 D mainactivity: Before setdatasource:/mnt/sdcard/music/download/Happy birthday -liu ruoying
06-24 20:26:36. 238 26426 26426 D mainactivity: After setdatasource to prepare
06-24 20:26:36. 243 26426 26426 D mainactivity: onprepared

(3) The logs before and after reset execution are as follows. The reset will be executed within a very short period of time, generally within 10 milliseconds.

20:26:04-24. 678 26426 26426 D mainactivity: Before Reset
20:26:04-24. 683 26426 D mainactivity: After Reset
20:26:04 am-24. 898 26426 D mainactivity: Before Reset
20:26:04 am-24. 898 26426 D mainactivity: After Reset
20:26:05 am-24. 063 26426 26426 D mainactivity: Before Reset
20:26:05-24. 068 26426 26426 D mainactivity: After Reset
24:06-24 20:26:05. 243 26426 26426 D mainactivity: Before Reset
25:06-24 20:26:05. 243 26426 26426 D mainactivity: After Reset
31: 06-24 20:26:05. 578 26426 26426 D mainactivity: Before Reset
32: 06-24 20:26:05. 583 26426 26426 D mainactivity: After Reset
38: 06-24 20:26:05. 758 26426 26426 D mainactivity: Before Reset
39: 06-24 20:26:05. 763 26426 26426 D mainactivity: After Reset
45: 06-24 20:26:05. 918 26426 26426 D mainactivity: Before Reset
46: 06-24 20:26:05. 918 26426 26426 D mainactivity: After Reset
52: 06-24 20:26:06. 098 26426 26426 D mainactivity: Before Reset
53: 06-24 20:26:06. 098 26426 26426 D mainactivity: After Reset
......

1144: 06-24 20:26:34. 838 26426 26426 D mainactivity: Before Reset
1145: 06-24 20:26:34. 838 26426 26426 D mainactivity: After Reset
1151: 06-24 20:26:35. 013 26426 26426 D mainactivity: Before Reset
1152: 06-24 20:26:35. 018 26426 26426 D mainactivity: After Reset
1158: 06-24 20:26:35. 213 26426 26426 D mainactivity: Before Reset
1159: 06-24 20:26:35. 218 26426 26426 D mainactivity: After Reset
1165: 06-24 20:26:35. 403 26426 26426 D mainactivity: Before Reset
1166: 06-24 20:26:35. 403 26426 26426 D mainactivity: After Reset
1172: 06-24 20:26:35. 593 26426 26426 D mainactivity: Before Reset
1173: 06-24 20:26:35. 598 26426 26426 D mainactivity: After Reset
1179: 06-24 20:26:35. 983 26426 26426 D mainactivity: Before Reset
1180: 06-24 20:26:35. 983 26426 26426 D mainactivity: After Reset
1186: 06-24 20:26:36. 168 26426 26426 D mainactivity: Before Reset
1187: 06-24 20:26:36. 173 26426 26426 D mainactivity: After Reset

(4) The verification code is as follows:

Public class mainactivity extends activity implements view. onclicklistener {Private Static final string tag = "mainactivity"; private mediaplayer mmediaplayer = NULL; private button mbtnplaystop; private button mbtnnext; private Boolean misplayflag = false; private string mpatharray [] = new string [] {"/mnt/sdcard/music/Bai an/MAI Tian catcher -bai 'an", "/mnt/sdcard/music/download/Happy birthday -liu ruoying ", "/mnt/sdcard/music/ Download/spend the rest of your life together-Andy Lau. AAC "}; private int mindex = 0; Public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); mbtnplaystop = (button) findviewbyid (R. id. btnplaystop); mbtnplaystop. setonclicklistener (this); mbtnnext = (button) findviewbyid (R. id. btnnext); mbtnnext. setonclicklistener (this); setvolumecontrolstream (audiomanager. stream_musi C); mmediaplayer = new mediaplayer (); mmediaplayer. setonpreparedlistener (New onpreparedlistener () {@ override public void onprepared (mediaplayer arg0) {log. D (TAG, "onprepared"); mmediaplayer. start () ;}}); mmediaplayer. setoncompletionlistener (New oncompletionlistener () {@ override public void oncompletion (mediaplayer MP) {log. D (TAG, "oncompletion"); next () ;}}); mmediaplayer. setonerrorlistener (New onerrorlistener () {@ override public Boolean onerror (mediaplayer MP, int what, int extra) {log. D (TAG, "onerror what:" + What + ", extra:" + extra); Return false ;}) ;}@ override public void onclick (view V) {Switch (v. GETID () {case R. id. btnplaystop: If (misplayflag) {try {log. D (TAG, "-------------------------------"); log. D (TAG, "Play:" + mpatharray [mindex]); log. D (TAG, "Before RESET "); Mmediaplayer. reset (); log. D (TAG, "after reset"); log. D (TAG, "Before setdatasource"); mmediaplayer. setdatasource (mpatharray [mindex]); log. D (TAG, "After setdatasource to prepare"); mmediaplayer. prepare ();} catch (exception e) {e. printstacktrace ();} mbtnplaystop. settext ("stop");} else {log. D (TAG, "-------------------------------"); log. D (TAG, "stop" + mpatharray [mindex]); mmediaplayer. stop (); MBT Nplaystop. settext ("play");} misplayflag =! Misplayflag; break; case R. id. btnnext: log. D (TAG, "-------------------------------"); log. D (TAG, "Play next Music"); next (); break ;}} private void next () {try {log. D (TAG, "Before RESET"); mmediaplayer. reset (); log. D (TAG, "after reset"); If (mindex <mpatharray. length-1) {mindex ++;} else {mindex = 0;} log. D (TAG, "Before setdatasource:" + mpatharray [mindex]); mmediaplayer. setdatasource (mpatharray [mindex]); log. D (TAG, "After setdatasource to prepare"); mmediaplayer. prepare ();} catch (exception e) {e. printstacktrace ();}}}

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.