Android 播放SMB共享视频

表面上看MediaPlayer只能播放本地和http协议视频。没有直接支持smb://协议。那还能播放smb视频呢?也可以的!

MediaPlayer有一个方法叫:setDataSource(MediaDataSource)。

java 复制代码
    /**
     * Sets the data source (MediaDataSource) to use.
     *
     * @param dataSource the MediaDataSource for the media you want to play
     * @throws IllegalStateException if it is called in an invalid state
     * @throws IllegalArgumentException if dataSource is not a valid MediaDataSource
     */
    public void setDataSource(MediaDataSource dataSource)
            throws IllegalArgumentException, IllegalStateException {
        _setDataSource(dataSource);
    }

我们只要实现MediaDataSource这个接口就可以了。

java 复制代码
/**
 * For supplying media data to the framework. Implement this if your app has
 * special requirements for the way media data is obtained.
 *
 * <p class="note">Methods of this interface may be called on multiple different
 * threads. There will be a thread synchronization point between each call to ensure that
 * modifications to the state of your MediaDataSource are visible to future calls. This means
 * you don't need to do your own synchronization unless you're modifying the
 * MediaDataSource from another thread while it's being used by the framework.</p>
 */
public abstract class MediaDataSource implements Closeable {
    /**
     * Called to request data from the given position.
     *
     * Implementations should fill {@code buffer} with up to {@code size}
     * bytes of data, and return the number of valid bytes in the buffer.
     *
     * Return {@code 0} if size is zero (thus no bytes are read).
     *
     * Return {@code -1} to indicate that end of stream is reached.
     *
     * @param position the position in the data source to read from.
     * @param buffer the buffer to read the data into.
     * @param offset the offset within buffer to read the data into.
     * @param size the number of bytes to read.
     * @throws IOException on fatal errors.
     * @return the number of bytes read, or -1 if end of stream is reached.
     */
    public abstract int readAt(long position, byte[] buffer, int offset, int size)
            throws IOException;

    /**
     * Called to get the size of the data source.
     *
     * @throws IOException on fatal errors
     * @return the size of data source in bytes, or -1 if the size is unknown.
     */
    public abstract long getSize() throws IOException;
}

下面是我实现的代码:

java 复制代码
public class CustomMediaDataSource extends MediaDataSource {
    private static Logger Log4j = Logger.getLogger(CustomMediaDataSource.class);
    private SmbRandomAccessFile mFile; // must not Main UI thread.
    private long mFileSize;

    public CustomMediaDataSource(SmbRandomAccessFile smbFile, long size) throws SmbException {
        this.mFile = smbFile;
        mFileSize = size;
    }

    @Override
    public int readAt(long position, byte[] buffer, int offset, int size) throws IOException {
        if (mFile.getFilePointer() != position)
            mFile.seek(position);

        if (size <= 0)
            return 0;

        Log4j.info("CustomMediaDataSource, readAt, position:" + position);
        return mFile.read(buffer, 0, size);
    }

    @Override
    public long getSize() throws IOException {
        return mFileSize;
    }

    @Override
    public void close() throws IOException {
        mFileSize = 0;
        if (mFile != null) {
            mFile.close();
            mFile = null;
        }
    }
}

这里有一个小插曲, 导致花费了大半天时间。视频播放不能用InputStream接口,要用RandomAccess接口。因为视频播放的数据不是按顺序取的。不然就会报错:java.io.IOException: Prepare failed.: status=0x1。

如果你是其他播放器的, 同样也会有这样的接口提供给你。比如ljkPlayer等。

这个主题值得写一下,网上基本没有讲到。

相关推荐
宇的出海纪元8 分钟前
一条长视频怎么复用成五个平台的内容?
音视频·视频
ck0135793 小时前
网课视频太长怎么整理?通过视频修剪和片段提取制作学习资料
图像处理·人工智能·测试工具·音视频·视频
TST-魚爷4 小时前
腾讯会议搭配一体化全向麦,音频快速设置教程-易视讯
服务器·windows·音视频·腾讯会议·zoom会议
开开心心就好5 小时前
电脑小毛病修复工具,不用重装系统了
智能手机·pdf·电脑·word·音视频·laravel·perl
Android系统攻城狮5 小时前
FFmpeg深度解析之av_frame_get_buffer:如何为AVFrame分配缓冲区?(八十四)
linux·ffmpeg·音视频·车载音频·ffmpeg音视频进阶
hai_android7 小时前
从导入视频到上屏出声:Android + FFmpeg 播放器全流程拆解
android·flutter·ffmpeg·kotlin·android studio·音视频·android jetpack
魔众7 小时前
AIGCPanel 升级 MuseTalk:一键装好,视频就能对口型
音视频
jinlong06037 小时前
雪亮 / 政务 / 公安等项目国标GB28181级联视频汇聚平台LiveGBS的功能简介和使用介绍
音视频·视频回放·gb28181监控平台·监控汇聚·国标录像·安防录像
Ryan-Zhong1 天前
短视频提取文案解析 API 实战:从视频链接到可检索文本的异步转写流水线
音视频
ARM+FPGA+AI工业主板定制专家1 天前
Jetson Thor 平台 BSP 开发:新模组第三方载板适配经验分享
人工智能·经验分享·单片机·机器人·音视频