2

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

aman
  • 307
  • 21
  • 48
  • I haven't tested it so I am not sure about what I am saying, but since assets folder is always kept compressed on the device, I believe that ExoPlayer won't be able to stream data out of it. You will need to copy the file somewhere else to get it uncompressed and then play it via ExoPlayer. Although you could get the InputStream from assets file and with some extra coding let exoplayer use the InputStream – Masoud Dadashi Aug 27 '18 at 19:53

6 Answers6

10

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:///

donturner
  • 17,867
  • 8
  • 59
  • 81
4

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();

}
2

Try a uri of this format:

file:///android_asset/

For your example that would need to be:

Uri url = Uri.parse("file:///android_asset/folder1/video.mp4");
marcbaechinger
  • 2,759
  • 18
  • 21
  • I tried your method, but it doesn't work. Should the folder name be "asset"? – aman Aug 20 '18 at 07:58
  • I have tried you method by renaming the folders to "assets" and "asset", but it still doesn't work. – aman Aug 20 '18 at 08:00
1

I'm using similar code for playing video from my assets folder with ExoPlayer and it works good.
But I noticed you mentioned diferent folders in description and code. You are saying the folder in assets is named folder1 but in code you have folder.
So make sure you have correct path to video.

goofy
  • 425
  • 7
  • 13
  • @Pierre.Vriens I'm also using similar code to play video from my assets folder and it works. So IMHO the only problem is in wrong path. I'm sorry if it was not obvious from my answer. – goofy Oct 02 '18 at 08:31
1

I write this answer because some methods on old answers deprecated :

fun prepareExoPlayerFromAssetResource(
 uri: String = "asset://example.mp3") 
{
  val dataSourceFactory = DataSource.Factory {AssetDataSource(context)}
  val mediaSource = ProgressiveMediaSource
                     .Factory(dataSourceFactory)
                     .createMediaSource(MediaItem.fromUri(Uri.parse(uri)))
        
  exoPlayer.addMediaSource(audioSource)
  exoPlayer.prepare()
  exoPlayer.play()
}

I hope this is useful for someone :)

  • You're not using `mediaSource` and instead are using an undefined `audioSource` variable later on. Did you mean to use `mediaSource` in both instances? – jari Jun 01 '23 at 09:25
0

This may be useful:

import android.annotation.SuppressLint
import android.net.Uri
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.google.android.exoplayer2.*
import com.google.android.exoplayer2.source.ProgressiveMediaSource
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector
import com.google.android.exoplayer2.upstream.*
import com.google.android.exoplayer2.upstream.DataSource.Factory
import com.google.android.exoplayer2.util.Util
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : AppCompatActivity(), Player.EventListener {

    private var player: SimpleExoPlayer? = null
    private var playWhenReady = true
    private var currentWindow = 0
    private var playbackPosition: Long = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }

    public override fun onStart() {
        super.onStart()
        if (Util.SDK_INT > 23) {
            initializePlayer("assets:///your_file.your_extension") //"assets:///pillwheel_pills.mp4"
        }
    }

    public override fun onResume() {
        super.onResume()
        hideSystemUi()
        if (Util.SDK_INT <= 23 || player == null) {
            initializePlayer("assets:///your_file.your_extension") //"assets:///pillwheel_pills.mp4"
        }
    }

    public override fun onPause() {
        super.onPause()
        if (Util.SDK_INT <= 23) {
            releasePlayer()
        }
    }

    public override fun onStop() {
        super.onStop()
        if (Util.SDK_INT > 23) {
            releasePlayer()
        }
    }

    private fun initializePlayer(path: String) {
        if (player == null) {
            val trackSelector = DefaultTrackSelector(this)
            trackSelector.setParameters(
                trackSelector.buildUponParameters().setMaxVideoSizeSd())
            player = SimpleExoPlayer.Builder(this)
                .setTrackSelector(trackSelector)
                .build()
        }
        video_view?.player = player
        video_view?.requestFocus()

        val dataSourceFactory = Factory { AssetDataSource(this@MainActivity) }

        val videoSource = ProgressiveMediaSource.Factory(dataSourceFactory)
            .createMediaSource(Uri.parse(path))

        player?.playWhenReady = playWhenReady
        player?.seekTo(currentWindow, playbackPosition)
        player?.addListener(this)
        player?.prepare(videoSource)
    }

    private fun releasePlayer() {
        if (player != null) {
            playbackPosition = player?.currentPosition!!
            currentWindow = player?.currentWindowIndex!!
            playWhenReady = player?.playWhenReady!!
            player?.removeListener(this)
            player?.release()
            player = null
        }
    }

    /**
     * set fullscreen
     */
    @SuppressLint("InlinedApi")
    private fun hideSystemUi() {
        video_view?.systemUiVisibility = (View.SYSTEM_UI_FLAG_LOW_PROFILE
                or View.SYSTEM_UI_FLAG_FULLSCREEN
                or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION)
    }

    override fun onPlayerError(error: ExoPlaybackException) {
        super.onPlayerError(error)

        //handle the error
    }

    override fun onPlayerStateChanged(playWhenReady: Boolean, playbackState: Int) {
        val stateString: String = when (playbackState) {
            ExoPlayer.STATE_IDLE -> "ExoPlayer.STATE_IDLE      -"
            ExoPlayer.STATE_BUFFERING -> "ExoPlayer.STATE_BUFFERING -"
            ExoPlayer.STATE_READY -> "ExoPlayer.STATE_READY     -"
            ExoPlayer.STATE_ENDED -> "ExoPlayer.STATE_ENDED     -"
            else -> "UNKNOWN_STATE             -"
        }
        Log.d(TAG, "changed state to " + stateString
                + " playWhenReady: " + playWhenReady)
    }

    companion object {
        private val TAG = MainActivity::class.java.name
    }
}

You can find the full source code on GitHub

Mattia Ferigutti
  • 2,608
  • 1
  • 18
  • 22