7

I am trying to implement list view with videos as it's elements. I am using this project to display video on texture view. It uses MediaPlayer underneath. It fails (most of the time) when loading two videos at same time.

The error I get is :

TextureVideoView error. File or network related operation errors.

MediaPlayer: error (1, -2147479551)

and this also happens when file is loaded from the disk

In error handling part I tried to reset the URL. Then I mostly get

E/BufferQueueProducer: [unnamed-30578-12] disconnect(P): connected to another API (cur=0 req=3)

error. What is not clear to me is that setting some arbitrary video from the web will work but retrying same URL will fail.

So in OnErrorListener :

textureView.setVideo(item.getUriMp4(),MediaFensterPlayerController.DEFAULT_VIDEO_START); 

will fail but :

textureView.setVideo("http://different.video" ... )

will work just great.

This is also not a problem with a specific file , as while scrolling different video files will fail. Sometimes those which failed will work next time etc.

I also tried MediaCodec and MediaExtractor combination instead of MediaPlayer approach but I run into , what looks like, device specific platform bug

any hints? any suggestions?

thanks

w.

Community
  • 1
  • 1
wonglik
  • 1,043
  • 4
  • 18
  • 36
  • 1
    are you trying to get multiple videos to actually *play at the same time in a listview? Or do you just want to load them and prepare them for playback (one at a time) if the user taps on them? – Yevgeny Simkin Oct 29 '15 at 08:09
  • I am trying to play them at once when visible but I am ok to somehow delay start of the other video if that would be the constraint. – wonglik Oct 29 '15 at 08:25
  • @Yvette , if two rows appears on the screen (99% of time) then it will try to play both simultaneously. – wonglik Oct 31 '15 at 09:49
  • 1
    @Yvette ideally both would play. They do not need to start at exactly same time but if two videos fits the screen both should play – wonglik Oct 31 '15 at 11:07
  • @Yvette : For UX reasons. It does not make sense for user to click start on each single video. Just like in Vine. And since those videos are mostly horizontal , most of the time there will be more then one on the screen. – wonglik Nov 01 '15 at 07:47
  • 1
    @Yvette They are unpleasant when sound mixes. I mute the players so this is not the issue here. – wonglik Nov 01 '15 at 09:55
  • how about create another thread for playing the video? – Randyka Yudhistira Nov 02 '15 at 02:43
  • don't you think its the listview who is flawing your party?? i mean think about it. – Elltz Nov 06 '15 at 22:28
  • @Elltz Playing video in a list is well established UX pattern. Just check vine or FB. ListView is not a problem here. – wonglik Nov 07 '15 at 05:33
  • @RandykaYudhistira It didn't help. It seems that MediaPlayer can not handle two videos starting at roughly the same time. – wonglik Nov 07 '15 at 05:34
  • @wonglik any luck getting that to work? If yes, please share with us. – Mateus Gondim Jan 23 '16 at 15:03
  • @MateusGondim I didn't succeed with playing all videos simultaneously. It seems MediaPlayer is not happy with that. My walk around was to stop other videos and play the one that is in most center position of the screen. – wonglik Jan 28 '16 at 09:57

5 Answers5

2

You can Try this instead of a library It's taken from a sample by Google on 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;
    //How many videos to play simultaneously.

    // Must be static storage so they'll survive Activity restart.
    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");
    }


    /**
     * Video playback blob.
     * <p>
     * Encapsulates the video decoder and playback surface.
     * <p>
     * We want to avoid tearing down and recreating the video decoder on orientation changes,
     * because it can be expensive to do so.  That means keeping the decoder's output Surface
     * around, which means keeping the SurfaceTexture around.
     * <p>
     * It's possible that the orientation change will cause the UI thread's EGL context to be
     * torn down and recreated (the app framework docs don't seem to make any guarantees here),
     * so we need to detach the SurfaceTexture from EGL on destroy, and reattach it when
     * the new SurfaceTexture becomes available.  Happily, TextureView does this for us.
     */
    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;

        /**
         * Constructs the VideoBlob.
         *
         * @param view The TextureView object we want to draw into.
         * @param movieTag Which movie to play.
         * @param ordinal The blob's ordinal (only used for log messages).
         */
        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);
        }

        /**
         * Performs partial construction.  The VideoBlob is already created, but the Activity
         * was recreated, so we need to update our 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);
            }
        }

        /**
         * Stop playback and shut everything down.
         */
        public void stopPlayback() {
            Log.d(LTAG, "stopPlayback");
            mPlayThread.requestStop();
            // TODO: wait for the playback thread to stop so we don't kill the Surface
            //       before the video stops

            // We don't need this any more, so null it out.  This also serves as a signal
            // to let onSurfaceTextureDestroyed() know that it can tell TextureView to
            // free the SurfaceTexture.
            mSavedSurfaceTexture = null;
        }

        @Override
        public void onSurfaceTextureAvailable(SurfaceTexture st, int width, int height) {
            Log.d(LTAG, "onSurfaceTextureAvailable size=" + width + "x" + height + ", st=" + st);

            // If this is our first time though, we're going to use the SurfaceTexture that
            // the TextureView provided.  If not, we're going to replace the current one with
            // the original.

            if (mSavedSurfaceTexture == null) {
                mSavedSurfaceTexture = st;

                File sliders = ContentManager.getInstance().getPath(mMovieTag);
                mPlayThread = new PlayMovieThread(sliders, new Surface(st), mCallback);
            } else {
                // Can't do it here in Android <= 4.4.  The TextureView doesn't add a
                // listener on the new SurfaceTexture, so it never sees any updates.
                // Needs to happen from activity onCreate() -- see recreateView().
                //Log.d(LTAG, "using saved st=" + mSavedSurfaceTexture);
                //mTextureView.setSurfaceTexture(mSavedSurfaceTexture);
            }
        }

        @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);
            // The SurfaceTexture is already detached from the EGL context at this point, so
            // we don't need to do that.
            //
            // The saved SurfaceTexture will be null if we're shutting down, so we want to
            // return "true" in that case (indicating that TextureView can release the ST).
            return (mSavedSurfaceTexture == null);
        }

        @Override
        public void onSurfaceTextureUpdated(SurfaceTexture st) {
            //Log.d(TAG, "onSurfaceTextureUpdated st=" + st);
        }
    }

    /**
     * Thread object that plays a movie from a file to a surface.
     * <p>
     * Currently loops until told to stop.
     */
    private static class PlayMovieThread extends Thread {
        private final File mFile;
        private final Surface mSurface;
        private final SpeedControlCallback mCallback;
        private MoviePlayer mMoviePlayer;

        /**
         * Creates thread and starts execution.
         * <p>
         * The object takes ownership of the Surface, and will access it from the new thread.
         * When playback completes, the Surface will be released.
         */
        public PlayMovieThread(File file, Surface surface, SpeedControlCallback callback) {
            mFile = file;
            mSurface = surface;
            mCallback = callback;

            start();
        }

        /**
         * Asks MoviePlayer to halt playback.  Returns without waiting for playback to halt.
         * <p>
         * Call from UI thread.
         */
        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"?>

<!-- portrait layout -->
<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>
Vishavjeet Singh
  • 1,385
  • 11
  • 13
  • 1
    This is an example from Google Grafika. It works most of the time. Check my other [SO question](http://stackoverflow.com/questions/33296292/android-mediaextractor-crash-when-decoding-some-mp4-files-libc-fatal-signal-11) if you are curious why I can not use it. – wonglik Nov 06 '15 at 06:55
2

Add all your video paths in array or ArrayList and implement mediaplayer.setOnMediaPlayerCompletionListener, when media will be played then this interface will be called from here initialise new Media player instance providing new media and call start()

I am just telling you the logic, I hope this will work

Abdul Aziz
  • 442
  • 5
  • 12
  • Thing is that those videos should loop. So when completion hook is called I simply rewind to the start and play over. Beside original idea was to play video whenever it is visible on the screen. Issue is in playing videos simultaneously and it seems it might be impossible with MediaPlayer – wonglik Nov 07 '15 at 05:37
  • I have found something for you that may help you and while searching for a solution I also recalled a news that some high end samsung phones can play multiple video files and which I also saw in news few years back so its totally depends on hardware too... http://stackoverflow.com/a/10161316/5479863 – Abdul Aziz Nov 07 '15 at 07:15
1

Use VideoView instead of ListView it may work. Take a look at here http://developer.android.com/reference/android/widget/VideoView.html

Sathish Kumar
  • 919
  • 1
  • 13
  • 27
1

This question has already got several answers here: stackoverflow.com/questions/31532893/i-want-to-display-multiple-video-in-listview-using-video-but-not-able-to-do-this. Unless your problem is different or more specific, this topic will be marked as a duplicate.

Community
  • 1
  • 1
  • Non of those answers is actually accepted or has application to my problem. – wonglik Nov 06 '15 at 06:53
  • @wonglik Your problem is about ListView, right? Or do you mean a _list view_? If it is ListView, could you clarify how your problem differs from the one in the link I gave? –  Nov 07 '15 at 02:18
  • My problem is about playing simultaneously two videos that happen to be on the list view. They could as well be in LinearLayout and problem would be exactly the same. – wonglik Nov 07 '15 at 05:31
0

Current Solution: I recommend JavaCV / OpenCV for playing multiple videos at once in Java. It supports allot of formats.

Tutorial - http://ganeshtiwaridotcomdotnp.blogspot.co.nz/search/label/OpenCV-JavaCV

JavaFX can also play some .MP4 video formats.

Old Solution: - Even though JMF can play multiple videos at once it is out dated and no longer maintained.

Chaoslab
  • 33
  • 8