How to play a video from the Internet in Android Studio - Programming Tutorial
AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET"/>
MainActivity.java
package com.example.myapplication;
import android.os.Build;
import android.os.Bundle;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.media3.common.MediaItem;
import androidx.media3.exoplayer.ExoPlayer;
import androidx.media3.ui.PlayerView;
public class MainActivity extends AppCompatActivity {
private PlayerView playerView;
private ExoPlayer player;
private boolean playWhenReady = true;
private Long playbackPosition =0L;
private MediaItem mediaItem;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
playerView = findViewById(R.id.video_view);
}
private void initializePlayer(){
player = new ExoPlayer.Builder(this).build();
playerView.setPlayer(player);
mediaItem = MediaItem.fromUri("https://cdn.pixabay.com/video/2023/09/24/182082-867762198_large.mp4");
player.setMediaItem(mediaItem,playbackPosition);
player.setPlayWhenReady(playWhenReady);
player.prepare();
player.play();
}
@Override
protected void onStart() {
super.onStart();
if (Build.VERSION.SDK_INT>23){
initializePlayer();
}
}
@Override
protected void onResume() {
super.onResume();
if ((Build.VERSION.SDK_INT<=23)||(player==null)){
initializePlayer();
}
}
private void releasePlayer(){
if (player!=null){
playbackPosition = player.getCurrentPosition();
playWhenReady = player.getPlayWhenReady();
player.release();
}
player = null;
}
@Override
protected void onPause() {
super.onPause();
if (Build.VERSION.SDK_INT<=23){
releasePlayer();
}
}
@Override
protected void onStop() {
super.onStop();
if (Build.VERSION.SDK_INT>23){
releasePlayer();
}
}
}activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black"
tools:context=".MainActivity">
<androidx.media3.ui.PlayerView
android:layout_width="match_parent"
android:layout_height="500dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:id="@+id/video_view"
/>
</androidx.constraintlayout.widget.ConstraintLayout>build.gradle
implementation("androidx.media3:media3-exoplayer:1.3.1")
implementation("androidx.media3:media3-exoplayer-dash:1.3.1")
implementation("androidx.media3:media3-ui:1.3.1")
Comments
Post a Comment