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等。

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

相关推荐
换个昵称都难21 分钟前
webrtc voice engine 介绍(新版webrtc)
ffmpeg·音视频·webrtc
searchforAI28 分钟前
2026年AI笔记工具对比实测:NotebookLM、通义听悟、Ai好记怎么选?
人工智能·笔记·gpt·ai·whisper·音视频·语音识别
音视频牛哥3 小时前
基于 SmartMediaKit 的座舱远程遥控系统技术方案——面向多摄像头、多角度、低延迟传输控制与内网/5G广域网融合场景
音视频·低延迟rtsp播放器·低延迟视频传输·座舱远程遥控·5g远程控制·远程驾驶系统·低延迟rtmp播放
EasyGBS4 小时前
从“后厨黑箱”到“透明厨房”:国标GB28181视频平台EasyGBS平台AI视频分析如何守护舌尖上的安全
人工智能·安全·音视频
昨日之日20064 小时前
LongCat-Video-Avatar-1.5 - 一句话生成口型同步、动作稳定的数字人 说话/唱歌 视频 一键整合包下载
音视频
searchforAI5 小时前
长视频和播客怎么变成结构化读书笔记?一套 AI 时代的知识管理方法
人工智能·笔记·gpt·音视频·语音识别
不想吃饭e6 小时前
uniapp-图片,视频上传组件封装
java·uni-app·音视频
做萤石二次开发的哈哈7 小时前
具备 ERTC 能力的萤石设备如何对接客户端通话?
音视频·实时音视频·萤石开放平台
电子元件小说家7 小时前
音频调音台直滑电位器选型:ALPS RK12L123000E 与国产同于科技替代方案评估
科技·音视频
人工智能研究所8 小时前
字节开源 OmniShow:文本,图片,音频,人体姿态多输入,一键成片
人工智能·神经网络·开源·音视频·开源软件·字节跳动·ai 视频