ExoPlayer – 如何播放本地mp3文件

我试图使用ExoPlayer而不是MediaPlayer,因为这是一个常见的错误,MediaPlayer返回错误的getCurrentPosition(),我需要一个替代品。

但我无法find任何地方如何通过文件path打开一个本地文件的文件相同的MediaPlayer的.setDataSource(String filepath)

Google没有任何示例,官方文档网站奇怪地将两台电脑上的FireFox浏览器崩溃

Related of "ExoPlayer – 如何播放本地mp3文件"

github中的ExoPlayer演示程序可以修改为播放本地文件。 为此,请编辑https://github.com/google/ExoPlayer/blob/master/demo/src/main/java/com/google/android/exoplayer/demo/Samples.java文件以添加新的video集。

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

为此,请编辑https://github.com/google/ExoPlayer/blob/master/demo/src/main/java/com/google/android/exoplayer/demo/Samples.java文件以添加新的样本集。

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

Srikanth Peddibhotla的代码作了一个小修改

该文件的Uristring在Samples.java中应该是“file:///mnt/sdcard/YourFilename.mp4”,而不是“/mnt/sdcard/YourFilename.mp4”

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

另外, 将以下行添加到SampleChooserActivity.java

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

使用ExoPlayer 2.1,并从demo项目开始,您可以播放assets文件夹中的mp3文件,而无需修改任何Java代码,只需在assets文件夹中添加mp3文件并创build或修改json文件即可。 从ExoPlayer demo项目开始:

  1. 把mp3文件放在demo/assets文件夹中(用media.exolist.json )。

  2. 请修改media.exolist.json或创build一个新文件,如my.exolist.json其中包含一个或多个格式如下所示的条目:

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

(最后一个逗号是假设会有另外一个类别,例如Blues SongsJazz Songs等有更多的mp3条目,最后一个类别后面没有逗号。

下图显示了您点击Children's Songs后的select器活动屏幕:

ExoPlayer选择器活动

点击Mary Had a Little LambItsy Bitsy Spider和那个MP3播放。

谷歌今天改变了一些variables名称和类定义! 以下不同的作品对我来说。

 --- 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() {} }