ExoPlayer - 如何播放本地mp3文件

时间:2015-02-21 17:41:50

标签: android exoplayer

我正在尝试使用ExoPlayer而不是MediaPlayer,因为它是MediaPlayer返回错误的getCurrentPosition()的常见错误,我需要替代。

但我找不到任何信息如何通过文件的文件路径打开本地文件,与MediaPlayer的.setDataSource(String filepath)

相同

Google没有任何示例,官方文档网站奇怪地在两台计算机上崩溃我的FireFox浏览器

4 个答案:

答案 0 :(得分:9)

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

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

答案 1 :(得分:5)

使用Srikanth Peddibhotla的代码进行小修改

该文件的Uri字符串应为“file:///mnt/sdcard/YourFilename.mp4”而不是Samples.java中的“/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);

答案 2 :(得分:1)

使用ExoPlayer 2.1,从demo项目开始,只需添加assets中的mp3文件,即可在不修改任何Java代码的情况下播放assets文件夹中的mp3文件文件夹以及创建或修改json文件。从ExoPlayer demo项目开始:

  1. 将mp3文件放入demo/assets文件夹(media.exolist.json)。

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

  3. { "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后的选择器活动屏幕:

    ExoPlayer Chooser Activity

    点击Mary Had a Little LambItsy Bitsy Spider并播放该mp3。

答案 3 :(得分:0)

Google最近更改了一些变量名称和类定义!以下不同对我有用。

Sub TryThis()
' Write each slide's notes to a text file
' in same directory as presentation itself
' Each file is named NNNN_Notes_Slide_xxx
' where NNNN is the name of the presentation
'       xxx is the slide number

Dim oSl As Slide
Dim oSh As Shape
Dim strFileName As String
Dim strNotesText As String
Dim intFileNum As Integer
Dim strLine As String
Dim strData As String

' Since Mac PPT will toss non-fatal errors, just keep moving along:
On Error Resume Next

' Get the notes text
For Each oSl In ActivePresentation.Slides
    For Each oSh In oSl.NotesPage.Shapes

        ' Here's where the error will occur, if any:
        If oSh.PlaceholderFormat.Type = ppPlaceholderBody Then
        ' so deal with it if so:
        If Err.Number = 0 Then
            If oSh.HasTextFrame Then
                If oSh.TextFrame.HasText Then
                    strData = strData + "Folie " & oSl.SlideIndex & vbCrLf & oSh.TextFrame.TextRange.Text & vbCrLf & vbCrLf
                    Close #intFileNum
                End If  ' HasText
            End If   ' HasTextFrame
        End If  ' Err.Number = 0
        End If  ' PlaceholderType test
    Next oSh
Next oSl

' now write the text to file
strFileName = ActivePresentation.Path _
& "\" & ActivePresentation.Name & "_Notes" _
& ".txt"
intFileNum = FreeFile()
Open strFileName For Output As intFileNum
Print #intFileNum, strData
Close #intFileNum

End Sub