Java. Lang. illegalargumentexception: The surface has been released

Source: Internet
Author: User

Function: enter an activity and use the mediaplayer and surfaceview classes to play the video. When the activity is started, the mediaplayer is initialized directly. start (), instead of starting through a listener or something, the following error occurs: The surface has been released.

Error location: mediaplayer. setdisplay (surfaceview. gethost ());

Cause: the mediaplayer object is not ready.

Why? Think about it. If you make a listener, the video will not be played directly. If he has enough time to prepare, there will be no errors.

Solution: write a program using a scheduledexecutorservice and handler class. Not much code. [Mw_shl_code = Java, true] import Android. View. surfaceview;


Public class playactivity extends activity {


Private Static string path, endname;



Private surfaceholder msurfaceholder;

Private surfaceview msurfaceview;

Private mediaplayer mmediaplayer;



Private scheduledexecutorservice executorservice;



Private handler;



@ Override

Public void oncreate (bundle savedinstancestate ){

Super. oncreate (savedinstancestate );

Setcontentview (R. layout. Play );

Initialize ();

}



Private void initialize (){

Executorservice = executors. newscheduledthreadpool (1 );

Msurfaceview = (surfaceview) findviewbyid (R. Id. videoview01 );

Msurfaceholder = msurfaceview. getholder ();

Msurfaceholder. settype (surfaceholder. surface_type_push_buffers );

Intent I = getintent ();

Path = I. getstringextra ("path ");

Endname = I. getstringextra ("endname ");

Log. I ("ABC", path + "," + endname );

Handler = new handler (new handler. Callback (){

@ Override

Public Boolean handlemessage (Message MSG ){

Initmedia ();

Return false;

}

});

Executorservice. Schedule (New runnable (){

@ Override

Public void run (){

Handler. sendemptymessage (0 );

}

}, 10000, timeunit. microseconds );

}





Private void initmedia (){

Mmediaplayer = new mediaplayer ();

Mmediaplayer. setaudiostreamtype (audiomanager. stream_music );

Mmediaplayer. setdisplay (msurfaceholder );

Try {

Mmediaplayer. setdatasource (PATH );

Mmediaplayer. Prepare ();

} Catch (illegalargumentexception e ){

// Todo auto-generated Catch Block

E. printstacktrace ();

} Catch (illegalstateexception e ){

// Todo auto-generated Catch Block

E. printstacktrace ();

} Catch (ioexception e ){

// Todo auto-generated Catch Block

E. printstacktrace ();

}

Mmediaplayer. Start ();

}

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.