在视频视图中无法从SD卡播放视频

时间:2016-07-20 06:56:08

标签: java android android-studio

RelativeLayout中的VideoView以及在视频视图中打开和播放视频的代码如下所示。通过更改videoview的背景颜色,我可以检查视频的可见性,但视频没有被播放,我可以听到视频的声音,但视频中的内容没有显示。 谢谢。

<RelativeLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:background="#9057ac"
    android:visibility="gone"
    android:id="@+id/videoContainer">
<Button
    android:id="@+id/CancelRecording"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:layout_marginRight="10dp" />
<VideoView
    android:id="@+id/VideoPlayer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#e3e3e3"/>
<Button
    android:id="@+id/UploadButton"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:layout_marginRight="10dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"/>
</RelativeLayout>


public class CameraActivity extends Activity{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.third_activity_viewpager);
    private RelativeLayout videoContainer;
    videoContainer = (RelativeLayout) findViewById(R.id.videoContainer);
    videoContainer.setVisibility(View.VISIBLE);
    String video = file.toString();
                    videoView.setVideoURI(Uri.parse(video));
                    videoView.start();
 //i checked the *file* path is correct no need to worry about Path.
}

1 个答案:

答案 0 :(得分:0)

            Change video video view to textureview and use it 

在xml中:

            <RelativeLayout
        android:id="@+id/preview_video_parent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="62dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:weightSum="1">

            <TextureView
                android:id="@+id/preview_video"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="0.5" />

            <TextureView
                android:id="@+id/preview_video_2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="0.5" />
        </LinearLayout>

        <ImageView
            android:id="@+id/previre_play"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:scaleType="center"
            android:src="@drawable/icn_play_big" />
    </RelativeLayout>

在你的行动中:

    implements TextureView.SurfaceTextureListener        , OnClickListener, OnCompletionListener 

        private TextureView surfaceView;

private ImageView imagePlay;

        surfaceView = (TextureView) findViewById(R.id.preview_video_2);

        surfaceView.setSurfaceTextureListener(this);
                surfaceView.setOnClickListener(this);

                path = getIntent().getStringExtra("your path");

        mediaPlayer = new MediaPlayer();
                mediaPlayer.setOnCompletionListener(this);
            }

            @Override
            protected void onStop() {
                if (mediaPlayer.isPlaying()) {
                    mediaPlayer.pause();
                    imagePlay.setVisibility(View.GONE);
                }
                super.onStop();
            }

            private void prepare(Surface surface) {
                try {
                    mediaPlayer.reset();
                    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                    mediaPlayer.setDataSource(path);
                    mediaPlayer.setSurface(surface);
                    mediaPlayer.setLooping(true);
                    mediaPlayer.prepare();
                    mediaPlayer.seekTo(0);
                } catch (Exception e) {
                }
            }

            @Override
            public void onSurfaceTextureAvailable(SurfaceTexture arg0, int arg1,
                                                  int arg2) {
                prepare(new Surface(arg0));
            }

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

            @Override
            public void onSurfaceTextureSizeChanged(SurfaceTexture arg0, int arg1,
                                                    int arg2) {

            }

            @Override
            public void onSurfaceTextureUpdated(SurfaceTexture arg0) {

            }