2017-09-16 8 views
0

私はどのようにして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です:OK

<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(); 
     } 
     } 

。最初に私は "生の"フォルダを作成し、それに私のビデオを入れます。私はvideoPathを設定し、すべて正常に動作します。 私はビデオをストリーミングしようと決めました。それはコード内のリンクを動作させますが、ランダムなYouTubeリンクは動作しません(なぜか分かりません)。 しかし、本当の問題は:なぜこの2行:

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

は間違っていますか?私はビデオを/storage/emulated/0/myvideoに入れましたが、私はいつもエラーcan't play the videoを受け取ります。私はvideoViewを使用する別の方法を見つけることができなかったので、私は本当にどこに自分のせいであるか分からない。 ああ、マニフェストに私がpermissonsを追加しました:

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

答えて

0

はありません、その行が正しく、それが正常に動作し、これは拡張名を追加しない

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

関連する問題