denzcoskun / ImageSlideshow

Android image slider.
Apache License 2.0
354 stars 141 forks source link

Feature request - load MP4 video #81

Closed marinsagovac closed 1 year ago

marinsagovac commented 2 years ago

Create pager_video.yml inside layout resource:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <VideoView
        android:id="@+id/videoView"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentEnd="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>

Add inside instantiateItem to check if extension mp4 then load video:

InstantiateItem:


        val itemViewVideo = layoutInflater!!.inflate(R.layout.pager_video, container, false)
        val videoView = itemViewVideo.findViewById<VideoView>(R.id.videoView)

then add after view:


val imgType = imageList!![position].imageUrl!!.toString().substringAfterLast('.', "")

        if (imgType == "mp4") {
            val mediaController = MediaController(itemViewVideo.context)

            val vidAddress = imageList!![position].imageUrl.toString()
            val vidUri = parse(vidAddress)
            videoView.setVideoURI(vidUri);
            videoView.setOnPreparedListener(OnPreparedListener { mp ->
                mp.setVolume(0f, 0f)
                mp.isLooping = true
            })
            videoView.seekTo(1);
            videoView.start();
            mediaController.setAnchorView(videoView);

            container.addView(itemViewVideo)

            return itemViewVideo
        }

Take care to change and increase period of time. Also video should run when UI is visible so need to fine tune.

Hope someone helps.

denzcoskun commented 1 year ago

No it just image slider.