Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement Exoplayer 2.11.1 in android?

i am trying to implement exoplayer this is my exoplayer version

implementation 'com.google.android.exoplayer:exoplayer:2.11.1'

i am creating a music player app and i don't know anything about exoplayer i am trying to implement exoplayer from last 2 days but it's not working. i couldn't understand anything in the official documentation .

i find many example and tutorial but it's all about playing video using exoplayer. many example's are using deprecated methods.

I am trying to implement using this tutorial but many of methods are deprecated so it's not working EX.

simpleExoplayer = ExoPlayerFactory.newSimpleInstance(
            DefaultRenderersFactory(this),
            DefaultTrackSelector(adaptiveTrackSelectionFactory),
            DefaultLoadControl()
        )

can anyone suggest me where to start or how do i build music streaming app using latest version of exoplayer.

Any help would be highly appreciated.

like image 515
Chirag Patel Avatar asked Dec 21 '19 20:12

Chirag Patel


2 Answers

Until 2.15.0 version, you can create SimpleExoPlayer instance as the following :

SimpleExoPlayer.Builder(this)
            .setMediaSourceFactory(mediaSourceFactory)
            .build() 

With the 2.16.0 version, SimpleExoPlayer is deprecated, you should use ExoPlayer instance instead. You can create it as the following :

ExoPlayer.Builder(this)
            .setMediaSourceFactory(mediaSourceFactory)
            .build()

https://exoplayer.dev/doc/reference/com/google/android/exoplayer2/SimpleExoPlayer.Builder.html

like image 83
oiyio Avatar answered Oct 04 '22 21:10

oiyio


For Java Guys (Long Live Java)

In Activity

private PlayerView epPlayerView = findViewById(R.id.design_reference);

The Public Function

public static void runExoPlayer(PlayerView epPlayerView,
                                String url,
                                Context context) {

    Uri videoUri = Uri.parse(url);

    SimpleExoPlayer  exoPlayer = new SimpleExoPlayer.Builder(context).build();
    epPlayerView.setPlayer(exoPlayer);

    MediaItem mediaItem = MediaItem.fromUri(videoUri);
    exoPlayer.clearMediaItems();
    exoPlayer.setMediaItem(mediaItem);
    exoPlayer.prepare();
    exoPlayer.setPlayWhenReady(true);

}

Build Gradle

   // Exo Media Player
    implementation 'com.google.android.exoplayer:exoplayer:2.15.1'
    implementation 'com.google.android.exoplayer:exoplayer-core:2.15.1'
    implementation 'com.google.android.exoplayer:exoplayer-ui:2.15.1'
    implementation 'com.google.android.exoplayer:exoplayer-dash:2.15.1'
    implementation 'com.google.android.exoplayer:exoplayer-hls:2.15.1'
    implementation 'com.google.android.exoplayer:exoplayer-smoothstreaming:2.15.1'
    androidTestImplementation 'androidx.test:rules:1.4.0'
like image 29
DragonFire Avatar answered Oct 04 '22 22:10

DragonFire