Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play video with ExoPlayer through a folder in the "assets" folder

I'm trying to play a video with the use of ExoPlayer API and by using the exoplayer library of version : 'com.google.android.exoplayer:exoplayer:2.8.1'. I want to play a video called video.mp4 which is in a folder called folder1 and this folder in inside the folder assets in the res (res/assets/folder1/video.mp4). I cannot get my code to play the video. Please help me.

My MainActivity.java:

package com.example.amandeep.example2;

import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;

import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.ExoPlayerFactory;
import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.source.ConcatenatingMediaSource;
import com.google.android.exoplayer2.source.ExtractorMediaSource;
import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.source.MergingMediaSource;
import com.google.android.exoplayer2.source.SingleSampleMediaSource;
import com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection;
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector;
import com.google.android.exoplayer2.trackselection.TrackSelection;
import com.google.android.exoplayer2.ui.PlayerView;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DefaultBandwidthMeter;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
import com.google.android.exoplayer2.util.MimeTypes;
import com.google.android.exoplayer2.util.Util;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity
{
    SimpleExoPlayer video_player;
    PlayerView player_screen;
    DefaultTrackSelector track_selector;
    DefaultBandwidthMeter band_width_meter = new DefaultBandwidthMeter();

    MediaSource mediaSource;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        player_screen = findViewById (R.id.player_screen);
        player_screen.requestFocus();

        TrackSelection.Factory video_track_selection_factory = new AdaptiveTrackSelection.Factory(band_width_meter);
        track_selector = new DefaultTrackSelector(video_track_selection_factory);
        video_player = ExoPlayerFactory.newSimpleInstance(this, track_selector);

        player_screen.setPlayer(video_player);
        video_player.setPlayWhenReady(true);

        DataSource.Factory data_source_factory = new DefaultDataSourceFactory(this, Util.getUserAgent(this, "Application Name"), new DefaultBandwidthMeter());
        Uri url = Uri.parse("file:///android_asset/folder/video.mp4");
        mediaSource = new ExtractorMediaSource.Factory(data_source_factory).createMediaSource(url);

        video_player.prepare(mediaSource);
    }
}

My 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">

    <com.google.android.exoplayer2.ui.PlayerView
        android:id="@+id/player_screen"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_gravity="center"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:use_controller="true" />

</android.support.constraint.ConstraintLayout>

Note: I want to use the Uri.parse method.

IF SOMEONE CANNOT UNDERSTAND MY QUESTION PROPERLY, PLEASE COMMENT BELOW.

IF URI.PARSE METHOD CANNOT USE FILES FROM ASSETS THEN PLEASE TELL ME

like image 411
aman Avatar asked Oct 27 '25 09:10

aman


2 Answers

ExoPlayer 2.12 introduces the MediaItem class so you can do:

val firstVideoUri = Uri.parse("asset:///localfile.mp4")
val firstItem = MediaItem.fromUri(firstVideoUri)
player.addMediaItem(firstItem)

Note that the URI should start with asset:/// not assets:///

like image 66
donturner Avatar answered Oct 29 '25 01:10

donturner


This is my code to play audio file, hope it helps you.

    private void prepareExoPlayerFromAssetResourceFile(int current_file) {
    exoPlayer = ExoPlayerFactory.newSimpleInstance(this, new DefaultTrackSelector((TrackSelection.Factory) null), new DefaultLoadControl());
    exoPlayer.addListener(eventListener);

    //DataSpec dataSpec = new DataSpec(uri);
    //DataSpec dataSpec = new DataSpec(Uri.parse("asset:///001.mp3"));
    DataSpec dataSpec = new DataSpec(Uri.parse("asset:///" + current_file +".mp3"));
    final AssetDataSource assetDataSource = new AssetDataSource(this);
    try {
        assetDataSource.open(dataSpec);
    } catch (AssetDataSource.AssetDataSourceException e) {
        e.printStackTrace();
    }

    DataSource.Factory factory = new DataSource.Factory() {
        @Override
        public DataSource createDataSource() {
            //return rawResourceDataSource;
            return assetDataSource;
        }
    };

    MediaSource audioSource = new ExtractorMediaSource(assetDataSource.getUri(),
            factory, new DefaultExtractorsFactory(), null, null);

    exoPlayer.prepare(audioSource);
    initMediaControls();

}
like image 39
صلي علي محمد Atef Farouk Avatar answered Oct 29 '25 00:10

صلي علي محمد Atef Farouk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!