exoplayer 中的 Media Item 和 Media Source 有什么区别?

Different between Media Item and Media Source in exoplayer?

exoplayer 中的 MediaItemMediaSource 有什么不同,我如何在最新版本的 exoplayer 库 (2.17.1) 中使用 MediaSource?

这是一个简单的项目,如何使用 MediaSource 而不是 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();
        
    }
    
    
    
}

ExoPlayer 文档对此进行了讨论:

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)

和:

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

如果您只想播放媒体项目或创建播放列表,那么您可能只需要使用 MediaItem - 最新的 CodeLabs 中有一些很好的示例:https://developer.android.com/codelabs/exoplayer-intro#0