2013年7月26日星期五

android mediaPlay Play Video Playback

I have a stored route List play four files inside, I want to play these four files have been circulating. Ask how to achieve? ? Best to give a demo, thank you very much!
------ Solution ---------------------------------------- ----
Mediaplayer plus a OnCompletionListener

After playing the next one wants.
------ Solution ---------------------------------------- ----
play the next one before the mediaplayer object stop () && release (), so try it again?
seemingly finished playing a direct setDataSource () is not acceptable.
------ Solution ---------------------------------------- ----
today I was able to achieve this functionality. But I am using VideoView controls do. .
You've inherited OnCompletionListener, Why add listener time, but also their new one yet.
------ Solution ---------------------------------------- ----

you do, then this method will not be implemented, it will only add that execute your listener onCompletion method.
public void onCompletion (MediaPlayer mp) {
/ / TODO Auto-generated method stub
/ / mp.seekTo (0);

mp.start ();
}

------ For reference only ---------------------------------- -----
set up, but even after setting the video can not be played? Help me find code?


package ele.bus.ele;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class ElebusDemActivity extends Activity implements
OnCompletionListener, Callback, OnBufferingUpdateListener,
OnPreparedListener {
/ ** Called when the activity is first created. * /
private SurfaceView sView;
private SurfaceHolder hodler;
private MediaPlayer mediaPlayer;
private int videoWhith;
private int videoHeight;
private List list;
private int index = 0;

@ Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
/ / requestWindowFeature (Window.FEATURE_NO_TITLE);
setContentView (R.layout.main);
mediaPlayer = new MediaPlayer ();
sView = (SurfaceView) findViewById (R.id.surfaceView1);
/ / get SurfaceHolder objects
hodler = sView.getHolder ();
/ / Set callback function
hodler.addCallback (this);
/ / set the style
hodler.setType (SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
list = new ArrayList ();
String url = Environment.getExternalStorageDirectory ()
. getAbsolutePath () + File.separatorChar + "VideosZip";
System.out.println (url);
getFiles (url);
}

private void play (String str) {

String path = Environment.getExternalStorageDirectory (). getPath ()
+ File.separatorChar + "VideosZip1" + File.separator
+ "video.mp4";

/ / build MediaPlayer object
try {
/ / set the media file path
mediaPlayer.setDataSource (path);
System.out.println (str);
/ / set to display through SurfaceView
mediaPlayer.setDisplay (hodler);
/ / prepare
mediaPlayer.prepareAsync ();
/ / set up event listeners
mediaPlayer.start ();
} 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 blockj
e.printStackTrace ();
}
mediaPlayer.setOnBufferingUpdateListener (this);
mediaPlayer.setOnPreparedListener (this);
mediaPlayer.setOnCompletionListener (new OnCompletionListener () {

@ Override
public void onCompletion (MediaPlayer mp) {
/ / TODO Auto-generated method stub
next ();
}
});
mediaPlayer.setAudioStreamType (AudioManager.STREAM_MUSIC);
}

@ Override
public boolean onKeyDown (int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount () == 0) {
event.startTracking ();
finish ();
System.exit (0);
return true;
}

return super.onKeyDown (keyCode, event);
}

public class MyWebViewClient extends WebViewClient {
public boolean shouldOverviewUrlLoading (WebView view, String url) {
view.loadUrl (url);
return true;
}
}

private void next () {
if (+ + index> = list.size ()) {
index = 0;
} else {
play (list.get (index));
System.out.println ("111111111111111");
}
}


public void onCompletion (MediaPlayer mp) {
/ / TODO Auto-generated method stub
/ / mp.seekTo (0);

mp.start ();
}

public void onPrepared (MediaPlayer mp) {
/ / TODO Auto-generated method stub
videoWhith = mediaPlayer.getVideoWidth ();
videoHeight = mediaPlayer.getVideoHeight ();

if (videoWhith! = 0 && videoHeight! = 0) {
/ / Set the video width and height
hodler.setFixedSize (videoWhith, videoHeight);
/ / start playing

}
}

public void onBufferingUpdate (MediaPlayer mp, int percent) {
/ / TODO Auto-generated method stub

}

/ / change trigger event
public void surfaceChanged (SurfaceHolder holder, int format, int width,
int height) {
/ / TODO Auto-generated method stub

}

/ / create a trigger when SurfaceHolder
public void surfaceCreated (SurfaceHolder holder) {
/ / TODO Auto-generated method stub
play (list.get (index));

}


public void surfaceDestroyed (SurfaceHolder holder) {
/ / TODO Auto-generated method stub

}

/ / Destruction
@ Override
protected void onDestroy () {
/ / TODO Auto-generated method stub
super.onDestroy ();
if (mediaPlayer! = null) {
mediaPlayer.release ();
mediaPlayer = null;
}
}

private void getFiles (String url) {
try {
File file = new File (url);
File [] files = file.listFiles ();

for (int i = 0; i if (files [i]. isDirectory ()) {
getFiles (files [i]. getAbsolutePath ());
} else {
if (files [i]. getAbsolutePath (). endsWi (".3 gp")
| | files [i]. getAbsolutePath (). endsWith (". mp4")) {
list.add (files [i]. toString ());
System.out.println (files [i]. toString ());
}
}
}
} catch (Exception e) {
Log.d ("getfile", "Find Success!");
}

}
}
------ For reference only --------------------------------- ------
problem is found, is a synchronous and general preparedness.
------ For reference only -------------------------------------- -

---- - For reference only ---------------------------------------
successful landlord do? Can you share with the code?

没有评论:

发表评论