无法使用资源文件夹播放Exoplayer视频

时间:2017-12-14 07:37:47

标签: android exoplayer exoplayer2.x

我想用ExoPlayer播放视频,但不幸的是我找不到任何解决方案。请帮助我,我已经摸不着头两天但找不到任何东西。

这是我准备和设置exoPlayer属性时的代码。

{{1}}

此外,我一直在关注youtube上的链接。 https://www.youtube.com/watch?v=vwImlEfitQU

4 个答案:

答案 0 :(得分:0)

您应该使用DefaultDataSourceFactory而不是DefaultHttpDataSourcFactory:

new ExtractorMediaSource(
    Uri.parse("asset://splash_video.mp4"),
    new DefaultDataSourceFactory(context, userAgent),
    new DefaultExtractorsFactory(),
    null,
    null);

答案 1 :(得分:0)

这是我的代码正在处理音频文件,工作正常且经过测试可以: 您只需传递文件名,该文件名应为 assets \ 001.mp3

中的文件

您应该这样调用函数:

prepareExoPlayerFromAssetResourceFile(001);

代码是:

    private void prepareExoPlayerFromAssetResourceFile(int current_file) {

    exoPlayer = ExoPlayerFactory.newSimpleInstance(this, new DefaultTrackSelector((TrackSelection.Factory) null), new DefaultLoadControl());
    exoPlayer.addListener(eventListener);


    //DataSpec dataSpec = new DataSpec(Uri.parse("asset:///001.mp3"));
    DataSpec dataSpec = new DataSpec(Uri.parse("asset:///" + (current_file))+".mp3");
    final AssetDataSource assetDataSource = new AssetDataSource(this);
    try {
        assetDataSource.open(dataSpec);
    } catch (AssetDataSource.AssetDataSourceException e) {
        e.printStackTrace();
    }

    DataSource.Factory factory = new DataSource.Factory() {
        @Override
        public DataSource createDataSource() {
            //return rawResourceDataSource;
            return assetDataSource;
        }
    };

    MediaSource audioSource = new ExtractorMediaSource(assetDataSource.getUri(),
            factory, new DefaultExtractorsFactory(), null, null);



   /*
    MediaSource audioSource1 = new ExtractorMediaSource(
            Uri.parse("asset:///" + getfileName(current_file)), // file audio ada di folder assets
            new DefaultDataSourceFactory(this, userAgent),
            new DefaultExtractorsFactory  (),
            null,
            null
    );

    */
    exoPlayer.prepare(audioSource);

}

答案 2 :(得分:0)

在应用程序的“ res”文件夹中创建一个“原始”文件夹,然后将该文件复制到新创建的文件夹中并替换,

private void CheckIfDeviceInstalled(string driver, bool shouldBeInstalled)
{
    main_wtl.writeToConsoleAndLog("Searching for " + driver + " driver...", ConsoleColor.White);

    SelectQuery query = new SelectQuery("Win32_pnpsigneddriver");
    query.Condition = "DeviceName = '"+driver+"'";
    ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
    var drivers = searcher.Get();

    if (shouldBeInstalled)
    {
        if (drivers.Count > 0)
            main_wtl.writeToConsoleAndLog(driver + " Driver exists.", ConsoleColor.Green);
        else
            main_wtl.writeToConsoleAndLog(driver + " Driver could not be found.", ConsoleColor.Red);
    }
    else
    {
        if (drivers.Count > 0)
            main_wtl.writeToConsoleAndLog(driver + " Driver exists.", ConsoleColor.Red);
        else
            main_wtl.writeToConsoleAndLog(driver + " Driver could not be found.", ConsoleColor.Green);
    }
}

此代码带有

   ExtractorMediaSource audioSource = new ExtractorMediaSource(
            Uri.parse("asset:///" + current_file), // file audio ada di folder assets
            new DefaultDataSourceFactory(this, userAgent),
            new DefaultExtractorsFactory(),
            null,
            null
    );

答案 3 :(得分:0)

您可能还需要在build.gradle(app)中设置appt compress配置。

android {
  ...
  aaptOptions{
    noCompress "mp4" // file extesion
  }
}