SimpleExoPlayer to play a video in Java

Send video from python use Fastapi

@app.get("/video")
async def video_endpoint(range: str = Header(None),path:str=None):
video_path = Path(path)
if range==None:
range = "bytes=0-"
start, end = range.replace("bytes=", "").split("-")
start = int(start)
end = int(end) if end else start + CHUNK_SIZE
print(range)
with open(video_path, "rb") as video:
video.seek(start)
data = video.read(end - start)
filesize = str(video_path.stat().st_size)
headers = {
"content-type": "video/mp4",
"content-encoding": "identity",
'Content-Range': f'bytes {str(start)}-{str(end)}/{filesize}',
'Accept-Ranges': 'bytes',
"content-length" : str(filesize),
"access-control-expose-headers": (
"content-type, accept-ranges, content-length, "
"content-range, content-encoding"
),
}
return Response(data, status_code=206, headers=headers, media_type="video/mp4")


 Here is an example of how to use SimpleExoPlayer to play a video in Java:

  1. First, you'll need to add the ExoPlayer library to your project. You can do this by adding the following dependency to your app's build.gradle file:
groovy
implementation 'com.google.android.exoplayer:exoplayer:2.16.1'
  1. Next, create a layout file for your video player. Here's an example:
xml
<com.google.android.exoplayer2.ui.PlayerView
android:id="@+id/video_player_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
  1. In your activity or fragment, initialize the player:
java
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.source.ProgressiveMediaSource;
import com.google.android.exoplayer2.ui.PlayerView;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
private SimpleExoPlayer player;
private void initializePlayer() {
// Create a SimpleExoPlayer instance
player = new SimpleExoPlayer.Builder(this).build();

// Bind the player to the view
PlayerView playerView = findViewById(R.id.video_player_view);
playerView.setPlayer(player);

// Create a MediaSource representing the media to be played
MediaSource mediaSource = new ProgressiveMediaSource.Factory(
new DefaultDataSourceFactory(this, "exoplayer-example")
).createMediaSource(Uri.parse("http://192.168.100.27:8000/video?path=Recordings/file,63,start_recording.mp4"));

// Prepare the player with the source
player.setMediaSource(mediaSource);
player.prepare();
}
  1. When the activity or fragment is started, call initializePlayer() to start playing the video:
java
@Override
protected void onStart() {
super.onStart();
initializePlayer();
}
  1. When the activity or fragment is stopped, release the player:
java
@Override
protected void onStop() {
super.onStop();
releasePlayer();
}
private void releasePlayer() {
player.release();
}

That's it! With these steps, you should now be able to play a video using SimpleExoPlayer in Java.

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.