Objective: To achieve local video playback in the program. Think of using VideoView, but there is a problem, as the title describes. Internet also find some information, it is estimated
Keyword is not precise enough, so I did not find the answer. We help out, can you tell me why this problem occurs, or give me some tips, ideas on how to achieve
Local video playback program (what plug-ins like it?)
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
What prompted whether to stop playing? If there is error, the error message will Logcat made up.
Reply:
package com.zhu.view;
import java.io.File;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;
import com.zhu.R;
public class VideoPlay extends Activity implements Callback {
private String path;
private MediaPlayer mediaPlayer;
String pathload;
private SurfaceView surfaceView;
private boolean pause;
private String filenames;
@ Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
/ * Hide the status bar * /
this.getWindow (). setFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
/ * Hide the title bar * /
requestWindowFeature (Window.FEATURE_NO_TITLE);
setContentView (R.layout.video);
mediaPlayer = new MediaPlayer ();
Bundle bundle = this.getIntent () getExtras ();.
filenames = bundle.getString ("filenames");
pathload = bundle.getString ("pathload");
surfaceView = (SurfaceView) this.findViewById (R.id.surfaceView);
surfaceView.getHolder () setType (SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);.
. surfaceView.getHolder () setFixedSize (320, 180);
surfaceView.getHolder () setKeepScreenOn (true);.
surfaceView.getHolder () addCallback (this);.
playOn ();
}
public void mediaplay (View v) {
switch (v.getId ()) {
case R.id.playbutton:
if (pause && mediaPlayer! = null)
{
mediaPlayer.start ();
pause = false;
}
else {
String filename = filenames.toString ();
File file = new File (Environment.getExternalStorageDirectory (),
filename);
if (file.exists ()) {
path = file.getAbsolutePath ();
play (0);
} Else {
path = null;
Toast.makeText (this, R.string.filenoexsit, Toast.LENGTH_LONG) show ();.
}
}
break;
case R.id.pausebutton:
if (mediaPlayer.isPlaying ()) {
mediaPlayer.pause ();
pause = true;
} Else {
if (pause) {
mediaPlayer.start ();
pause = false;
}
}
break;
case R.id.resetbutton:
if (mediaPlayer.isPlaying ()) {
mediaPlayer.seekTo (0);
} Else {
if (path! = null) {
play (0);
}
}
break;
case R.id.stopbutton:
if (mediaPlayer.isPlaying ()) {
mediaPlayer.stop ();
}
break;
}
}
private void play (int position) {
try {
mediaPlayer.reset ();
mediaPlayer.setDataSource (path);
mediaPlayer.setDisplay (surfaceView.getHolder ());
mediaPlayer.prepare () ;/ / buffer
mediaPlayer.setOnPreparedListener (new PrepareListener (position));
} Catch (Exception e) {
e.printStackTrace ();
}
}
private void playOn () {
try {
mediaPlayer.reset ();
mediaPlayer.setDataSource (pathload);
mediaPlayer.setDisplay (surfaceView.getHolder ());
mediaPlayer.prepare () ;/ / buffer
mediaPlayer.start ();
} Catch (Exception e) {
e.printStackTrace ();
}
}
private final class PrepareListener implements OnPreparedListener {
private int position;
public PrepareListener (int position) {
this.position = position;
}
public void onPrepared (MediaPlayer mp) {
mediaPlayer.start ();
if (position> 0)
mediaPlayer.seekTo (position);
}
}
@ Override
public void surfaceCreated (SurfaceHolder holder) {
/ / TODO Auto-generated method stub
}
@ Override
public void surfaceChanged (SurfaceHolder holder, int format, int width,
int height) {
/ / TODO Auto-generated method stub
}
@ Override
public void surfaceDestroyed (SurfaceHolder holder) {
/ / TODO Auto-generated method stub
}
}
Reply:
No error message, the specific situation is this: Play a 15 seconds too 3gp format video, the first three seconds of video in 10 seconds to play 3-5 this interval, the video will stop playing, but the progress bar moving, 10 seconds After the video, the progress bar had stopped
Reply:
package com.android.loop;
import java.io.IOException;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.os.PowerManager;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
public class PreviewActivityLoop extends Activity {
public static final String VIDEO_FOLDER_PATH = PropagandaVedioSettings.path;
private PowerManager.WakeLock localWakeLock = null;
private SurfaceView surfaceView = null;
private SurfaceHolder holder = null;
private MediaPlayer mPlayer = null;
private String [] sourceList;
private int sourceIndex = -1;
FileList filelist = new FileList ();
public void onCreate (Bundle paramBundle) {
super.onCreate (paramBundle);
requestWindowFeature (Window.FEATURE_NO_TITLE); / / hide title
getWindow (). setFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN
, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setRequestedOrientation (0) ;/ / change display orientation
localWakeLock = ((PowerManager) getSystemService (Context.POWER_SERVICE))
. NewWakeLock (PowerManager.FULL_WAKE_LOCK, "DoNotDimScreen");
localWakeLock.acquire () ;/ / do not auto lock
View view = getLayoutInflater () inflate (R.layout.surfaceview, null);.
setContentView (view);
Bundle bundle = getIntent () getExtras ();.
sourceIndex = bundle.getInt ("sourceIndex");
sourceList = bundle.getStringArray ("sourceList");
mPlayer = new MediaPlayer ();
surfaceView = (SurfaceView) findViewById (R.id.surfaceView);
surfaceView.setOnTouchListener (new View.OnTouchListener () {
@ Override
public boolean onTouch (View v, MotionEvent event) {
finish ();
return false;
}
});
holder = surfaceView.getHolder ();
holder.setType (SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
holder.setKeepScreenOn (true);
holder.addCallback (new SurfaceListener ());
mPlayer.setOnCompletionListener (new MediaPlayer.OnCompletionListener () {
@ Override
public void onCompletion (MediaPlayer mp) {
/ / TODO Auto-generated method stub
Intent intent = new Intent ();
Bundle bundleloop = new Bundle ();
bundleloop.putInt ("sourceIndexloop", sourceIndex);
bundleloop.putStringArray ("sourceListloop", sourceList);
intent.putExtras (bundleloop);
ComponentName cn = new ComponentName ("com.android.loop", "com.android.loop.PreviewActivity");
intent.setComponent (cn);
startActivity (intent);
finish ();
}
});
}
@ Override
protected void onDestroy () {
if (mPlayer.isPlaying ()) {
mPlayer.stop ();
}
mPlayer.release () ;/ / release mPlayer
localWakeLock.release () ;/ / release localWakeLock
super.onDestroy ();
}
@ Override
protected void onPause () {
finish ();
super.onPause ();
}
private void play (String dataSource) {
try {
mPlayer.reset ();
mPlayer.setAudioStreamType (AudioManager.STREAM_MUSIC);
mPlayer.setDataSource (dataSource);
mPlayer.setDisplay (holder);
mPlayer.prepare ();
mPlayer.start ();
} Catch (IOException e) {
Log.e ("Exception", e.toString ());
}
/ / MPlayer.setOnPreparedListener (new OnPreparedListener () {
/ /
/ / @ Override
/ / Public void onPrepared (MediaPlayer mp) {
/ / MPlayer.start ();
/ /}
/ /});
}
private class SurfaceListener implements SurfaceHolder.Callback {
@ Override
public void surfaceChanged (SurfaceHolder holder, int format, int width,
int height) {
/ / TODO Auto-generated method stub
}
@ Override
public void surfaceCreated (SurfaceHolder holder) {
/ / Play (filelist.getVideo (sourceIndex));
play (sourceList [sourceIndex]);
sourceIndex + +;
sourceIndex = sourceIndex% sourceList.length;
}
@ Override
public void surfaceDestroyed (SurfaceHolder holder) {
}
}
}
Reply:
Can the code layout files posted, then the appropriate code for your comments, you are given a piece of code like this, is really hard to understand what you mean, thank you
Reply:
Well, thank you, playback problems be solved. But encountered two new problems: First, the video playback of a meal card, search, said that the reason for the simulator, not the problem on the real machine, because the real test is useless, so I do not know ; Second, the problem of video format support, read online said, Android supported video formats by default only 3gp, mp4 (mp4 looks like not all supported), if I want to play avi videos, is there any plug-ins you can use , or to write their own decoding achieve?
Reply:
Will the landlord is how to solve the first issue? I have encountered, and also hope to be able to share with the landlord
No comments:
Post a Comment