【问题标题】:Can't Play Video from SD Card in video view无法在视频视图中播放 SD 卡中的视频
【发布时间】:2016-11-23 06:43:04
【问题描述】:

RelativeLayout 中的VideoView 以及在视频视图中打开和播放视频的代码如下。通过更改 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.
}

【问题讨论】:

    标签: java android android-studio


    【解决方案1】:
                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) {
    
                }
    

    【讨论】:

    • 好吧,我不能给你分数,因为我没有多少分。我必须在这里写下我给你 -100 分严重的解决方案是单行,即这个 videoView.setZOrderOnTop(true);在我实现 videoView.setOnPreparedListener 之前,我需要添加它......!!!
    • 如果你有这样的行为,没有人会帮助你。我把我的整个代码放在这里以寻求帮助。这并不意味着它完全错误。
    • 看起来我是 Android 开发的新手,你粘贴了代码,这让我完全沮丧,我写的代码完全错误,我认为我所做的一切都是无用的。当我更优雅地搜索 Querry 时,这让我很生气,我找到了解决方案。你的代码有点混乱,完全改变了我正在做的事情!!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-09
    • 2014-02-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多