2017-09-16 3 views
0

J'apprends comment fonctionne videoView. J'ai suivi un tutoriel; voici mon activity_main.xml:Je ne comprends pas pourquoi cette démo videoView simple ne fonctionne pas

<?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> 

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

Ok. D'abord je crée un dossier "brut" unde "res" et y mets ma vidéo. J'ai mis le videoPath et tout fonctionne bien. J'ai donc décidé d'essayer de diffuser une vidéo. Il fonctionne le lien dans le code, mais il ne fonctionne pas un lien aléatoire de YouTube (et je ne comprends pas pourquoi). Mais la vraie question est la suivante: pourquoi ces 2 lignes:

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

ont tort? J'ai mis la vidéo dans le /storage/emulated/0/myvideo mais j'ai toujours l'erreur can't play the video. Je n'ai pas trouvé une manière différente d'utiliser videoView et donc je ne sais vraiment pas où est ma faute. Oh, dans le manifeste, j'ai ajouté les permissons:

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

Répondre

0

essayer ce ne pas ajouter le nom d'extension

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

Pas cette ligne est correcte et il fonctionne très bien. La ligne qui ne fonctionne pas est le 2 que j'ai souligné à la fin, quand j'essaye d'accéder à un dossier dans le stockage interne de mon téléphone. Et là, j'ai essayé de supprimer l'extension, mais cela ne fonctionne pas – Simone