Sidharth
Sidharth

Reputation: 81

Unable to play Multiple Audio files using Latest Exoplayer(2.18.1) in Kotlin

I want to implement the Simple App that can play multiple asset audio files together.

same as soft sound but with the latest build gradle of Exoplayer i.e, 2.18.1

it has only two kt classes MainActivity.kt, PlayerService.kt

but most of the used ExoPlayer classes were deprecated in it.

PlyerService.kt

 private fun initializeExoPlayer(soundFile: String): SimpleExoPlayer {
        // 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 = ExtractorMediaSource.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
    }

USING

implementation 'com.google.android.exoplayer:exoplayer-core:2.8.4'
                                   **TRIED**

managed to omit flashing errors by changing the function into

 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()
        exoPlayer.play()
        // loop indefinitely
        exoPlayer.repeatMode = Player.REPEAT_MODE_ALL

        return exoPlayer
    }

using

api "com.google.android.exoplayer:exoplayer-core:2.18.1"

all deprecated indications errors were gone but NO MEDIA IS PLAYING

Please guide me on how to change the upper function correctly Any help would be highly appreciated.

Upvotes: 1

Views: 879

Answers (1)

Suraj Bahadur
Suraj Bahadur

Reputation: 3928

After checking the code that you have shared with me. You need to add a PlayerService class in the AndroidManifest.xml file.

<service
            android:name=".PlayerService"
            android:enabled="true"
            android:exported="false" />

Upvotes: 1

Related Questions