Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExoPlayer - how to play local mp3 file

I'm trying to use ExoPlayer instead of MediaPlayer because it's a common bug that MediaPlayer returns wrong getCurrentPosition() and I need a substitute.

But I can't find an info anywhere how to open a local file through the file path to the file same as MediaPlayer's .setDataSource(String filepath)

Google doesn't have any example and the official documentation site strangely crash my FireFox browser on both computers

like image 786
Lyubov Alekseeva Avatar asked Feb 21 '15 17:02

Lyubov Alekseeva


People also ask

Does ExoPlayer use mediaplayer?

ExoPlayer is an app-level media player built on top of low-level media APIs in Android. It is an open source project used by Google apps, including YouTube and Google TV.

Can we play YouTube video in ExoPlayer Android?

@MichaelStoddart Yes. It is a possibility.

How to play audio and video in ExoPlayer?

For ExoPlayer we are using PlayerView here, which can be used to play both audio and video. Also, it displays the subtitles of the video and it has many other features also. You can use the PlayerControlView also.

How do I use ExoPlayer in Java?

Add ExoPlayer in your layout file. In your Java/Kotlin file make a function to initialize the player. This function will initialise the ExoPlayer in the player view and it will play the media when the player is ready. You can call this function from onStart or onResume.

Where can I find the ExoPlayer support for Android?

So, you will find each and every support for the ExoPlayer on the Developers website of Google. MediaPlayer is there from Android API level 1. But ExoPlayer was introduced in Android API level 16 i.e. the Android Jelly Bean. So, you can't use ExoPlayer for API less than 16.

How do I use ExoPlayer in Kotlin?

Add dependency of ExoPlayer in your application. Add ExoPlayer in your layout file. In your Java/Kotlin file make a function to initialize the player. This function will initialise the ExoPlayer in the player view and it will play the media when the player is ready.


1 Answers

The ExoPlayer demo app in github can be modified to play local files. To do that, edit https://github.com/google/ExoPlayer/blob/master/demo/src/main/java/com/google/android/exoplayer/demo/Samples.java file to add a new video set.

public static final Sample[] LOCAL_VIDEOS = new Sample[] {
   new Sample("Some User friendly name of video 1",
     "/mnt/sdcard/video1.mp4", DemoUtil.TYPE_OTHER),
  new Sample("Some User friendly name of video 2",
    "/mnt/sdcard/video2.mp4", DemoUtil.TYPE_OTHER),
};

To do that, edit https://github.com/google/ExoPlayer/blob/master/demo/src/main/java/com/google/android/exoplayer/demo/Samples.java file to add a new sample set.

sampleAdapter.add(new Header("Local Videos"));
sampleAdapter.addAll((Object[]) Samples.LOCAL_VIDEOS);
like image 105
Srikanth Peddibhotla Avatar answered Sep 18 '22 17:09

Srikanth Peddibhotla