0

携帯電話でビデオを録画するときに小さな問題があります。私は次の例に従って学び、インスピレーションを得ました。

http://examples.javacodegeeks.com/android/core/android-video-capture-example/

それに基づいて(ところで良い例)、ビデオを録画すると、アスペクトが少し変わるようですが、それは避けたいです。

何かアドバイス?

Android コード:

public class VideoPreivewFragment extends Fragment implements TextureView.SurfaceTextureListener, OnClickListener {
    @InjectView(R.id.preview_movie)
    TextureView mTextureView;
    @InjectView(R.id.video_play_button)
    ImageButton mButtonVideoPlay;
    @InjectView(R.id.playMovie_afl)
    AspectFrameLayout layout;

    private MediaPlayer mMediaPlayer;
    float mVideoHeight, mVideoWidth;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View viewRoot = inflater.inflate(R.layout.video_preview_screen, container, false);
        ButterKnife.inject(this, viewRoot);
        calculateVideoSize();
        mTextureView.setSurfaceTextureListener(this);
        Backendless.setUrl("https://api.backendless.com");
        return viewRoot;
    }

    private void calculateVideoSize() {
        try {
            String videopath = ((VideoPreviewActivity) getActivity()).
                    fragmentArgumentToIntent(getArguments()).getStringExtra(Constants.TEMP_VIDEOPATH);
            MediaMetadataRetriever metaRetriever = new MediaMetadataRetriever();
            metaRetriever.setDataSource(videopath);
            String height = metaRetriever
                    .extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT);
            String width = metaRetriever
                    .extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH);
            mVideoHeight = Float.parseFloat(height);
            mVideoWidth = Float.parseFloat(width);
        } catch (Exception e) {
        }
    }


    private void adjustAspectRatio(float videoWidth, float videoHeight) {
        int viewWidth = layout.getWidth();
        int viewHeight = layout.getHeight();
        double aspectRatio = (double) videoHeight / videoWidth;

        int newWidth, newHeight;
        if (viewHeight > (int) (viewWidth * aspectRatio)) {
            // limited by narrow width; restrict height
            newWidth = viewWidth;
            newHeight = (int) (viewWidth * aspectRatio);
        } else {
            // limited by short height; restrict width
            newWidth = (int) (viewHeight / aspectRatio);
            newHeight = viewHeight;
        }
        int xoff = (viewWidth - newWidth) / 2;
        int yoff = (viewHeight - newHeight) / 2;

        Matrix txform = new Matrix();
        mTextureView.getTransform(txform);
        txform.setScale((float) newWidth / viewWidth, (float) newHeight / viewHeight);
        //txform.postRotate(10);          // just for fun
        txform.postTranslate(xoff, yoff);
        mTextureView.setTransform(txform);
    }

    @Override
    public void onStart() {
        super.onStart();
    }


    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        ButterKnife.reset(this);
        super.onPause();
    }

    @Override
    public void onStop() {
        ButterKnife.reset(this);
        super.onStop();
    }

    @Override
    public void onDestroy() {
        ButterKnife.reset(this);
        if (mMediaPlayer != null) {
            // Make sure we stop video and release resources when activity is destroyed.
            mMediaPlayer.stop();
            mMediaPlayer.release();
            mMediaPlayer = null;
        }
        super.onDestroy();
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    @Override
    @OnClick({R.id.preview_movie, R.id.video_play_button})
    public void onClick(View view) {
        if (view.getId() == R.id.video_play_button) {
        } else if (view.getId() == R.id.preview_movie) {
            if(mMediaPlayer.isPlaying()) {
                mMediaPlayer.pause();
            }
            else {
                mMediaPlayer.start();
            }
        }
    }

    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int i, int i1) {
        Surface surface = new Surface(surfaceTexture);

        try {
            String videopath = ((VideoPreviewActivity) getActivity()).
                    fragmentArgumentToIntent(getArguments()).getStringExtra(Constants.TEMP_VIDEOPATH);
            mMediaPlayer = new MediaPlayer();
            mMediaPlayer.setDataSource(videopath);
            mMediaPlayer.setSurface(surface);

            // don't forget to call MediaPlayer.prepareAsync() method when you use constructor for
            // creating MediaPlayer
            mMediaPlayer.prepareAsync();

            mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mediaPlayer) {
                    int videoWidth = mMediaPlayer.getVideoWidth();
                    int videoHeight = mMediaPlayer.getVideoHeight();
                    DisplayMetrics displayMetrics = new DisplayMetrics();
                    getActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
                    int screenWidth = displayMetrics.widthPixels;
                    int screenHeight = displayMetrics.heightPixels;
                    float videoProportion = (float) videoWidth / (float) videoHeight;
                    float screenProportion = (float) screenWidth / (float) screenHeight;
                    ViewGroup.LayoutParams lp = layout.getLayoutParams();

                    if (videoProportion > screenProportion) {
                        lp.width = screenWidth;
                        lp.height = (int) (videoProportion * (float) screenHeight);
                        lp.height = screenHeight;
                    }
                    layout.setLayoutParams(lp);

                    mediaPlayer.start();
                }
            });
            mTextureView.setFitsSystemWindows(true);

        } catch (IllegalArgumentException | IllegalStateException | SecurityException | IOException e) {
        }
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int i, int i1) {

    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
        return false;
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {

    }
}

レイアウト ファイル:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.awesome.kkh.theelevator.layout.AspectFrameLayout
        android:id="@+id/playMovie_afl"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/control"
        android:layout_centerInParent="true" >

    <TextureView
        android:id="@+id/preview_movie"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:layout_gravity="center"
        />

    </com.awesome.kkh.theelevator.layout.AspectFrameLayout>


    <FrameLayout
        android:id="@+id/control"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:background="@color/color_darker_background">

        <ImageButton
            android:id="@+id/video_play_button"
            android:layout_width="55dp"
            android:layout_height="55dp"
            android:layout_gravity="center"
            android:background="@drawable/circlebutton"
            android:src="@drawable/ic_fa_play" />

    </FrameLayout>

</RelativeLayout>
4

0 に答える 0