0

I want to play Video with ExoPlayer, but unfortunately I cannot find any solution.Please help me I've been scratching my head for two days but can't find anything.

This is my code when preparing and setting exoPlayer properties.

String path = "asset:///splash_video.mp4";
    Uri uri = Uri.parse(path);
    player = ExoPlayerFactory.newSimpleInstance(new DefaultRenderersFactory(this), new DefaultTrackSelector(), new DefaultLoadControl());
    DataSource.Factory factory = new DefaultHttpDataSourceFactory(Util.getUserAgent(this, getPackageName()));
    simpleExoPlayerView.setPlayer(player);
    ExtractorMediaSource extractorMediaSource = new ExtractorMediaSource(uri, factory, new DefaultExtractorsFactory(), new Handler(), null);
    player.prepare(extractorMediaSource);
    player.setPlayWhenReady(true);

Also I've been following this link from youtube. https://www.youtube.com/watch?v=vwImlEfitQU

Ahsan Saeed
  • 701
  • 10
  • 22
  • [This](https://stackoverflow.com/questions/30852975/exoplayer-reading-mp3-file-from-raw-folder) may help. – ADM Dec 14 '17 at 07:48
  • I also use this approach but not working. Also exoplayer cannot read file from raw folder it says here. @ADM – Ahsan Saeed Dec 14 '17 at 08:07
  • There are same issues discussed on Git too . You can have a look there too . – ADM Dec 14 '17 at 08:08

4 Answers4

3

Here is my code is working for audio files,working fine and tested ok: You just pass file name which should be like 001 is the file in assets\001.mp3

you should call the function like this:

prepareExoPlayerFromAssetResourceFile(001);

the code is:

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



   /*
    MediaSource audioSource1 = new ExtractorMediaSource(
            Uri.parse("asset:///" + getfileName(current_file)), // file audio ada di folder assets
            new DefaultDataSourceFactory(this, userAgent),
            new DefaultExtractorsFactory  (),
            null,
            null
    );

    */
    exoPlayer.prepare(audioSource);

}
1

You may also need to set appt compress config in build.gradle(app).

android {
  ...
  aaptOptions{
    noCompress "mp4" // file extesion
  }
}
blake wu
  • 11
  • 3
0

You should use a DefaultDataSourceFactory instead of a DefaultHttpDataSourcFactory:

new ExtractorMediaSource(
    Uri.parse("asset://splash_video.mp4"),
    new DefaultDataSourceFactory(context, userAgent),
    new DefaultExtractorsFactory(),
    null,
    null);
marcbaechinger
  • 2,759
  • 18
  • 21
0

Create a folder "raw" inside your application's "res" folder and copy the file to the newly created folder and replace,

   ExtractorMediaSource audioSource = new ExtractorMediaSource(
            Uri.parse("asset:///" + current_file), // file audio ada di folder assets
            new DefaultDataSourceFactory(this, userAgent),
            new DefaultExtractorsFactory(),
            null,
            null
    );

this code with,

   Uri uri = Uri.parse("android.resource://Your Package Name/" + R.raw.welcome_video);
   ExtractorMediaSource audioSource = new ExtractorMediaSource(
            Uri.parse("uri ), // file audio ada di folder assets
            new DefaultDataSourceFactory(this, userAgent),
            new DefaultExtractorsFactory(),
            null,
            null
    );
SHIDHIN TS
  • 1,557
  • 3
  • 26
  • 58