Search code examples
javaandroidexoplayerexoplayer2.x

Different between Media Item and Media Source in exoplayer?


Whats different between MediaItem and MediaSource in exoplayer and how i can use MediaSource in in the latest version of exoplayer library (2.17.1) ?

This is simple project how use MediaSource instead of MediaItem ?

package com.agono.exoplayer;

import ...


public class MainActivity extends AppCompatActivity {
    
    private Context ctx;
    private StyledPlayerView playerView;
    private Button button1;
    private boolean isShowingTrackSelectionDialog;
    private DefaultTrackSelector trackSelector;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String videoUri=getIntent().getStringExtra("url");
        
        trackSelector = new DefaultTrackSelector(/* context= */ this);
        playerView = findViewById(R.id.test);
        button1= findViewById(R.id.button1);
        ExoPlayer player = new ExoPlayer.Builder(this)
        .setTrackSelector(trackSelector)
        .build();
        MediaItem mediaItem = MediaItem.fromUri(videoUri);
        // Attach player to the view.
        playerView.setPlayer(player);
        // Set the media item to be played.
        player.setMediaItem(mediaItem);
        // Prepare the player.
        player.prepare();
        
    }
    
    
    
}

Solution

  • The ExoPlayer documentation discusses this:

    In ExoPlayer every piece of media is represented by a MediaItem. However internally, the player needs MediaSource instances to play the content. The player creates these from media items using a MediaSource.Factory.

    (https://exoplayer.dev/media-sources.html)

    and:

    The playlist API is based on MediaItems, which can be conveniently built using MediaItem.Builder. Inside the player, media items are converted into playable MediaSources by a MediaSource.Factory

    If all you want is to play a media item or create a playlist then you probably only need to use MediaItem - there are some good examples in the latest CodeLabs: https://developer.android.com/codelabs/exoplayer-intro#0