尽管代码看起来不错,但应用程序无法播放 SD 卡中的视频 [英] App can´t play a video from the SD-Card despite the fact that the code looks fine

查看:54
本文介绍了尽管代码看起来不错,但应用程序无法播放 SD 卡中的视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的代码使用 Android 的视频视图从我的 SD 卡播放视频.我在外部存储根目录中放置了一个名为 Video.mp4 的视频文件.但是当在我的智能手机上启动我的应用程序并使用下面的 Video.java 代码切换到我的视频活动时,我看到一个带有 OK 按钮的通知,说视频无法播放.(德语:视频 kann nicht wiedergegeben werden.")

I use the code below to play a video from my SD-Card using Video View of Android. I placed a video file named Video.mp4 in the external storage root directory. But when starting my App on my Smartphone and switching to my video activity with the code below for Video.java I see a notification with an OK button saying that the video can not be played. (German: "Video kann nicht wiedergegeben werden.")

是否在我的 AndroidManifest.xml 中缺少权限?

Is it a missing permission in my AndroidManifest.xml?

提前感谢您的任何提示和帮助.

Thanks in advance for any hints and help.

package com.noureddine_ouertani.www.wocelli50;

import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.widget.MediaController;
import android.widget.VideoView;


public class Video extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        VideoView v = (VideoView) findViewById(R.id.videoView);

        v.setVideoURI(Uri.parse(Environment.getExternalStorageDirectory().getPath() + "/Video.mp4"));
        //Set media controller buttons
        v.setMediaController(new MediaController(this));
        v.requestFocus();
        v.start(); //start Playback

    }
}

@Alexandre Martin:感谢您的回答.在我的应用程序中嵌入视频没有一般问题.当我创建一个原始文件夹并将视频 testvideo.mp4 放入其中然后替换

@Alexandre Martin: Thanks for your answer. I have no general problem with embedding videos in my app. When I create a raw folder and put the video testvideo.mp4 in it then replace

v.setVideoURI(Uri.parse(Environment.getExternalStorageDirectory().getPath() + "/Video.mp4"));

 v.setVideoURI(Uri.parse("android.resource://com.noureddine_ouertani.www.wocelli50/"+ R.raw.testvideo));

在我的代码中,一切正常,我看到我的视频在我的应用中播放.备注:我不得不将 Video.mp4 重命名为 testvideo.mp4,因为从原始文件夹调用的视频不允许使用大写字母.我的问题只是获取 SD 卡上视频的路径.

in my code, everything works fine and I see my video playing in my app. Remark: I had to rename Video.mp4 to testvideo.mp4 because capital letters are not allowed for videos called from the raw folder. My problem is just to get the path to the video on my SD-card.

@Lonnie Zamora:感谢您的回答.这似乎不是格式或编码问题.testvideo.mp4 只是 Video.mp4 重命名并放置在我的原始文件夹中.它与下面的代码一起玩得很好:

@Lonnie Zamora: Thanks for your answer. It doesn´t seem to be a format or coding problem. testvideo.mp4 is just Video.mp4 renamed and placed in my raw folder. And it plays fine with the code below:

package com.noureddine_ouertani.www.wocelli50;

import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.MediaController;
import android.widget.VideoView;


public class Video extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        VideoView v = (VideoView) findViewById(R.id.videoView);

        v.setVideoURI(Uri.parse("android.resource://com.noureddine_ouertani.www.wocelli50/"+ R.raw.testvideo));
        //Set media controller buttons
        v.setMediaController(new MediaController(this));
        v.requestFocus();
        v.start(); //start Playback

    }
}

@CommonsWare:感谢您的回答.我尝试了您的建议,但没有解决问题.

@CommonsWare: Thanks for your answer. I tried your suggestion but it didn´t solve the problem.

最好的问候,

编辑 2: @CommonsWare 这似乎不是缺少权限或缺少权限请求问题.我实现了以下运行时权限逻辑并看到视频正在播放.不需要权限请求".这对我来说意味着 (Manifest.permission.READ_EXTERNAL_STORAGE == PackageManager.PERMISSION_GRANTED) 是真的

EDIT 2: @CommonsWare It doesn´t seem to be a missing permission or a missing permission request issue. I implemented the following runtime permission logic and see "Video is playing. No permissioon request was needed." which means to me that (Manifest.permission.READ_EXTERNAL_STORAGE == PackageManager.PERMISSION_GRANTED) was true

package com.noureddine_ouertani.www.wocelli50;

import android.Manifest;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;

public class Video extends AppCompatActivity {

    final int REQUEST_READ_EXTERNAL_STORAGE = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video);


        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {



            // Should we show an explanation?
            if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_EXTERNAL_STORAGE)) {
                Toast.makeText(getApplicationContext(), "Show explanation for requesting the permission to read the SD-card.", Toast.LENGTH_SHORT).show();
                // Show an expanation to the user *asynchronously* -- don't block
                // this thread waiting for the user's response! After the user
                // sees the explanation, try again to request the permission.

            } else {

                // No explanation needed, we can request the permission.
                Toast.makeText(getApplicationContext(), "No explanation needed, we can request the permission.", Toast.LENGTH_SHORT).show();
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_READ_EXTERNAL_STORAGE);

                // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
                // app-defined int constant. The callback method gets the
                // result of the request.
        }
        } else {
            playmyvideo();
            Toast.makeText(getApplicationContext(), "Video is playing. No permissioon request was needed.", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case REQUEST_READ_EXTERNAL_STORAGE: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    playmyvideo();
                    Toast.makeText(getApplicationContext(), "Video is playing after permissioon was granted.", Toast.LENGTH_SHORT).show();

                } else {
                    Toast.makeText(getApplicationContext(), "permission denied,Disable the functionality that depends on this permission.", Toast.LENGTH_SHORT).show();
                    // permission denied, boo! Disable the
                    // functionality that depends on this permission.
                }
                return;
            } 

            // other 'case' lines to check for other
            // permissions this app might request
        }
    }

    public void playmyvideo(){

        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        VideoView v = (VideoView) findViewById(R.id.videoView);
        v.setVideoURI(Uri.parse(Environment.getExternalStorageDirectory().getPath() + "/testvideo.mp4"));
        //v.setVideoURI(Uri.parse("android.resource://com.noureddine_ouertani.www.wocelli50/"+ R.raw.testvideo));

        v.setMediaController(new MediaController(this));
        v.requestFocus();
        v.start(); //start Playback
        }
    }

推荐答案

解决方案 @all访问外部 SD 卡的解决方案:

SOLUTION @all Solution to access the external SD-card:

v.setVideoURI(Uri.parse("/storage/sdcard1/testvideo.mp4")); 

代替

v.setVideoURI(Uri.parse(Environment.getExternalStorageDirectory().getPath() + "/testvideo.mp4")); 

原因:

Environment.getExternalStorageDirectory().getPath() == "/storage/emulated/0" 

Environment.getExternalStorageDirectory().getPath() 提供到我的智能手机 (HUAWEI P8) 上的内部存储芯片的路径,该芯片像 SD 卡一样安装,但不是外部 SD 卡不过.

Environment.getExternalStorageDirectory().getPath() delivers the path to an internal storage chip on my smartphone (HUAWEI P8) that is mounted like an SD-card but is not the external SD-card though.

这篇关于尽管代码看起来不错,但应用程序无法播放 SD 卡中的视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆