我无法弄清楚为什么这个简单的videoView演示不起作用

时间:2017-09-16 11:15:58

标签: java android android-videoview

我正在学习videoView的工作原理。我按照教程;这是我的activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.simoc.videoviewdemo.MainActivity"
tools:layout_editor_absoluteY="81dp"
tools:layout_editor_absoluteX="0dp">

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginLeft="0dp"
    android:layout_marginRight="0dp"
    android:layout_marginTop="0dp"
    android:layout_marginBottom="0dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent">


    <VideoView
        android:id="@+id/videoView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"/>
</RelativeLayout>

这是我的mainActivity.java:

<package com.simoc.videoviewdemo;
import android.net.Uri;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.VideoView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    VideoView myVideoView = (VideoView) findViewById(R.id.videoView);
    myVideoView.setVideoPath("android.resource://"+ getPackageName()+"/"+R.raw.myvideo.3gp);

//myVideoView.setVideoURI(Uri.parse("http://archive.org/download/ksnn_compilation_master_the_internet/ksnn_compilation_master_the_internet_512kb.mp4"));
            //String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/myvideo.3gp";
            //myVideoView.setVideoPath(path);
            //Log.i("MY_PATH: ", path);
            myVideoView.start();
        }
        }

确定。首先,我创建一个“原始”文件夹unde“res”并将我的视频放入其中。我已经设置了videoPath,一切正常。 所以我决定尝试流式传输视频。它适用于代码中的链接,但它无法使用随机的YouTube链接(我不明白为什么)。 但真正的问题是:为什么这两行:

String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/myvideo.3gp";
        myVideoView.setVideoPath(path);

错了?我将视频放在/storage/emulated/0/myvideo中,但我总是收到错误can't play the video。我没有找到使用videoView的不同方式,所以我真的不知道我的错在哪里。 哦,在清单中我添加了许可证:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />

1 个答案:

答案 0 :(得分:0)

试试这个不要添加扩展名

VideoView myVideoView = (VideoView) findViewById(R.id.videoView);
String path = "android.resource://" + getPackageName() + "/" + R.raw.myvideo;
myVideoView.setVideoURI(Uri.parse(path));
myVideoView.start();