Media Player Custom Interface

Source: Internet
Author: User

public class Videoactivity extends Activity implements callback{
Private Surfaceview view;
Private Surfaceholder holder;
Private MediaPlayer MPlayer;

Private String path;

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.video_main);

Initwidgets ();
}

private void Initwidgets () {
view= (Surfaceview) Findviewbyid (R.id.surfaceview);

Holder=view.getholder ();
Holder.setfixedsize (320, 480);
Holder.setkeepscreenon (TRUE);
Setting the listener for callbacks
Holder.addcallback (this);

Path=getintent (). Getstringextra ("path");
}

@Override
public void surfacechanged (Surfaceholder arg0, int arg1, int arg2, int arg3) {

}

@Override
public void surfacecreated (Surfaceholder arg0) {
Mplayer=new MediaPlayer ();
try {
Mplayer.setdatasource (path);
Mplayer.setaudiostreamtype (Audiomanager.stream_music);
Mplayer.setdisplay (holder);

Mplayer.prepare ();
} catch (IllegalArgumentException e) {
E.printstacktrace ();
} catch (SecurityException e) {
E.printstacktrace ();
} catch (IllegalStateException e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
}
}

@Override
public void surfacedestroyed (Surfaceholder arg0) {
Mplayer.release ();
}

public void Play (View v) {
if (!mplayer.isplaying ()) {
Mplayer.start ();
}
}
}

public class Mainactivity extends Activity implements Surfaceholder.callback {//2. Implementation callback Surfaceholder.callback
Private Videoview Videoview;

The second type of interface
Private Surfaceview view;
Private Surfaceholder holder;

Private MediaPlayer MPlayer;

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.vedio_main);

String path = environment.getexternalstoragedirectory () + "/move.mp4";
Initializing the Video interface
Videoview = (videoview) Findviewbyid (R.id.video);
//
Load a video source into the video interface
Videoview.setvideopath (path);
//
Mediacontroller controller = new Mediacontroller (this);//control Center
Controller.setmediaplayer (Videoview);//control the playback interface with controller
//
Videoview.setmediacontroller (Controller);//interface and control Center binding
Videoview.requestfocus ();//Set video has focus

Custom interface: Video player, Surfaceview is a dedicated interface container
View = (Surfaceview) Findviewbyid (R.id.view);

1. Bind the interface and set the parameters
Holder = View.getholder ();//Instantiate the video holder object, at which time the parameters are set by the holder
Holder.setfixedsize (320, 480);//video resolution, do not write is the default
Holder.setkeepscreenon (TRUE);//the screen remains open

Setting the listener for callbacks
Holder.addcallback (this);
}

@Override
public void surfacechanged (surfaceholder holder, int format, int width,
int height) {//when the interface changes

}

@Override
public void surfacecreated (Surfaceholder holder) {//when creating the interface
MPlayer = new MediaPlayer ();
try {
cursor cursor = getcontentresolver (). Query (MediaStore.Video.Media.EXTERNAL_CONTENT_URI, null,null,null,null);
while (Cursor.movetonext ()) {
LOG.I ("Path", Cursor.getstring (Cursor.getcolumnindex (Media.data)));
//}

Mplayer.setdatasource (Environment.getexternalstoragedirectory () + "/move.mp4");
Mplayer.setaudiostreamtype (audiomanager.stream_music);//audio Source Type
Mplayer.setdisplay (holder);//load the custom interface set up
Mplayer.prepare ();//Preparation
} catch (Exception e) {
E.printstacktrace ();
}
}

@Override
public void surfacedestroyed (Surfaceholder holder) {//Destroy interface
if (mplayer.isplaying ()) {
Mplayer.stop ();
Mplayer.release ();
}
}

Play button
public void Play (View v) {
if (!mplayer.isplaying ()) {
Mplayer.start ();
}
}
}

Media Player Custom Interface

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.