Here we will use simple Exoplayer to play any video in android application. Steps are following:-
1. Take new project in android studio and open activity_main.xml and write following:-
activity_main.xml :-
4. create new layout name of videoplay_activity.xml:-
1. Take new project in android studio and open activity_main.xml and write following:-
activity_main.xml :-
<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/tvWatchVideo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Watch Video"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" /> </android.support.constraint.ConstraintLayout>
2. Open MainActivity.java and write following code:-
public class MainActivity extends AppCompatActivity { private TextView tvWatchVideo; @Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tvWatchVideo = (TextView)findViewById(R.id.tvWatchVideo); tvWatchVideo.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View view) { startActivity(new Intent(MainActivity.this,VideoPlayActivity.class)); } }); } }3. Now to play video we will add Exoplayer library in gradle like following:-
dependencies { // exo player
implementation 'com.google.android.exoplayer:exoplayer:2.7.0'}
4. create new layout name of videoplay_activity.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"> <com.google.android.exoplayer2.ui.SimpleExoPlayerView
android:layout_width="match_parent"
android:id="@+id/exoPlayerview"
android:layout_height="match_parent"> </com.google.android.exoplayer2.ui.SimpleExoPlayerView> </RelativeLayout>
5. Now to play video we will create new class with name of VideoPlayActivity:-
public class VideoPlayActivity extends AppCompatActivity { String videoURL = "http://blueappsoftware.in/layout_design_android_blog.mp4"; private SimpleExoPlayerView simpleExoPlayerView; private SimpleExoPlayer exoPlayer; @Override
protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.videoplay_activity); exoPlayerControl(); } private void exoPlayerControl() { simpleExoPlayerView = (SimpleExoPlayerView)findViewById(R.id.exoPlayerview); try { BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter(); TrackSelector trackSelector = new DefaultTrackSelector(new AdaptiveTrackSelection.Factory(bandwidthMeter)); exoPlayer = ExoPlayerFactory.newSimpleInstance(this, trackSelector); Uri videoUrl = Uri.parse(videoURL); DefaultHttpDataSourceFactory defaultHttpDataSource = new DefaultHttpDataSourceFactory("exoplayer"); ExtractorsFactory factory = new DefaultExtractorsFactory(); MediaSource mediaSource = new ExtractorMediaSource(videoUrl,defaultHttpDataSource,factory,null,null); simpleExoPlayerView.setPlayer(exoPlayer); exoPlayer.prepare(mediaSource); exoPlayer.setPlayWhenReady(true); }catch (Exception e){ e.printStackTrace(); } } }6.Output will be like this:-
Hello, can you please make this in kotlin?
ReplyDelete