Android exoplayer tutorial in aide ide




Tutorial on how to integrate google exoplayer in aide ide with sample codes. ExoPlayer is an application level media player built on top of Android's low level media APIs. ExoPlayer has a number of advantages over Android's built-in MediaPlayer. It supports many of the same media formats as MediaPlayer plus adaptive formats such as DASH and SmoothStreaming. ExoPlayer is highly customizable and extensible, making it capable of many advanced use cases. It is an open source project used by Google apps including YouTube and Google Play Movies.

Lets start coding, first goto your "build.gradle" and add this folling codes.
compile 'com.google.android.exoplayer:exoplayer:r2.4.0'
    compile 'com.google.android.exoplayer:exoplayer-core:r2.4.0'
    compile 'com.google.android.exoplayer:exoplayer-dash:r2.4.0'
    compile 'com.google.android.exoplayer:exoplayer-hls:r2.4.0'
    compile 'com.google.android.exoplayer:exoplayer-smoothstreaming:r2.4.0'
    compile 'com.google.android.exoplayer:exoplayer-ui:r2.4.0'

Next goto your "AndroidManifest.xml" and add internet permission.
<uses-permission android:name="android.permission.INTERNET" />

In your layout add this code below, example "main.xml"
<com.google.android.exoplayer2.ui.SimpleExoPlayerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/exo_player_view">
</com.google.android.exoplayer2.ui.SimpleExoPlayerView>

Next goto your java files and add this code below, example "MainActivity.java"
import android.app.*;
import android.net.*;
import android.os.*;
import android.util.*;
import com.google.android.exoplayer2.*;
import com.google.android.exoplayer2.extractor.*;
import com.google.android.exoplayer2.source.*;
import com.google.android.exoplayer2.trackselection.*;
import com.google.android.exoplayer2.ui.*;
import com.google.android.exoplayer2.upstream.*;

public class MainActivity extends Activity {

    SimpleExoPlayerView exoPlayerView;
    SimpleExoPlayer exoPlayer;
    String videoURL = "http://blueappsoftware.in/layout_design_android_blog.mp4";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        exoPlayerView = (SimpleExoPlayerView) findViewById(R.id.exo_player_view);
try {


BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
TrackSelector trackSelector = new DefaultTrackSelector(new AdaptiveTrackSelection.Factory(bandwidthMeter));
exoPlayer = ExoPlayerFactory.newSimpleInstance(this, trackSelector);

Uri videoURI = Uri.parse(videoURL);

DefaultHttpDataSourceFactory dataSourceFactory = new DefaultHttpDataSourceFactory("exoplayer_video");
ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
MediaSource mediaSource = new ExtractorMediaSource(videoURI, dataSourceFactory, extractorsFactory, null, null);

exoPlayerView.setPlayer(exoPlayer);
exoPlayer.prepare(mediaSource);
exoPlayer.setPlayWhenReady(true);
}catch (Exception e){
Log.e("MainAcvtivity"," exoplayer error "+ e.toString());
}

    }
}


Exoplayer listener, NOTE prz is my circle progressbar and lmm is my layout that contain error message, you can remove that codes if you want.
exoPlayer.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){
prz.setVisibility(View.VISIBLE);
} else {
prz.setVisibility(View.GONE);
}
}

@Override
public void onPlayerError(ExoPlaybackException error) {
lmm.setVisibility(View.VISIBLE);

}

@Override
public void onPositionDiscontinuity() {
prz.setVisibility(View.VISIBLE);

}

@Override
public void onPlaybackParametersChanged(PlaybackParameters playbackParameters) {}
});

Read more exoplayer documentation on github.

Comments

Popular posts from this blog

Implement in-app purchase in aide ide

Implement google admob interstitial ads in aide ide

Implement a-ads on blogger or website

Android play .gif file on imageview in aide ide

Aide ide tutorial - Add line graph


Malitanyo Developer blog & tutorial 2020
Refund Policy    Facebook Page    Google Play

Blogger.com