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

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

相关推荐
昨日之日20061 小时前
WanGP V54:极致低显存再突破,更快生成与智能制作全面升级!低显存玩转一站式AI创作神器,视频图像3D世界音乐全搞定
人工智能·3d·音视频
Likeadust2 小时前
险情发生,会已经开起来了:私有化音视频实时通信/视频会议平台EasyDSS临时会商如何抢出黄金处置时间
网络·音视频·easydss
林墨聊AIGC4 小时前
AI逐帧动画量产秘籍:从零到商业级
大数据·人工智能·科技·ai作画·aigc·音视频
万岳科技系统开发6 小时前
短视频直播定制开发如何适配不同终端?APP、小程序与网页端的功能差异
小程序·音视频
Android系统攻城狮7 小时前
FFmpeg深度解析之av_frame_apply_cropping:如何裁剪视频有效画面?(九十)
ffmpeg·音视频·车载音视频·ffmpeg音视频
豪气的程序猿7 小时前
Win11播HEVC视频报0x80040265排查与D3D11硬解管线配置
3d·电脑·音视频
卢卡斯2338 小时前
8个免费无版权可商用视频素材网站,2026最新分享!
音视频
lingyubb20 小时前
适合短视频配音 操作简单的AI配音软件推荐怎么落地?从文案到成片的四步实操拆解
人工智能·音视频·状态模式
Kylin5241 天前
用 Python 做批量音频频谱分析:从 M4A 解码到频带能量统计
音视频
Black蜡笔小新1 天前
实时音视频通信/WebRTC音视频开发不用自研!EasyRTC把音视频嵌入式WebRTC原生SDK做好了!
音视频·webrtc·实时音视频