Simple Media Player in Android

huangapple go评论66阅读模式
英文:

Simple Media Player in Android

问题

以下是您提供的代码的翻译部分:

我正在尝试创建一个没有 `MediaPlayer` 类的媒体播放器只是一个非常基本的 VideoView它可以从设备中选择文件并播放它但是我收到了一个错误消息

> 无法播放此视频

以下是我的代码

包名com.phantom.player

```java
package com.phantom.player;

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 文件:

<?xml version="1.0" encoding="utf-8"?>
<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>

这是 logcat 显示的内容(我猜这包含了错误信息):

2020-08-09 12:55:37.585 21894-21894/com.phantom.player W/MediaPlayer:
无法打开 /document/primary:WhatsApp/Media/WhatsApp
Video/VID-20191031-WA0041.mp4
java.io.FileNotFoundException: 没有内容提供程序:/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 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(&quot;*/*&quot;);
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:

&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_height=&quot;match_parent&quot;
xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
android:layout_width=&quot;match_parent&quot;&gt;
&lt;VideoView
android:id=&quot;@+id/videoView&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
tools:layout_editor_absoluteX=&quot;318dp&quot;
tools:layout_editor_absoluteY=&quot;716dp&quot; /&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

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.

答案1

得分: 0

我不明白问题是什么。但当我将onActivityResult更改为以下内容时,问题得以解决:

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

I didn't understand what was the problem.But it was solved when I changed onActivityResult to

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

答案2

得分: 0

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

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();
    }
英文:

Try to set orientation with object of video player

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

huangapple
  • 本文由 发表于 2020年8月9日 15:35:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/63323707.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定