您可以尝试使用此方法代替库。这是从Google在github上的示例中提取的:
Decodes two video streams simultaneously to two TextureViews.
One key feature is that the video decoders do not stop when the activity is restarted due
to an orientation change. This is to simulate playback of a real-time video stream. If
the Activity is pausing because it's "finished" (indicating that we're leaving the Activity
for a nontrivial amount of time), the video decoders are shut down.
TODO: consider shutting down when the screen is turned off, to preserve battery.
Java:
DoubleDecodeActivity.java
public class DoubleDecodeActivity extends Activity {
private static final String TAG = MainActivity.TAG;
private static final int VIDEO_COUNT = 2;
private static boolean sVideoRunning = false;
private static VideoBlob[] sBlob = new VideoBlob[VIDEO_COUNT];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_double_decode);
if (!sVideoRunning) {
sBlob[0] = new VideoBlob((TextureView) findViewById(R.id.double1_texture_view),
ContentManager.MOVIE_SLIDERS, 0);
sBlob[1] = new VideoBlob((TextureView) findViewById(R.id.double2_texture_view),
ContentManager.MOVIE_EIGHT_RECTS, 1);
sVideoRunning = true;
} else {
sBlob[0].recreateView((TextureView) findViewById(R.id.double1_texture_view));
sBlob[1].recreateView((TextureView) findViewById(R.id.double2_texture_view));
}
}
@Override
protected void onPause() {
super.onPause();
boolean finishing = isFinishing();
Log.d(TAG, "isFinishing: " + finishing);
for (int i = 0; i < VIDEO_COUNT; i++) {
if (finishing) {
sBlob[i].stopPlayback();
sBlob[i] = null;
}
}
sVideoRunning = !finishing;
Log.d(TAG, "onPause complete");
}
private static class VideoBlob implements TextureView.SurfaceTextureListener {
private final String LTAG;
private TextureView mTextureView;
private int mMovieTag;
private SurfaceTexture mSavedSurfaceTexture;
private PlayMovieThread mPlayThread;
private SpeedControlCallback mCallback;
public VideoBlob(TextureView view, int movieTag, int ordinal) {
LTAG = TAG + ordinal;
Log.d(LTAG, "VideoBlob: tag=" + movieTag + " view=" + view);
mMovieTag = movieTag;
mCallback = new SpeedControlCallback();
recreateView(view);
}
public void recreateView(TextureView view) {
Log.d(LTAG, "recreateView: " + view);
mTextureView = view;
mTextureView.setSurfaceTextureListener(this);
if (mSavedSurfaceTexture != null) {
Log.d(LTAG, "using saved st=" + mSavedSurfaceTexture);
view.setSurfaceTexture(mSavedSurfaceTexture);
}
}
public void stopPlayback() {
Log.d(LTAG, "stopPlayback");
mPlayThread.requestStop();
mSavedSurfaceTexture = null;
}
@Override
public void onSurfaceTextureAvailable(SurfaceTexture st, int width, int height) {
Log.d(LTAG, "onSurfaceTextureAvailable size=" + width + "x" + height + ", st=" + st);
if (mSavedSurfaceTexture == null) {
mSavedSurfaceTexture = st;
File sliders = ContentManager.getInstance().getPath(mMovieTag);
mPlayThread = new PlayMovieThread(sliders, new Surface(st), mCallback);
} else {
}
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture st, int width, int height) {
Log.d(LTAG, "onSurfaceTextureSizeChanged size=" + width + "x" + height + ", st=" + st);
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture st) {
Log.d(LTAG, "onSurfaceTextureDestroyed st=" + st);
return (mSavedSurfaceTexture == null);
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture st) {
}
}
private static class PlayMovieThread extends Thread {
private final File mFile;
private final Surface mSurface;
private final SpeedControlCallback mCallback;
private MoviePlayer mMoviePlayer;
public PlayMovieThread(File file, Surface surface, SpeedControlCallback callback) {
mFile = file;
mSurface = surface;
mCallback = callback;
start();
}
public void requestStop() {
mMoviePlayer.requestStop();
}
@Override
public void run() {
try {
mMoviePlayer = new MoviePlayer(mFile, mSurface, mCallback);
mMoviePlayer.setLoopMode(true);
mMoviePlayer.play();
} catch (IOException ioe) {
Log.e(TAG, "movie playback failed", ioe);
} finally {
mSurface.release();
Log.d(TAG, "PlayMovieThread stopping");
}
}
}
}
XML:
activity_double_decode.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:baselineAligned="false"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:layout_weight="1"
android:layout_marginBottom="8dp" >
<TextureView
android:id="@+id/double1_texture_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:layout_weight="1" >
<TextureView
android:id="@+id/double2_texture_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>