0

I have a video on my server which is encrypted and I want to play that video using exoplayer but I am unable to find any code that answers my question completely. I have no idea how to use customDatasource and how do I exactly attach it exoplayer.

I tried some code samples from the internet but it is giving me unrecognizedformatexception

Abhijit Howal
  • 13
  • 1
  • 4
  • 1
    What kind of encoding are you using for your video? is it dash, normal or any other encoding – p.mathew13 Apr 15 '19 at 12:00
  • I have no idea about video streaming and encoding the video was encrypted by the client and he is asking me to decrypt and stream it on the app which I am developing. He is not from technical background so asking him these questions is of no use. I only know that video is encrypted using a password. And has .encrypted format – Abhijit Howal Apr 15 '19 at 12:03
  • Check whether this relates to your problem: http://blog.moagrius.com/android/android-play-encrypted-video-in-exoplayer/ https://stackoverflow.com/questions/38729220/reproducing-encrypted-video-using-exoplayer – p.mathew13 Apr 15 '19 at 12:05
  • It only explains how to stream file from file system. I want to stream it from server – Abhijit Howal Apr 15 '19 at 12:25
  • Streaming is also simple. you just have to give your server url instead of local url like : Uri uri = Uri.parse(serverUrl); . Now use this uri to build your datasource – p.mathew13 Apr 15 '19 at 12:27
  • I have built the datasource but I am confused about it. How should exactly I attach it to exoplayer? And it also needs an upstream parameter. What exactly is upstream? And how should I provide that parameter to it? – Abhijit Howal Apr 15 '19 at 12:31

1 Answers1

0

Recently I have implemented exoplayer in my project.

First create a fragment or activity

class VideoViewFragment : Fragment() {

    var dataSourceFac: DataSource.Factory? = null

    companion object {
        var nPlayer: ExoPlayer? = null
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_video_view, container, false)
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        getBundledArguments()
        ivCancel.setOnClickListener {
            if (nPlayer != null) {
                nPlayer!!.release()
                nPlayer = null
            }
            activity?.onBackPressed()
        }
    }


    private fun getBundledArguments() {
        if (arguments != null) {
            if (arguments?.getString(Constants.VIDEO_URL) != null)
                playVideo(arguments?.getString(Constants.VIDEO_URL)!!)
            else
                activity?.onBackPressed()
        }
    }

    private fun playVideo(videoUrl: String) {
        val player: ExoPlayer
        if (nPlayer != null) {
            nPlayer!!.release()
            nPlayer = null
        }

        val defaultBandwidthMeter = DefaultBandwidthMeter()
        val dataSourceFactory = DefaultDataSourceFactory(activity, Util.getUserAgent(context, context!!.getString(com.app.tourguide.R.string.app_name)), defaultBandwidthMeter)
        dataSourceFac = dataSourceFactory


        val videoTrackSelectionFactory = AdaptiveTrackSelection.Factory(defaultBandwidthMeter)
        val trackSelector = DefaultTrackSelector(videoTrackSelectionFactory)
        val contentMediaSource = buildMediaSource(Uri.parse(videoUrl))

        val mediaSources = arrayOfNulls<MediaSource>(2) //The Size must change depending on the Uris
        mediaSources[0] = contentMediaSource //uri
        val subtitleSource = SingleSampleMediaSource(Uri.parse("https://www.iandevlin.com/html5test/webvtt/upc-video-subtitles-en.vtt"),
                dataSourceFactory, Format.createTextSampleFormat(null, MimeTypes.TEXT_VTT, Format.NO_VALUE, "en", null),
                C.TIME_UNSET)

        // mediaSources[1] = subtitleSource

        //val mediaSource = MergingMediaSource(mediaSources[0], mediaSources[1])

        player = ExoPlayerFactory.newSimpleInstance(context, trackSelector)
        player.setPlayWhenReady(true)
        playerView.player = player

        player.prepare(contentMediaSource)
        nPlayer = player


        nPlayer?.addListener(object : Player.EventListener {
            override fun onPlaybackParametersChanged(playbackParameters: PlaybackParameters?) {
                Log.d(TAG, "" + playbackParameters)
            }

            override fun onSeekProcessed() {
                Log.d(TAG, "")
            }

            override fun onTracksChanged(trackGroups: TrackGroupArray?, trackSelections: TrackSelectionArray?) {
                Log.d(TAG, "" + trackGroups)
            }

            override fun onPlayerError(error: ExoPlaybackException?) {
                Log.d(TAG, "" + error!!.message)
            }

            override fun onLoadingChanged(isLoading: Boolean) {
                Log.d(TAG, "loading [$isLoading]")
            }

            override fun onPositionDiscontinuity(reason: Int) {
                Log.d(TAG, "" + reason)
            }

            override fun onRepeatModeChanged(repeatMode: Int) {
                Log.d(TAG, "" + repeatMode)
            }

            override fun onShuffleModeEnabledChanged(shuffleModeEnabled: Boolean) {
                Log.d(TAG, "" + shuffleModeEnabled)
            }

            override fun onTimelineChanged(timeline: Timeline?, manifest: Any?, reason: Int) {
                Log.d(TAG, "" + timeline)
            }

            override fun onPlayerStateChanged(playWhenReady: Boolean, playbackState: Int) {
                Log.d(TAG, "" + getStateString(playbackState))
                if (playbackState == Player.STATE_ENDED) {
                    if (nPlayer != null) {
                        nPlayer!!.release()
                        nPlayer = null
                        activity?.onBackPressed()
                    }
                }
            }

        })

    }

    private fun getStateString(state: Int): String {
        when (state) {
            Player.STATE_BUFFERING -> return "B"
            Player.STATE_ENDED -> return "E"
            Player.STATE_IDLE -> return "I"
            Player.STATE_READY -> return "R"
            else -> return "?"
        }
    }

    private fun buildMediaSource(uri: Uri): MediaSource {
        @C.ContentType val type = Util.inferContentType(uri)
        when (type) {
            /*C.TYPE_DASH:
               return new DashMediaSource.Factory(dataSourceFactory).createMediaSource(uri);
            C.TYPE_SS:
               return new SsMediaSource.Factory(dataSourceFactory).createMediaSource(uri);*/
            C.TYPE_HLS -> return HlsMediaSource.Factory(dataSourceFac).createMediaSource(uri)
            C.TYPE_OTHER -> return ExtractorMediaSource.Factory(dataSourceFac).createMediaSource(uri)
            else -> throw IllegalStateException("Unsupported type: $type") as Throwable
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        if (nPlayer != null) {
            nPlayer!!.release()
            nPlayer = null
        }
    }

}









<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/black">

    <com.google.android.exoplayer2.ui.PlayerView
        android:id="@+id/playerView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        app:auto_show="true"
        app:hide_on_touch="true"
        app:resize_mode="zoom"
        app:surface_type="texture_view"
        app:use_controller="true" />

    <ImageView
        android:id="@+id/ivCancel"
        android:layout_width="48dp"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_marginTop="@dimen/_5sdp"
        android:layout_marginEnd="@dimen/_5sdp"
        android:src="@android:drawable/ic_menu_close_clear_cancel" />

</RelativeLayout>
Suraj Bahadur
  • 3,730
  • 3
  • 27
  • 55