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

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

相关推荐
QT 小鲜肉19 分钟前
【孙子兵法之终篇】《孙子兵法》真人阅读、朗读、讲解的视频链接
网络·笔记·音视频·读书·孙子兵法
路溪非溪1 小时前
音频相关基础知识2
音视频
一点晖光2 小时前
ffmpeg合并视频
ffmpeg·音视频
简鹿视频2 小时前
mp4视频转换成avi格式攻略
ffmpeg·音视频·视频编解码·格式工厂
IT·小灰灰3 小时前
当AI开口说话:可灵视频2.6如何终结“默片时代“重塑视听共生
大数据·人工智能·python·深度学习·数据挖掘·开源·音视频
小尧嵌入式3 小时前
深入理解C/C++指针
java·c语言·开发语言·c++·qt·音视频
IT·小灰灰4 小时前
AI成为精确的执行导演:Runway Gen-4.5如何用控制美学重塑社媒视频工业
大数据·图像处理·人工智能·python·数据分析·音视频
xingqing87y4 小时前
视频后面声音慢慢变小怎么调
音视频
音视频牛哥4 小时前
具身智能时代的音视频架构重构:从延迟到多模态的技术挑战
人工智能·计算机视觉·音视频·具身智能·具身智能低延迟方案·智能机器人rtsp rtmp·rtsp、rtmp低延迟播放器
yong999017 小时前
基于MATLAB的帧间差分法视频目标检测实现
目标检测·matlab·音视频