ExoPlayer - how to play local mp3 file

前端 未结 4 586
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-07 13:37

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

相关标签:
4条回答
  • 2021-02-07 13:50

    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);
    
    0 讨论(0)
  • 2021-02-07 13:52

    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.

    0 讨论(0)
  • 2021-02-07 13:52

    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() {}
    
     }
    
    0 讨论(0)
  • 2021-02-07 13:53

    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);
    
    0 讨论(0)
提交回复
热议问题