How do I use custom android.media.MediaDataSource along with android.media.MediaPlayer?

前端 未结 1 872
-上瘾入骨i
-上瘾入骨i 2021-01-18 04:33

I know Android\'s MediaPlayer is a great thing. It allows us to play local files as well as media streams. And it is quite easy to use (just for example):

1条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-18 05:08

    The main point is that the MediaPlayer does playback audio/mpeg (both ways - through URL and through custom MediaDataSource), but audio/aacp streams could be played back only via URL as DataSource.

    So, let's understand what's happening under the hoods.

    When you are passing an URL as a data source, then this check is being performed:

    
        if ("file".equals(scheme)) {
            path = uri.getPath();
        } else if (scheme != null) {
            // handle non-file sources
            nativeSetDataSource(
                MediaHTTPService.createHttpServiceBinderIfNecessary(path),
                path,
                keys,
                values);
            return;
        }
    
    

    MediaPlayer uses MediaHTTPService class, which is responsible for providing data from http, https and widevine protocols. MediaHTTPService internally uses MediaHTTPConnection, which takes all the heavy lifting for working with that type of streams. Unfortunately, these APIs are not public (yet), but you can see how connection establishing is done in MediaHTTPConnection sources (particularly seekTo method). So, the custom data source that you provide to MediaPlayer should depict approximately the logics, that MediaHTTPConnection class implements.

    0 讨论(0)
提交回复
热议问题