Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExoPlayerImplInternal: Renderer error can't play video android

I am regularly using google Exoplayer2 library in all my 4 apps. But I have facing some problem to play video its omits error:

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)

If you want now my code for exoplayer initializing see this.

like image 479
Jayesh Rathod Avatar asked Nov 08 '22 06:11

Jayesh Rathod


1 Answers

Try this code and also in this code read video path in assets folder. you can pass direct video url. and in this code play multiple video.

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();
}
}

and xml code like this..

<?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>
like image 53
Android Team Avatar answered Dec 16 '22 05:12

Android Team