androidkotlinexoplayerexoplayer2.xexoplayer-media-item

Unresolved reference: ExoPlayerFactory in ExoPlayerFactory.newSimpleInstance and Type mismatch: inferred type is Uri! but MediaItem was expected in


after updating to the latest build version of ExoPlayer i.e "2.18.1", ExoPlayerFactory.newSimpleInstance showing unresolved reference Error,

Want to reformat this Initialize Function to the latest version of exoplayer without changing its Logic

getting obscured errors in function

 private fun initializeExoPlayer(soundFile: String): ExoPlayer {
        // create the player
        val exoPlayer = ExoPlayerFactory.newSimpleInstance(
            DefaultRenderersFactory(this), DefaultTrackSelector()
        )

        // load the media source
        val dataSource = DefaultDataSourceFactory(this,
            Util.getUserAgent(this, this.getString(R.string.app_name)))
        val mediaSource = ProgressiveMediaSource.Factory(dataSource)
            .createMediaSource(Uri.parse("asset:///$soundFile"))

        // load the media
        Log.d("MAIN", "loading $soundFile")
        exoPlayer.prepare(mediaSource)
        // loop indefinitely
        exoPlayer.repeatMode = Player.REPEAT_MODE_ALL

        return exoPlayer
    }

the errors are

  1. Unresolved reference: ExoPlayerFactory
  2. Type mismatch: inferred type is Uri! but MediaItem was expected in
     .createMediaSource(Uri.parse("asset:///$soundFile"))
  1. Variable expected in
exoPlayer.repeatMode = Player.REPEAT_MODE_ALL

ScreenShot ScreenShot

gradle:


    // ExoPlayer
    api "com.google.android.exoplayer:exoplayer-core:2.18.1"
    api "com.google.android.exoplayer:exoplayer-ui:2.18.1"
    api "com.google.android.exoplayer:extension-mediasession:2.18.1"

TRIED after searching multiple times changed the following function into ->

  private fun initializeExoPlayer(soundFile: String): ExoPlayer {

        // create the player
        val exoPlayer = ExoPlayer.Builder(this).build()

        // load the media source
        val dataSource = DefaultDataSourceFactory(this,
                Util.getUserAgent(this, this.getString(R.string.app_name)))

        val firstAudioUri = Uri.parse("assets:///$soundFile")
        val mediaSource = MediaItem.fromUri(firstAudioUri)

        // load the media
        Log.d("MAIN", "loading $soundFile")
        exoPlayer.addMediaItem(mediaSource)
        exoPlayer.prepare()
        // loop indefinitely
        exoPlayer.repeatMode = Player.REPEAT_MODE_ALL

        return exoPlayer
    }

all the indicated Errors were gone but no media is playing and Variable 'dataSource' is never used

Any help would be highly appreciated.


Solution

  • i Tried this, and its working fine now.

       private fun initializeExoPlayer(soundFile: String): ExoPlayer {
    
    
            // create the player
            val trackSelector = DefaultTrackSelector(this)
            val exoPlayer = ExoPlayer.Builder(this).setTrackSelector(trackSelector).build()
    
            // load the media source
            val dataSource = DefaultDataSource.Factory(this)
    
            val mediaSource = ProgressiveMediaSource.Factory(dataSource)
                .createMediaSource(MediaItem.fromUri(Uri.parse("asset:///$soundFile")))
    
            // load the media
            Log.d("MAIN", "loading $soundFile")
            exoPlayer.setMediaSource(mediaSource)
            exoPlayer.prepare()
          
            // loop indefinitely
            exoPlayer.repeatMode = Player.REPEAT_MODE_ALL
    
            return exoPlayer
        }