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

问题描述:

我在学习videoView的工作原理。我跟着一个教程;这是我的activity_main.xml中:我无法弄清楚为什么这个简单的videoView演示不起作用

<?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链接(我不明白为什么)。 但真正的问题是:为什么这2行:

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" /> 

尝试这种不添加扩展名

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

没有这一行是正确的,它工作正常。当我尝试访问手机内部存储器中的文件夹时,无法使用的行是最后加下划线的2。在那里我试图删除扩展名,但它不起作用 – Simone