Issue
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):
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.
Solution
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();
}
}
Answered By - ADP
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.