Android ExoPlayer Listener无限循环

时间:2016-04-22 12:50:48

标签: android loops listener infinite-loop exoplayer

我想将ExoPlayer与SurfaceView和SeekBar一起使用。我从ExoPlayer演示中获取的buildTrackName函数。我想使用SeekBar来改变播放器的位置,使用listView来显示我视频的所有音轨。当我使用ExoPlayer函数来获取播放器持续时间或从视频中获取所有音轨时,这些函数对于getDuration()返回-1,对于getTrackCount()返回0。我试图将这些函数放在代码的不同部分,但它们仅适用于ExoPlayer.Listener的onPlayerStateChanged方法。但!在这种情况下,在所有函数之后,程序进入无限循环。

public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback{

private String videoPath = "myVideoPath";
private SurfaceView surfaceView;
private SeekBar seekBar;
private ListView listView;
private ArrayList<String> trackList;
private ArrayAdapter<String> arrayAdapter;
private ExoPlayer exoPlayer;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
seekBar = (SeekBar) findViewById(R.id.seekBar);
listView = (ListView) findViewById(R.id.listView);

trackList = new ArrayList<>();
arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, audioList);
listView.setAdapter(arrayAdapter);

MediaCodecSelector mediaCodecSelector = MediaCodecSelector.DEFAULT;

Uri uri = Uri.parse(videoPath);
DataSource dataSource = new DefaultUriDataSource(this, "test");
ExtractorSampleSource extractorSampleSource = new ExtractorSampleSource(
        uri, dataSource, new DefaultAllocator(64*1024), 256*64*1024,null);
MediaCodecVideoTrackRenderer videoRenderer = new MediaCodecVideoTrackRenderer(this,extractorSampleSource,mediaCodecSelector,MediaCodec.VIDEO_SCALING_MODE_SCALE_TO_FIT);
MediaCodecAudioTrackRenderer audioRenderer = new MediaCodecAudioTrackRenderer(
        extractorSampleSource, mediaCodecSelector);

exoPlayer = ExoPlayer.Factory.newInstance(2);

exoPlayer.sendMessage(videoRenderer, MediaCodecVideoTrackRenderer.MSG_SET_SURFACE, surfaceView.getHolder().getSurface());

exoPlayer.prepare(videoRenderer,audioRenderer);

exoPlayer.setPlayWhenReady(true);

exoPlayer.addListener(new ExoPlayer.Listener() {
    @Override
    public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
        if (playbackState == ExoPlayer.STATE_READY){
            seekBar.setMax((int)exoPlayer.getDuration());         
            int trackCount = exoPlayer.getTrackCount(1);
            for (int i = 0; i < trackCount; i++) {
                trackList.add(buildTrackName(exoPlayer.getTrackFormat(1, i)));
            }
        } //infinite loop after this place
    }

    @Override
    public void onPlayWhenReadyCommitted() {

    }

    @Override
    public void onPlayerError(ExoPlaybackException error) {

    }
});

}

///
}

1 个答案:

答案 0 :(得分:0)

ExoPlayer 2标志支持使用LoopingMediaSource

无缝循环播放视频