Android中的简单媒体播放器

l7wslrjt  于 2022-11-20  发布在  Android
关注(0)|答案(2)|浏览(165)

I am trying to create a media player without MediaPlayer class, just a very basic VideoView which selects a file from device and plays it. But I am receiving an error
Can't play this video.
Here's my code:

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Toast;
import android.widget.VideoView;

public class PlayerActivity extends AppCompatActivity {

    VideoView videoPlayer;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        videoPlayer = findViewById(R.id.videoView);
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("*/*");
        startActivityForResult(intent, 7);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 7:
                if (resultCode == RESULT_OK) {
                    String PathHolder = data.getData().getPath();
                    Toast.makeText(this, PathHolder, Toast.LENGTH_SHORT).show();
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                    videoPlayer.setVideoURI(Uri.parse(PathHolder));
                    videoPlayer.requestFocus();
                    videoPlayer.start();
                }
        }
    }
}

XML file:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:tools="http://schemas.android.com/tools"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent">
        <VideoView
        android:id="@+id/videoView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:layout_editor_absoluteX="318dp"
        tools:layout_editor_absoluteY="716dp" />
</androidx.constraintlayout.widget.ConstraintLayout>

This is what logcat shows (i guess this contains the error):
2020-08-09 12:55:37.585 21894-21894/com.phantom.player W/MediaPlayer: Couldn't open /document/primary:WhatsApp/Media/WhatsApp Video/VID-20191031-WA0041.mp4 java.io.FileNotFoundException: No content provider: /document/primary:WhatsApp/Media/WhatsApp Video/VID-20191031-WA0041.mp4 at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1680) at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1510) at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1427) at android.media.MediaPlayer.attemptDataSource(MediaPlayer.java:1149) at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1121) at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1145) at android.widget.VideoView.openVideo(VideoView.java:412) at android.widget.VideoView.access$2200(VideoView.java:83) at android.widget.VideoView$7.surfaceCreated(VideoView.java:694) at android.view.SurfaceView.updateSurface(SurfaceView.java:926) at android.view.SurfaceView.windowStopped(SurfaceView.java:315) at android.view.ViewRootImpl.setWindowStopped(ViewRootImpl.java:1973) at android.view.WindowManagerGlobal.setStoppedState(WindowManagerGlobal.java:709) at android.app.Activity.performRestart(Activity.java:8039) at android.app.ActivityThread.handleSleeping(ActivityThread.java:5007) at android.app.ActivityThread.access$2500(ActivityThread.java:268) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2080) at android.os.Handler.dispatchMessage(Handler.java:107) at android.os.Looper.loop(Looper.java:237) at android.app.ActivityThread.main(ActivityThread.java:7807) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1047)
I am not used to content provider.Please help.

wnrlj8wa

wnrlj8wa1#

我不明白是什么问题。但是当我把onActivityResult改为

switch (requestCode) {
            case 7:
                if (resultCode == RESULT_OK) {
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                    videoPlayer.setVideoURI(data.getData());
                    videoPlayer.requestFocus();
                    videoPlayer.start();
                }
        }
ctehm74n

ctehm74n2#

尝试使用视频播放器的对象设置方向

case 7:
            if (resultCode == RESULT_OK) {
                String PathHolder = data.getData().getPath();
                Toast.makeText(this, PathHolder, Toast.LENGTH_SHORT).show();
                videoPlayer.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                videoPlayer.setVideoURI(Uri.parse(PathHolder));
                videoPlayer.requestFocus();
                videoPlayer.start();
            }

相关问题