无法使用 VLC for Android 设置字幕 [英] Unable to Set Subtitles with VLC for Android

查看:99
本文介绍了无法使用 VLC for Android 设置字幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在为 Android 启动 VLC 的 VideoPlayerActivity 时,我无法设置字幕位置.我的目标是 API 27 并使用 FileProvider 来允许访问文件.

I'm having trouble setting the subtitles location when starting the VideoPlayerActivity of VLC for Android. I am targeting API 27 and using a FileProvider to allow access to files.

根据文档此处,如果您设置了subtitles_location" 额外,那么您可以提供字幕文件的路径.不幸的是,我似乎无法让它发挥作用.

According to the documentation here, if you set the, "subtitles_location" extra, then you can provide the path of the subtitles file. Unfortunately, I can't seem to get this to work.

我看到字幕"菜单项保持灰色,下载字幕似乎没有改变这种状态.点击时,选择字幕文件"VLC 声明目录中没有字幕文件,并且 URI 以content://"为前缀.

I'm seeing that the, "Subtitles" menu item remains grayed out and downloading subtitles doesn't seem to change this state. When tapping, "Select subtitle file" VLC states there are no subtitle files in the directory and the URI is prefixed with, "content://" as expected.

视频文件和字幕文件共享同一个父目录,并使用 FileProvider 提供.

Both the video file and subtitles file share the same parent directory and are provided using FileProvider.

定位 API >= 24 时如何设置字幕文件?

更新:

  • 似乎在点击字幕选择菜单项时,它使用视频文件的父目录.这是在这里看到的.
  • 想通了,请参阅下面的解决方案.

以下是我如何通过 Intent 启动 VideoPlayerActivity 的一些代码片段.

Below are some code snippets of how I'm starting the VideoPlayerActivity through an intent.

播放视频方法:

/**
 * Play a video using VLC media player. This method will play media by using
 * the VLC VideoPlayerActivity.
 *
 * @param activity         The current activity.
 * @param authority        The FileProvider authority.
 * @param vlcPackageName   The VLC package name.
 * @param vlcActivityName  The VLC activity name.
 * @param videoFile        The video file to play.
 * @param subtitlesFile    The subtitles file to play.
 * @param playbackPosition The playback position.
 * @param requestCode      The activity for result request code.
 */
public static void playVideo(
        AppCompatActivity activity,
        String authority,
        String vlcPackageName,
        String vlcActivityName,
        String vlcTitleExtra,
        String vlcPositionExtra,
        String vlcFromStartExtra,
        String vlcSubtitlesExtra,
        File videoFile,
        File subtitlesFile,
        long playbackPosition,
        int requestCode
) {
    Intent intent = new Intent(Intent.ACTION_VIEW);

    intent.setPackage(vlcPackageName);

    // Set component to VLC video player activity.
    intent.setComponent(new ComponentName(
            vlcPackageName,
            vlcActivityName
    ));

    // Set intent permission flags.
    intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
    intent.addFlags(Intent.FLAG_GRANT_PREFIX_URI_PERMISSION);
    intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

    // Set title and playback position.
    intent.putExtra(vlcTitleExtra, videoFile.getName());
    intent.putExtra(vlcPositionExtra, playbackPosition);
    intent.putExtra(vlcFromStartExtra, false);

    // Set video file location
    intent.setDataAndTypeAndNormalize(
            FileProvider.getUriForFile(
                    activity,
                    authority,
                    videoFile
            ),
            "video/*"
    );

    // Subtitles file provided, set location on intent.
    if (subtitlesFile != null) {
        intent.putExtra(
                vlcSubtitlesExtra,
                FileProvider.getUriForFile(
                        activity,
                        authority,
                        subtitlesFile
                )
        );
    }

    activity.startActivityForResult(intent, requestCode);
}

清单中的提供者定义

   <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="com.masterwok.bitcast.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true" >
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/paths" />
    </provider>

@xml/paths 定义:

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path
        name="Download"
        path="Download/"/>
</paths>

推荐答案

我能够通过使用未记录的item_location"额外定义来解决这个问题:这里.

I was able to solve this by using the undocumented, "item_location" extra defined: here.

这解决了定位 API >= 24 时的权限问题.发生的事情是 VideoPlayerActivity 试图读取使用 FileProvider 提供的 URI 的父目录.如果我不得不猜测,我会说 Android 不允许这样做.

This gets around the permission issue when targeting API >= 24. What was happening is the VideoPlayerActivity was trying to read the parent directory of the URI provided using the FileProvider. If I had to guess, I'd say Android doesn't allow this.

通过使用额外的item_location"传递 URI,我们可以绕过这个限制.

By passing the URI using the, "item_location" extra, we can bypass this restriction.

我现在播放视频文件的方法如下:

The method I use to play a video file is now the following:

/**
 * Play a video using VLC media player. This method will play media by using
 * the VLC VideoPlayerActivity.
 *
 * @param activity         The current activity.
 * @param vlcPackageName   The VLC package name.
 * @param vlcActivityName  The VLC activity name.
 * @param videoFile        The video file to play.
 * @param playbackPosition The playback position.
 * @param requestCode      The activity for result request code.
 */
public static void playVideo(
        AppCompatActivity activity,
        String vlcPackageName,
        String vlcActivityName,
        String vlcItemLocationExtra,
        String vlcTitleExtra,
        String vlcPositionExtra,
        String vlcFromStartExtra,
        File videoFile,
        long playbackPosition,
        int requestCode
) {
    Intent intent = new Intent(Intent.ACTION_VIEW);

    intent.setPackage(vlcPackageName);

    // Set component to VLC video player activity.
    intent.setComponent(new ComponentName(
            vlcPackageName,
            vlcActivityName
    ));

    // Set intent permission flags.
    intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
    intent.addFlags(Intent.FLAG_GRANT_PREFIX_URI_PERMISSION);
    intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

    // Set title and playback position.
    intent.putExtra(vlcTitleExtra, videoFile.getName());
    intent.putExtra(vlcPositionExtra, playbackPosition);
    intent.putExtra(vlcFromStartExtra, false);

    // Set video file location (subtitles are found by VLC by using parent directory).
    intent.putExtra(vlcItemLocationExtra, Uri.fromFile(videoFile));

    activity.startActivityForResult(intent, requestCode);
}

这篇关于无法使用 VLC for Android 设置字幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆