ExoPlayerImplInternal:渲染器错误无法播放视频android

时间:2018-06-06 06:07:12

标签: android exoplayer2.x

我经常在我的所有4个应用中使用Google Exoplayer2库。但是我在播放视频方面面临一些问题,忽略了错误:

ExoPlayerImplInternal: Renderer error.                                                                                                                                
com.google.android.exoplayer2.ExoPlaybackException                                                                                                                                     
    at com.google.android.exoplayer2.audio.MediaCodecAudioRenderer.processOutputBuffer(MediaCodecAudioRenderer.java:382)                                                                                                                           
    at com.google.android.exoplayer2.mediacodec.MediaCodecRenderer.drainOutputBuffer(MediaCodecRenderer.java:955)                                                                                                                                        
    at com.google.android.exoplayer2.mediacodec.MediaCodecRenderer.render(MediaCodecRenderer.java:510)                                                                                                                                            
    at com.google.android.exoplayer2.ExoPlayerImplInternal.doSomeWork(ExoPlayerImplInternal.java:479)                                                                                                                                           
    at com.google.android.exoplayer2.ExoPlayerImplInternal.handleMessage(ExoPlayerImplInternal.java:308)                                                                                                                                            
    at android.os.Handler.dispatchMessage(Handler.java:101)                                                                                                                                         
    at android.os.Looper.loop(Looper.java:164)                                                                                                                                         
    at android.os.HandlerThread.run(HandlerThread.java:65)                                                                                                                                         
Caused by: com.google.android.exoplayer2.audio.AudioTrack$InitializationException: AudioTrack init failed: 0, Config(48000, 12, 92288)                                                                                                                                             
    at com.google.android.exoplayer2.audio.AudioTrack.checkAudioTrackInitialized(AudioTrack.java:1358)                                                                                                                                             
    at com.google.android.exoplayer2.audio.AudioTrack.initialize(AudioTrack.java:648)                                                                                                                                             
    at com.google.android.exoplayer2.audio.AudioTrack.handleBuffer(AudioTrack.java:722)                                                                                                                                             
    at com.google.android.exoplayer2.audio.MediaCodecAudioRenderer.processOutputBuffer(MediaCodecAudioRenderer.java:376)

如果你现在想要exoplayer的代码初始化see this

1 个答案:

答案 0 :(得分:0)

尝试此代码,并在此代码中读取assets文件夹中的视频路径。你可以直接传递视频网址。并在此代码中播放多个视频。

public class MainActivity extends AppCompatActivity {
private SimpleExoPlayer simpleExoPlayer;
private SimpleExoPlayerView simpleExoPlayerView;
private AdaptiveTrackSelection.Factory factory;

private ProgressBar progressBar;
private TrackSelector trackSelector;
private LoadControl loadControl;
private DefaultBandwidthMeter defaultBandwidthMeter;
private LoopingMediaSource loopingMediaSource;
private int lastSongIndex = 0;
private List<String> videoList = new ArrayList<>();

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

/**
 * this method initialized view controls.
 */
private void initView() {
    simpleExoPlayerView = findViewById(R.id.videoPlayer);
    progressBar = findViewById(R.id.amPrgbrLoading);
    listAssetFiles("");

}

private boolean listAssetFiles(String path) {

    String[] list;
    try {
        list = getAssets().list(path);
        if (list.length > 0) {
            // This is a folder
            for (String file : list) {
                if (!listAssetFiles(path + "/" + file))
                    return false;
                else {
                    // This is a file
                    // TODO: add file name to an array list
                    if (file.contains(".mp4") || file.contains(".mp3")) {
                        videoList.add(file);
                    }
                }
            }
        }
    } catch (IOException e) {
        return false;
    }

    return true;
}

/**
 * this method play aut
 */
private void playMedia() {
    defaultBandwidthMeter = new DefaultBandwidthMeter();
    factory = new AdaptiveTrackSelection.Factory(defaultBandwidthMeter);
    trackSelector = new DefaultTrackSelector(factory);
    loadControl = new DefaultLoadControl();
    simpleExoPlayer = ExoPlayerFactory.newSimpleInstance(this, trackSelector, loadControl);
    simpleExoPlayerView.setPlayer(simpleExoPlayer);
    DefaultBandwidthMeter dBandwidthMeter = new DefaultBandwidthMeter();
    DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(this,
            Util.getUserAgent(this, "com.exoplayerdemo"), dBandwidthMeter);
    ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
    // below line you can pass video url
    MediaSource mediaSource = new ExtractorMediaSource(Uri.parse("file:///android_asset/" + videoList.get(lastSongIndex)),
            dataSourceFactory, extractorsFactory, null, null);

    loopingMediaSource = new LoopingMediaSource(mediaSource);
    simpleExoPlayer.prepare(mediaSource);
    simpleExoPlayer.setPlayWhenReady(true);
    simpleExoPlayer.addListener(new ExoPlayer.EventListener() {
        @Override
        public void onTimelineChanged(Timeline timeline, Object manifest) {

        }

        @Override
        public void onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray trackSelections) {

        }

        @Override
        public void onLoadingChanged(boolean isLoading) {

        }

        @Override
        public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
            if (playbackState == ExoPlayer.STATE_BUFFERING) {
                progressBar.setVisibility(View.VISIBLE);
            } else {
                progressBar.setVisibility(View.GONE);
            }
            if (playbackState == ExoPlayer.STATE_ENDED) {
                lastSongIndex++;
                playMedia();
            }
        }

        @Override
        public void onPlayerError(ExoPlaybackException error) {
            if (!TextUtils.isEmpty(error.getMessage())) {
                Log.d("ERROR::", error.getMessage());
                Toast.makeText(getApplicationContext(), "Error::" + error.getMessage(), Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onPositionDiscontinuity() {
            int latestSongIndex = simpleExoPlayer.getCurrentWindowIndex();
            if (latestSongIndex != lastSongIndex) {
                lastSongIndex = latestSongIndex;
            }
        }
    });
}

@Override
protected void onResume() {
    super.onResume();
    playMedia();
}

@Override
protected void onStop() {
    super.onStop();
    stopMedia();
}

@Override
protected void onPause() {
    super.onPause();
    stopMedia();
}

private void stopMedia() {
    simpleExoPlayer.stop();
    simpleExoPlayer.release();
}
}

和像这样的xml代码..

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">

<com.google.android.exoplayer2.ui.SimpleExoPlayerView
    android:id="@+id/videoPlayer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:resize_mode="fill"
    app:rewind_increment="30000"
    app:fastforward_increment="30000"
    />
<ProgressBar
    android:id="@+id/amPrgbrLoading"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_centerInParent="true" />

</RelativeLayout>