天天看點

VideoView 播放sd卡目錄下視訊的路徑問題

以下是視訊播放源代碼:

package com.example.c7contentprovider;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;

import java.io.File;
import java.io.IOException;

/**
 * Created by 鄧蘇桃 on 2017/7/4.
 */

public class VedioPlayerActivity extends AppCompatActivity implements View.OnClickListener {
    private VideoView videoView;
    MediaController mediaController;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.vedioplayer_main);
        videoView= (VideoView) findViewById(R.id.video_view);
        Button play = (Button) findViewById(R.id.vedio_play);
        Button pause = (Button) findViewById(R.id.vedio_pause);
        Button replay = (Button) findViewById(R.id.vedio_replay);
        play.setOnClickListener(this);
        pause.setOnClickListener(this);
        replay.setOnClickListener(this);
        if (ContextCompat.checkSelfPermission(VedioPlayerActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(VedioPlayerActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, );
        } else {
            initVideoPath();
        }
    }

    private void initVideoPath() {
        File file=new File("storage/sdcard1/shiping.mp4");
        if(file.exists())
        {
//            Toast.makeText(this, "have found video", Toast.LENGTH_SHORT).show();
            videoView.setVideoPath(file.getAbsolutePath());
//            mediaController=new MediaController(this);
//            videoView.setMediaController(mediaController);
//            mediaController.setMediaPlayer(videoView);
//            videoView.requestFocus();
//            videoView.start();
        }
        else
        {
            Toast.makeText(this, file.getAbsolutePath(), Toast.LENGTH_SHORT).show();
        }
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode)
        {
            case :
                if (grantResults.length>&&grantResults[]==PackageManager.PERMISSION_GRANTED)
                {
                    initVideoPath();
                }
                else
                {
                    Toast.makeText(this, "You Denied the Permission", Toast.LENGTH_SHORT).show();
                    finish();
                }
                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.vedio_play:
                if (!videoView.isPlaying()){
                    videoView.start();
                }
                break;
            case R.id.vedio_pause:
                if (videoView.isPlaying()){
                    videoView.pause();
                }
                break;
            case R.id.vedio_replay:
                if (videoView.isPlaying()){
                    videoView.resume();
                }
                break;
            default:
                break;
        }

    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (videoView!=null)
        {
            videoView.suspend();
        }
    }
}
           

關鍵問題在這裡:

private void initVideoPath() {
        File file=new File("storage/sdcard1/shiping.mp4");
        ...
        ...
           

按照《第一行代碼》第八章給的代碼應該寫成這樣:

File file=new File(Environment.getExternalStorageDirectory(),"shiping.mp4");
           

兩者差別在于:

Environment.getExternalStorageDirectory()的實際檔案路徑是:storage/sdcard0/,而如果這樣寫則會顯示找不到s**hiping.mp4**檔案。網上百度才發現,sdcard0是内置存儲,一般是手機U盤,sdcard1是外置存儲,隻手機上的sd卡。是以如果使用Environment.getExternalStorageDirectory(),應該把檔案放在手機U盤上。