首页 > 解决方案 > 有没有一种简单的方法可以让 android VideoView 播放 .mp4 文件?

问题描述

我正在尝试播放通过我的应用程序录制的 .mp4 文件,并且希望在 VideoView 上播放该文件。我尝试了很多选项,我收到一个异常说无法打开文件,然后是一个找不到文件的异常。我已经设置了我所有的文件权限。有什么方法可以让我知道 .mp4 文件的存储位置吗?任何帮助将不胜感激。我的代码:

package adi.app.thani;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.VideoView;
import androidx.annotation.Nullable;

import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;

public class Play extends Activity {
    MediaPlayer player;
    int count;
    String talam;
    HashMap<Integer, Integer> adi;
    HashMap<Integer, Integer> mishra;
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.play);
        final FloatingActionButton play = findViewById(R.id.floatingActionButton2);
        talam = getIntent().getStringExtra(Intent.EXTRA_INDEX);
        count = 0;
        mishra = new HashMap<Integer, Integer>();
        mishra.put(0, R.mipmap.realhand_foreground);
        mishra.put(1, R.mipmap.hello);
        mishra.put(2, R.mipmap.hello);
        mishra.put(3, R.mipmap.realhand_foreground);
        mishra.put(4, R.mipmap.hello);
        mishra.put(5, R.mipmap.realhand_foreground);
        mishra.put(6, R.mipmap.hello);
        mishra.put(7, R.mipmap.seven_foreground);
        mishra.put(8, R.mipmap.six_foreground);
        mishra.put(9, R.mipmap.five_foreground);
        mishra.put(10, R.mipmap.four_foreground);
        mishra.put(11, R.mipmap.three_foreground);
        mishra.put(12, R.mipmap.two_foreground);
        mishra.put(13, R.mipmap.one_foreground);
        adi = new HashMap<Integer, Integer>();
        adi.put(0, R.mipmap.realhand_foreground);
        adi.put(1, R.mipmap.realpinky_foreground);
        adi.put(2, R.mipmap.realring_foreground);
        adi.put(3, R.mipmap.realmiddle_foreground);
        adi.put(4, R.mipmap.realhand_foreground);
        adi.put(5, R.mipmap.realturn_foreground);
        adi.put(6, R.mipmap.realhand_foreground);
        adi.put(7, R.mipmap.realturn_foreground);
        adi.put(8, R.mipmap.eight_foreground);
        adi.put(9, R.mipmap.seven_foreground);
        adi.put(10, R.mipmap.six_foreground);
        adi.put(11, R.mipmap.five_foreground);
        adi.put(12, R.mipmap.four_foreground);
        adi.put(13, R.mipmap.three_foreground);
        adi.put(14, R.mipmap.two_foreground);
        adi.put(15, R.mipmap.one_foreground);
        final VideoView vid = findViewById(R.id.videoView2);
        String suffix = getIntent().getStringExtra(Intent.EXTRA_COMPONENT_NAME);
        final int bpm = getIntent().getIntExtra(Intent.EXTRA_TEXT,80);
        final String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).getAbsolutePath() + File.separator + suffix + ".mp4";
        Uri uri = Uri.parse(path);
        vid.setVideoURI(uri);
        vid.setMediaController(new MediaController(this));
        play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (count == 0) {
                    vid.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                        @Override
                        public void onPrepared(MediaPlayer mp) {
                            vid.start();
                        }
                    });
                } else {
                    vid.pause();
                }
            }
       });
        vid.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                finish();
            }
        });
    }
    protected void onStop() {
        super.onStop();
        if (player != null) {
            player.release();
            player = null;
        }
    }
}

日志输出:

04/24 17:41:15: Launching 'app' on motorola moto g(6).
$ adb shell am start -n "adi.app.thani/adi.app.thani.MainActivity" -a 
android.intent.action.MAIN -c android.intent.category.LAUNCHER
Connected to process 17612 on device 'motorola-moto_g_6_-ZY322R4GXD'.
Capturing and displaying logcat messages from application. This 
behavior can be disabled in the "Logcat output" section of the 
"Debugger" settings page.
W/adi.app.thani: Accessing hidden method 
Landroid/graphics/drawable/Drawable;- 
>getOpticalInsets()Landroid/graphics/Insets; (light greylist, linking)
Accessing hidden field Landroid/graphics/Insets;->left:I (light 
greylist, linking)
Accessing hidden field Landroid/graphics/Insets;->right:I (light 
greylist, linking)
Accessing hidden field Landroid/graphics/Insets;->top:I (light 
greylist, linking)
Accessing hidden field Landroid/graphics/Insets;->bottom:I (light 
greylist, linking)
W/System: A resource failed to call close. 
I/chatty: uid=10007(adi.app.thani) FinalizerDaemon identical 1 line
W/System: A resource failed to call close. 
W/adi.app.thani: Accessing hidden method Landroid/view/View;- >computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (light greylist, reflection)
W/adi.app.thani: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (light greylist, reflection)
W/adi.app.thani: Accessing hidden method Landroid/widget/TextView;->getTextDirectionHeuristic()Landroid/text/TextDirectionHeuristic; (light greylist, linking)
D/OpenGLRenderer: Skia GL Pipeline
I/Adreno: QUALCOMM build                   : dd44a11, I3cb150eb74
Build Date                       : 02/28/19
OpenGL ES Shader Compiler Version: EV031.25.03.02
Local Branch                     : 
Remote Branch                    : 
Remote Branch                    : 
Reconstruct Branch               : 
I/Adreno: Build Config                     : S L 6.0.7 AArch32
I/Adreno: PFP: 0x005ff110, ME: 0x005ff066
I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasHDRDispla y retrieved: 0
I/OpenGLRenderer: Initialized EGL, version 1.4
D/OpenGLRenderer: Swap behavior 2
W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@6475b9b
W/MediaPlayer: Couldn't open /storage/emulated/0/Music/Thani64.mp4: java.io.FileNotFoundException: No content provider: /storage/emulated/0/Music/Thani64.mp4
D/MediaPlayerNative: getMetadata`

标签: javaandroid

解决方案


FileNotFoundException 在尝试打开由指定路径名表示的文件失败时引发...

确保:

  1. 您的文件路径必须正确!(检查错别字)
  2. 您的文件必须存在于您指定的路径中

希望你能尽快解决你的问题!

参考:https ://developer.android.com/reference/java/io/FileNotFoundException


推荐阅读