Hi Android Developers,

I meet some problems playing multiple videos at the same time on Samsung 
Galaxy S2.
I am trying to play multiple videos at the same time in a screen. Then I 
initialize multiple VideoView on the same screen, and load the different 
videos. While only the first VideoView is being played successfully.

The device is running on Android 4.0.4
I am trying to read the source code of VideoView, I found out that the 
VideoView is a holder of SurfaceView + MediaPlayer. And I locate the error 
is caused by multiple MediaPlayer trying to prepare video at the same time.
After the error is occurred, it seems all the Media service is error on the 
device, e.g. the native Camera app, and native VideoPlayer app cannot work 
until I reboot the device.
The error only happen on the S2, while the same code works well on my other 
devices (e.g., Galaxy S3 JB, Nexus 4 JB_MR1, and some other ICS devices).

My question is what is the difference between S2 (SGH-T989) and other 
devices in their Media framework?

The source code is already attached in message.
The logcat looks like below:
04-30 14:30:24.218: I/Adreno200-EGLSUB(7938): <ConfigWindowMatch:2078>: 
Format RGBA_8888.
04-30 14:30:24.338: E/MediaPlayer(7938): error (1, -12)
04-30 14:30:24.368: E/MediaPlayer(7938): error (1, -12)
04-30 14:30:24.408: E/MediaPlayer(7938): Error (1,-12)
04-30 14:30:24.408: D/VideoView(7938): Error: 1,-12
04-30 14:30:24.408: E/VideoPlayActivity(7938): onError. what=1 extra=-12
04-30 14:30:24.428: E/MediaPlayer(7938): Error (1,-12)
04-30 14:30:24.428: D/VideoView(7938): Error: 1,-12
04-30 14:30:24.428: E/VideoPlayActivity(7938): onError. what=1 extra=-12

Thanks.

-- Kan Wu
[email protected]

-- 
-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Attachment: activity_video_play.xml
Description: XML document

package com.example.videoplayer;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.VideoView;

public class VideoPlayActivity extends Activity {

    final private static String TAG = VideoPlayActivity.class.getSimpleName();

    private int socketPort;

    private VideoView mActiveVideoView;
    private VideoView mPrevVideoView;
    private VideoView mNextVideoView;
    private MediaPlayerCallback mCallback;

    private int mPlayIndex = 0;

    private Handler mHandler;
    private Runnable mStartNextVideoThread;

    private String[] mVideoFiles;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_play);

        mVideoFiles = getVideosList();

        mActiveVideoView = (VideoView) findViewById(R.id.video_view_1);
        mActiveVideoView.setVisibility(View.VISIBLE);

        mPrevVideoView = (VideoView) findViewById(R.id.video_view_2);
        mPrevVideoView.setVisibility(View.VISIBLE);

        mNextVideoView = (VideoView) findViewById(R.id.video_view_3);
        mNextVideoView.setVisibility(View.VISIBLE);

        mCallback = new MediaPlayerCallback();
        mActiveVideoView.setOnCompletionListener(mCallback);
        mPrevVideoView.setOnCompletionListener(mCallback);
        mNextVideoView.setOnCompletionListener(mCallback);
        mActiveVideoView.setOnPreparedListener(mCallback);
        mPrevVideoView.setOnPreparedListener(mCallback);
        mNextVideoView.setOnPreparedListener(mCallback);
        mActiveVideoView.setOnErrorListener(mCallback);
        mPrevVideoView.setOnErrorListener(mCallback);
        mNextVideoView.setOnErrorListener(mCallback);
    }

    @Override
    protected void onResume() {
        super.onResume();

        mActiveVideoView.setVideoPath(mVideoFiles[mPlayIndex]);
        int nextIdx = (mPlayIndex + 1) == mVideoFiles.length ? 0 : mPlayIndex + 1;
        mNextVideoView.setVideoPath(mVideoFiles[nextIdx]);
       	int prevIdx = (mPlayIndex - 1) < 0 ? mVideoFiles.length - 1 : mPlayIndex - 1;
      	mPrevVideoView.setVideoPath(mVideoFiles[prevIdx]);

        mActiveVideoView.start();
       	mPrevVideoView.start();
       	mNextVideoView.start();

       	if (mActiveVideoView.getDuration() < 0) {
        	Log.w(TAG, "Cannot start playing video.");
        	return;
        }
    }

    @Override
    protected void onPause() {
        super.onPause();

        if (mActiveVideoView != null) {
            mActiveVideoView.suspend();
        }
        if (mPrevVideoView != null) {
            mPrevVideoView.suspend();
        }
        if (mNextVideoView != null) {
            mNextVideoView.suspend();
        }
    }

    private class MediaPlayerCallback implements MediaPlayer.OnCompletionListener,
        MediaPlayer.OnPreparedListener, MediaPlayer.OnErrorListener, MediaPlayer.OnInfoListener {

        @Override
        public void onCompletion(MediaPlayer mp) {
            Log.v(TAG, "onCompletion.");
        }

        @Override
        public void onPrepared(MediaPlayer mp) {
            Log.v(TAG, "onPrepared. mPlayIndex=" + mPlayIndex);
        }

        @Override
        public boolean onError(MediaPlayer mp, int what, int extra) {
            Log.e(TAG, "onError. what=" + what + " extra=" + extra);
            return false;
        }

        @Override
        public boolean onInfo(MediaPlayer mp, int what, int extra) {
            Log.i(TAG, "onInfo. what=" + what + " extra=" + extra);
            return false;
        }
    }

    private String[] getVideosList() {
        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MediaDir");

        if (!mediaStorageDir.exists()) {
            if (!mediaStorageDir.mkdirs()) {
                Log.d("MediaDir", "failed to create directory");
                return null;
            }
        }

        String[] videoFiles = new String[mediaStorageDir.list().length];
        int idx = 0;
        for (File f : mediaStorageDir.listFiles()) {
            videoFiles[idx] = f.getAbsolutePath();
            idx += 1;
        }
        return videoFiles;
    }

}

Reply via email to