我正在尝试在视频视频中播放视频,并希望在背景图表视图中显示来自设备的实时相机Feed。但问题是Surface视图覆盖视频视图完全隐藏它并且视频在视频中无法正常播放相当独特的声音。有人可以帮我这个吗?
活动XML代码:
<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文件:
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");
}
}
答案 0 :(得分:1)
我是通过为setZOrderOnTop(true)
添加videoview
来实现它的,并将其展现在前面,而且效果非常好。