简体   繁体   中英

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!

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

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

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

在此处输入图片说明

这些解决方案都不适合我,但在这里您可以找到如何从R.raw文件夹中获取 URI: https : //stackoverflow.com/a/8616187/8187578

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM