2

I want to play a video after a couple of images in the same SurfaceView. If I show images AFTER videos, everything works fine. But if I want to play a video after the images, the SurfaceView stays blank (black). Can anyone help? It seems to be a problem of clearing or resetting the SurfaceView!

The code of the SurfaceView (custom SurfaceView!!!):

public class MySurfaceView extends SurfaceView implements  SurfaceHolder.Callback, MediaPlayer.OnCompletionListener {

    private Bitmap image;
    private MediaPlayer mediaPlayer;
    private List<MySurfaceListener> objectsToCall = new ArrayList<>();

    public MySurfaceView(Context context) {
        super(context);
        getHolder().addCallback(this);
    }

    public void addOnPlayingFinishedListener(MySurfaceListener listener) {
        objectsToCall.add(listener);
    }

    public void notifyAllOnPlayingFinishedListener() {
        for(MySurfaceListener l : objectsToCall) {
            l.onMediaPlayFinished();
        }
    }

    public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
        // Helper method to scale down the image files
    }

    public static Bitmap decodeSampledBitmapFromFile(String path, int reqWidth, int reqHeight) {

        // Another helper method for the image files
    }

    public void drawImage(String path, final int durationMs) {
        this.image = decodeSampledBitmapFromFile(path, 1920, 1080);
        Canvas canvas = null;
        try {
            canvas = getHolder().lockCanvas(null);
            synchronized (getHolder()) {
                onDraw(canvas);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (canvas != null) {
                getHolder().unlockCanvasAndPost(canvas);
            }
            image.recycle();
            image = null;
        }

        Thread showPictureTimer = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(durationMs);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        showPictureTimer.start();
        try {
            showPictureTimer.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        notifyAllOnPlayingFinishedListener();
    }

    public void playVideo(String path) {
        try {
            mediaPlayer = new MediaPlayer();
            mediaPlayer.setOnCompletionListener(this);
            mediaPlayer.setDisplay(getHolder());
            mediaPlayer.setDataSource(path);
            mediaPlayer.prepare();
            mediaPlayer.start();
        } catch (IllegalArgumentException e) {
            Log.d("MyMediaPlayerControl", e.getMessage());
        } catch (IllegalStateException e) {
            Log.d("MyMediaPlayerControl", e.getMessage());
        } catch (IOException e) {
            Log.d("MyMediaPlayerControl", e.getMessage());
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.BLACK);
        canvas.drawBitmap(this.image, 0, 0, new Paint());
    }

    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
        Log.e("myApp", "surfaceChanged");
    }

    public void surfaceCreated(SurfaceHolder arg0) {
        Log.e("myApp", "surfaceCreated");
    }

    public void surfaceDestroyed(SurfaceHolder arg0) {
        Log.e("myApp", "surfaceDestroyed");
    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        if (mediaPlayer != null) {
            mediaPlayer.stop();
            mediaPlayer.release();
            mediaPlayer = null;
        }
        notifyAllOnPlayingFinishedListener();
    }
}

And the thread which gives the file paths to the surface view for playing (should not be the problem):

public class MediaControlThread extends Thread implements MySurfaceListener {
    public static List<String> mediaList = new ArrayList<String>();
    MyMediaActivity activity;
    private MySurfaceView surfaceView;
    private int currentVideo = 0;

    public MediaControlThread(MyMediaActivity activity) {
        this.activity = activity;
        this.surfaceView = activity.getSurfaceView();
        this.surfaceView.addOnPlayingFinishedListener(this);
    }

    @Override
    public void run() {
        try {
            while(mediaList.size() == 0) {
               Thread.sleep(1000);
            }
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        playMedia(mediaList.get(currentVideo));
    }

    private void playMedia(String mediaPath) {
        if(checkFileForImage(mediaPath)) {
            surfaceView.drawImage(mediaPath, 2000);
        } else {
            surfaceView.playVideo(mediaPath);
        }
    }

    private boolean checkFileForImage(String fileName) {
        String file = fileName.toLowerCase();
        return file.endsWith(".jpg") ||
                file.endsWith(".img") ||
                file.endsWith(".bmp") ||
                file.endsWith(".jpeg") ||
                file.endsWith(".ico") ||
                file.endsWith(".tif");
    }

    @Override
    public void onMediaPlayFinished() {
        surfaceView.invalidate();
        surfaceView.destroyDrawingCache();
        currentVideo++;
        if (currentVideo > mediaList.size() - 1) {
            currentVideo = 0;
        }
        playMedia(mediaList.get(currentVideo));
    }
}
D. Müller
  • 3,336
  • 4
  • 36
  • 84

1 Answers1

3

That is, unfortunately, the expected behavior. Due to a quirk in Android, once you've drawn on a Surface with Canvas you can't do anything else.

You have three choices:

  1. Draw the image with OpenGL ES rather than Canvas. Once you detach GLES you can go back to showing videos.
  2. Present the image as a single-frame video. This works nicely for trivial things like blanking the screen, but is a poor choice for dynamic content.
  3. Use a FrameLayout to overlay an ImageView or custom View. Show the View when you want to show an image, hide it otherwise. So long as the Surface is at the default Z-depth, the ImageView will appear in front of it and obscure it.
fadden
  • 51,356
  • 5
  • 116
  • 166
  • Thank you for that information, you saved my day! I tried your 3rd approach and added an ImageView beside the SurfaceView to my layout. Depending on the media-type I set the imageView (in)visible. IMPORTANT: Never set the SurfaceView invisible/gone, this would lead to the destruction of the SurfaceView, so it won't be able to use it anymore (this was my fault!!!)! – D. Müller Oct 27 '15 at 18:28