Search code examples
androidkotlinandroid-videoview

Can't play this video. video view in android Studio getting error


I am trying to play a video in Android studio using video View by giving the path it is stored in raw folder you can check it in the screenshot but on the emulator, it shows me can't play this video I even tried other answers to similar questions to this this is activity_main.xml

<VideoView
    android:id="@+id/testView"
    android:layout_width="wrap_content"
    android:layout_height="308dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.574"/>

this is MainActivity.kt

package com.example.video_player

import android.net.Uri
import android.net.Uri.*
import android.widget.MediaController
import android.widget.VideoView
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)


        val videoView = findViewById<VideoView>(R.id.testView);
        //Creating Media Controller
        val mediaController = MediaController(this)
        mediaController.setAnchorView(videoView)
        //specify the location of media file
        val uri:Uri = parse(
            "android.resources://" + packageName
                    + "/raw/test"
        )
        //Setting MediaController and URI, then starting the videoView
        videoView.setMediaController(mediaController)
        videoView.setVideoURI(uri)
        videoView.requestFocus()
        videoView.start()



    }
}

image of error


Solution

  • The problem should be here:

    val uri:Uri = parse(
        "android.resources://" + packageName
                + "/raw/test"
    )
    

    The above way may not be able to retrieve the targeted video. Try the following instead:

    val uri:Uri = parse("android.resource://" + packageName + "/" + R.raw.test)