user10517855
user10517855

Reputation:

Android cannot play video in videoview

I am a 11 year old coder and I am getting this error on my phone when I try to run the video view code so I can play a video. The error is,"Sorry, this video cannot be played." My video I downloaded was in the raw folder under resources and the code and xml are all fine. The video format of which I am using is a mp4. Heres my code:

package com.example.lenovouser.video;


import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    VideoView videoView = findViewById(R.id.video_view);

    Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.green_and_yellow_popsicle);
    videoView.setVideoURI(uri);
    videoView.setMediaController(new MediaController(this));
    videoView.start();


}

}

And here is my xml code:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.lenovouser.video.MainActivity">

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <VideoView
        android:id="@+id/video_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</FrameLayout>

</android.support.constraint.ConstraintLayout>

Thanks for your time!

Upvotes: 0

Views: 3183

Answers (4)

Kebab Krabby
Kebab Krabby

Reputation: 1699

None of these solutions worked for me, but here you can found out how to get an URI from R.raw folder: https://stackoverflow.com/a/8616187/8187578

Upvotes: 0

Arunmozhi Nambi
Arunmozhi Nambi

Reputation: 41

I don't see the extension of the video file. I am not sure if you can play any video from the raw, instead can you please create a assets folder an place the video file there.

videoView.setVideoPath("file:///assets/myvideo.mp4");  
videoView.start();

Also make sure you are using the video format specified in the android support documentation: https://developer.android.com/guide/topics/media/media-formats#recommendations

Upvotes: 2

Fr099y
Fr099y

Reputation: 774

Maybe your phone`s API level under 4.1 or try another video with different format.

enter image description here

Upvotes: 0

SamHoque
SamHoque

Reputation: 3154

try using this instead this uses the raw URI instead of R.raw check this post if you are still having trouble How to play video from raw folder with Android device?

String videoPath = "android.resource://" + getPackageName() + "/raw/green_and_yellow_popsicle";
videoView.start();

how ever if you still wanna use R.raw use the code below,

Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.green_and_yellow_popsicle);
videoView.setVideoURI(uri);
videoView.setMediaController(new MediaController(this));
videoView.start();

Upvotes: 0

Related Questions