ExoPlayer - how to play local mp3 file

徘徊边缘 提交于 2019-12-03 06:35:43

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

A minor modification with Srikanth Peddibhotla's code works

The Uri string for the file should be "file:///mnt/sdcard/YourFilename.mp4" instead of "/mnt/sdcard/YourFilename.mp4" in Samples.java

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

Also, add the following lines to SampleChooserActivity.java

 sampleAdapter.add(new Header("Local Videos"));
 sampleAdapter.addAll((Object[]) Samples.LOCAL_VIDEOS);

Using ExoPlayer 2.1, and starting with the demo project, you can play mp3 files from the assets folder without modifying any Java code, just by adding the mp3 files in the assets folder and creating or modifying a json file. Starting with the ExoPlayer demo project:

  1. Put the mp3 files in the demo/assets folder (with media.exolist.json).

  2. Either modify media.exolist.json or create a new file such as my.exolist.json containing one or more entries formatted like this:

{ "name": "Children's Songs", "samples": [ { "name": "Mary Had a Little Lamb", "uri": "asset:///mary1.mp3" }, { "name": "Itsy Bitsy Spider", "uri": "asset:///spider1.mp3" } ] },

(The final comma assumes there will be another category following, such as Blues Songs, Jazz Songs etc. with more mp3 entries. The final category has no comma after it.)

The figure below shows the chooser activity screen after you click on Children's Songs:

Click Mary Had a Little Lamb or Itsy Bitsy Spider and that mp3 plays.

Google changed some variable name and class definition these days! Below differ works for me.

--- a/demo/src/main/java/com/google/android/exoplayer/demo/SampleChooserActivity.java
+++ b/demo/src/main/java/com/google/android/exoplayer/demo/SampleChooserActivity.java
@@ -30,6 +28,8 @@ import android.widget.ExpandableListView;
 import android.widget.ExpandableListView.OnChildClickListener;
 import android.widget.TextView;

 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
@@ -44,7 +44,12 @@ public class SampleChooserActivity extends Activity {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.sample_chooser_activity);
     final List<SampleGroup> sampleGroups = new ArrayList<>();
-    SampleGroup group = new SampleGroup("YouTube DASH");
+
+    SampleGroup group = new SampleGroup("test videos");
+    group.addAll(Samples.LOCAL_VIDEOS);
+    sampleGroups.add(group);
+
+    group = new SampleGroup("YouTube DASH");
     group.addAll(Samples.YOUTUBE_DASH_MP4);
     group.addAll(Samples.YOUTUBE_DASH_WEBM);
     sampleGroups.add(group);
diff --git a/demo/src/main/java/com/google/android/exoplayer/demo/Samples.java b/demo/src/main/java/com/google/android/exoplayer/demo/Samples.java
index 9f58528..9e86f99 100644
--- a/demo/src/main/java/com/google/android/exoplayer/demo/Samples.java
+++ b/demo/src/main/java/com/google/android/exoplayer/demo/Samples.java
@@ -248,6 +248,13 @@ import java.util.Locale;
         "http://vod.leasewebcdn.com/bbb.flv?ri=1024&rs=150&start=0", Util.TYPE_OTHER),
   };

+  public static final Sample[] LOCAL_VIDEOS = new Sample[] {
+          new Sample("Some User friendly name of video 1",
+                  "file:///mnt/sdcard/test1.mp4", Util.TYPE_OTHER),
+          new Sample("Some User friendly name of video 2",
+                  "file:///mnt/sdcard/test2.mp4", Util.TYPE_OTHER),
+  };
+
   private Samples() {}

 }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!