Suresh Kumar S
Suresh Kumar S

Reputation: 182

Video not playing in video view - Surface View

I am trying to play a video in a videoview and want to show the live camera feed from the device in the background surface view.But the problem is Surface view overlays the videoview hiding it totally and video is not playing properly in videoview rather sound alone playing. Can anyone help me with this please?

Activity XML code:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<SurfaceView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/surfaceView"/>
    <ProgressBar
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:background="@drawable/seekbar_video"
        android:id="@+id/progressBar" />

    <ImageButton
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#00000000"
        android:layout_marginTop="20dp"
        android:src="@drawable/ic_close_3x"
        android:textSize="18dp"
        android:textAllCaps="false"
        android:layout_below="@+id/progressBar"
        android:drawableLeft="@drawable/ic_back_3x"
        android:id="@+id/close"
        />
    <ImageButton
        android:layout_alignParentRight="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#00000000"
        android:layout_marginTop="20dp"
        android:src="@drawable/ic_right_3x"
        android:layout_below="@+id/progressBar"
        android:textSize="18dp"
        android:layout_marginRight="20dp"
        android:textAllCaps="false"
        android:id="@+id/next"
        android:visibility="invisible"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RECORD"
        android:layout_marginTop="100dp"
        android:textColor="#ffffff"
        android:gravity="center"
        android:layout_centerHorizontal="true"
        android:textSize="25dp"
        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:background="#ffffff"
        android:gravity="center"
        android:orientation="vertical"
        android:layout_marginTop="140dp"
        android:id="@+id/linearLayout">
        <VideoView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/memix_video"
            />

    </LinearLayout>
    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#00000000"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="50dp"
        android:id="@+id/camera"
        android:src="@drawable/ic_frontcamera_3x"
        android:layout_marginBottom="40dp"
        />

    <ImageButton
        android:id="@+id/record"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/circle_bg"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="20dp"
        android:src="@drawable/ic_video"
        />
    <ImageButton
        android:id="@+id/record_large"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@drawable/circle_bg"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="20dp"
        android:src="@drawable/ic_video"
        android:visibility="invisible"
        />
    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#00000000"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginRight="50dp"
        android:src="@drawable/ic_undo_3x"
        android:id="@+id/undo"
        android:layout_marginBottom="40dp"
        />
</RelativeLayout>
</FrameLayout>

Java File:

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.media.MediaRecorder;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.hardware.Camera.Parameters;
import android.media.MediaPlayer;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.widget.VideoView;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class MemixActivity extends AppCompatActivity  implements Callback{
    VideoView videoView;
    private static final int MY_PERMISSIONS_REQUEST_ACCOUNTS = 1;
    File appDir = new File(Environment.getExternalStorageDirectory()+File.separator+"Memecry/temp");
    String filepath=appDir.getAbsolutePath()+"/trim.mp4";

    private SurfaceView mPreview;
    private Camera mCamera;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_memix);
        Log.e("file",filepath);
        videoView=(VideoView)findViewById(R.id.memix_video) ;
        mPreview=(SurfaceView)findViewById(R.id.surfaceView);


        videoView.setVideoPath("/storage/emulated/0/Memecry/temp/trim.mp4");
              videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mp.setLooping(true);
                mp.start();
                int duration=mp.getDuration();
                Log.e("dur", String.valueOf(duration));
            }
        });
        mPreview = (SurfaceView)findViewById(R.id.surfaceView);


       if( checkAndRequestPermissions()) {
           Log.e("camera","Camera Started");

           mPreview.getHolder().addCallback(this);
           mPreview.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

           mCamera = Camera.open();

       }
    }





    private boolean checkAndRequestPermissions() {
        int permissionCAMERA = ContextCompat.checkSelfPermission(this,
                Manifest.permission.CAMERA);


        int storagePermission = ContextCompat.checkSelfPermission(this,


                Manifest.permission.RECORD_AUDIO);



        List<String> listPermissionsNeeded = new ArrayList<>();
        if (storagePermission != PackageManager.PERMISSION_GRANTED) {
            listPermissionsNeeded.add(Manifest.permission.RECORD_AUDIO);
        }
        if (permissionCAMERA != PackageManager.PERMISSION_GRANTED) {
            listPermissionsNeeded.add(Manifest.permission.CAMERA);
        }
        if (!listPermissionsNeeded.isEmpty()) {
            ActivityCompat.requestPermissions(this,


                    listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), MY_PERMISSIONS_REQUEST_ACCOUNTS);
            return false;
        }

        return true;
    }



    @Override    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case MY_PERMISSIONS_REQUEST_ACCOUNTS:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    //Permission Granted Successfully. Write working code here.
                    mPreview.getHolder().addCallback(this);
                    mPreview.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

                    mCamera = Camera.open();



                } else {
                    //You did not accept the request can not use the functionality.
                    checkAndRequestPermissions();
                }
                break;
        }
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        try {
            mCamera.setPreviewDisplay(mPreview.getHolder());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        Camera.Parameters params = mCamera.getParameters();
        List<Camera.Size> sizes = params.getSupportedPreviewSizes();
        Camera.Size selected = sizes.get(0);
        params.setPreviewSize(selected.width,selected.height);
        mCamera.setParameters(params);

        mCamera.startPreview();

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        Log.i("PREVIEW","surfaceDestroyed");
    }
}

Upvotes: 2

Views: 1563

Answers (1)

Suresh Kumar S
Suresh Kumar S

Reputation: 182

I did it by adding setZOrderOnTop(true) for the videoview to bring it front and it's working perfectly.

Upvotes: 2

Related Questions