Author: Wuhua
Space: htt: // wuhua.3geye.net
Reprinted please keep the above information (please respect knowledge products) Thank you
Package com. ucweb. player;
Import java. Io. ioexception;
Import Android. App. activity;
Import Android. content. context;
Import Android. Graphics. pixelformat;
Import Android. Media. mediaplayer;
Import Android. Media. mediaplayer. onbufferingupdatelistener;
Import Android. Media. mediaplayer. oncompletionlistener;
Import Android. Media. mediaplayer. onerrorlistener;
Import Android. OS. Bundle;
Import Android. util. log;
Import Android. View. Menu;
Import Android. View. surface;
Import Android. View. surfaceholder;
Import Android. View. surfaceview;
Import Android. View. window;
Import Android. View. Menu. item;
Public class videoplayer extends activity implements onerrorlistener,
Onbufferingupdatelistener, oncompletionlistener
{
Private Static final string tag = "playvideo ";
Private Static final int play_video = menu. First + 1;
Private Static final int play_mp3 = menu. First + 2;
Private Static final int stop_play_id = menu. First + 4;
Private mediaplayer MP;
Private preview mpreview;
/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle icicle)
{
Super. oncreate (icicle );
Log. V (TAG, "oncreate: ==> ");
Requestwindowfeature (window. feature_no_title );
Getwindow (). setformat (pixelformat. Transparent );
Mpreview = new Preview (this );
Setcontentview (mpreview );
}
Private void playvideo ()
{
Try
{
Surfaceview view = new surfaceview (this );
Surface surface = view. getholder (). getsurface ();
Setcontentview (View );
Log. V (TAG, "height:" + mpreview. getheight ());
Log. V (TAG, "width:" + mpreview. getwidth ());
MP = mediaplayer. Create (this, r.raw.mp3 );
MP. setonerrorlistener (this );
MP. setdisplay (mpreview. getholder (). getsurface ());
MP. Prepare ();
Log. V (TAG, "Duration: ==>" + MP. getduration ());
MP. Start ();
}
Catch (exception E)
{
// E. printstacktrace ();
Log. E (TAG, "error:" + E. getmessage ());
Stopplay ();
}
}
Private void playmp3 () throws ioexception
{
MP = mediaplayer. Create (this, r.raw.mp3 );
MP. Prepare ();
Log. V (TAG, "Duration: ==>" + MP. getduration ());
MP. Start ();
}
Private void stopplay (){
If (MP! = NULL ){
MP. Stop ();
MP. Release ();
}
}
@ Override
Public Boolean oncreateoptionsmenu (menu)
{
Boolean result = super. oncreateoptionsmenu (menu );
Menu. Add (0, play_mp3, "MP3 ");
Menu. Add (1, play_video, "MP3 ");
Menu. Add (1, stop_play_id, "MP3 ");
Return result;
}
@ Override
Public Boolean onoptionsitemselected (item)
{
Log. V (TAG, "option:" + item. GETID ());
Switch (item. GETID ()){
Case play_mp3:
Try {
Playmp3 ();
} Catch (exception e ){
}
Break;
Case play_video:
Playvideo ();
Break;
Case stop_play_id:
Stopplay ();
Break;
}
Return super. onoptionsitemselected (item );
}
@ Override
Protected Boolean isfullscreenopaque ()
{
// Our main window is set to translucent, but we know that we
// Will
// Fill it with opaque data. Tell the system that so it can
// Perform
// Some important optimizations.
Return false;
}
@ Override
Protected void onresume (){
// Because the cameradevice object is not a shared resource,
// It's very important to release it when the activity is paused.
Super. onresume ();
// Mpreview. Resume ();
}
@ Override
Protected void onpause (){
// Start preview again when we resume.
Super. onpause ();
// Mpreview. Pause ();
}
Public void onerror (mediaplayer, int what, int extra ){
Log. E (TAG, "onerror ---> what:" + What + "Extra:" + extra );
If (mediaplayer! = NULL ){
Mediaplayer. Stop ();
Mediaplayer. Release ();
}
}
Public void onbufferingupdate (mediaplayer arg0, int percent ){
Log. D (TAG, "onbufferingupdate ---> percent:" + percent );
}
Public void oncompletion (mediaplayer arg0 ){
Log. D (TAG, "oncompletion ---> finished ");
}
}
Class preview extends surfaceview implements surfaceholder. Callback {
Surfaceholder mholder;
Private Boolean mhassurface;
Preview (context ){
Super (context );
Mholder = getholder ();
Mholder. addcallback (this );
Mhassurface = false;
Mholder. setfixedsize (320,240 );
// Mholder. setfixedsize (192,242 );
}
Public void surfacecreated (surfaceholder holder ){
// The surface has been created, start our main acquisition thread.
Mhassurface = true;
// Return true;
}
Public void surfacedestroyed (surfaceholder holder ){
// Surface will be destroyed when we return. Stop the preview.
Mhassurface = false;
}
Public void surfacechanged (surfaceholder holder, int format, int W,
Int h ){
// Surface size or format has changed. This shocould not happen in
// This
// Example.
}}
I am still exploring this thing. I don't know if he has provided streaming media support. If you can directly support streaming media. That's great. In this way, you can use Java to write streaming media. Continue Study