Friday, February 14, 2014

android mediaPlay Play Video Playback


I have a storage path List played four files inside, I want to play these four documents have been circulating. To ask how to achieve? ? Best to give a demo, thank you very much!<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
Mediaplayer plus a OnCompletionListener

Upon completion wants to play next.
Reply:
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 object
hodler = sView.getHolder ();
/ / Set callback function
hodler.addCallback (this);
/ / Set 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
by SurfaceViewmediaPlayer.setDisplay (hodler);
/ / Prepare
mediaPlayer.prepareAsync ();
/ / Set listener event
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

}

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

}

/ / Trigger
created when SurfaceHolderpublic void surfaceCreated (SurfaceHolder holder) {
/ / TODO Auto-generated method stub
play (list.get (index));

}


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

}

/ / Destroy
@ 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!");
}

}
}
Reply:
Before the next play, the mediaplayer object stop () && release (), so try it again?
After a seemingly broadcast directly setDataSource () is not acceptable.
Reply:
I was able to achieve this function today. But I am using VideoView controls do. .
You've inherited OnCompletionListener, Why add the listener time, but also a new own it.
Reply:
references, 4th Floor reply:
I was able to achieve this function today. But I am using VideoView controls do. .
You've inherited OnCompletionListener, Why add the listener time, but also a new own it.

You so, this method is not executed, only the implementation of the listener you add the onCompletion methods.
public void onCompletion (MediaPlayer mp) {
/ / TODO Auto-generated method stub
/ / Mp.seekTo (0);

mp.start ();
}

Reply:
Problems found, is a synchronous and general preparation problems.
Reply:

Reply:
Landlord success? Can you share with the code?
Reply:
Seeking to share the source code, thank you, my mailbox is yangxingda1988@163.com tribute! ! !
Reply:
Seeking to share the source code, thank you, I have to mail a yangxingda1988@163.com tribute! ! !

No comments:

Post a Comment