簡體   English   中英

MediaPlayer.setDataSource() 的演示?

[英]Demo of MediaPlayer.setDataSource()?

我正在學習 android 中的媒體播放器。

我想要一些簡單易懂的MediaPlayer.setDataSource()代碼示例。

好吧,為了更深入地了解MediaPlayer,最好閱讀官方文檔https://developer.android.com/reference/android/media/MediaPlayer#setDataSource(android.content.res.AssetFileDescriptor) 但是對於基本理解,這里是代碼示例。

MediaPlayer mp = new MediaPlayer();

        // Here you may set which stream to use either MEDIA or ALARM etc.
        mp.setAudioStreamType(AudioManager.STREAM_ALARM);
        try {
            if (isAnyActiveSongExist){
                // Here you may set dataSource as path of the file
                mp.setDataSource(firstPrioritySongEntityPath);
            }
            else{
                // Here you may set dataSource using Uri
                mp.setDataSource(context, Settings.System.DEFAULT_RINGTONE_URI);
            }

            mp.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }
        mp.start();

setDataSource() 定義了您的 MediaPlayer 應該使用哪個文件來播放。

首先,代碼從來都不是簡單易行的,除非你不會 go 通過它。 檢查這個鏈接點擊這里,我想你會從這里得到你的答案

關於 setDataSource(String) 調用

看到您的評論后,您似乎確實希望將 setDataSource(string) 用於您的目的。 我不明白為什么。 但是,我認為,出於某種原因,您正試圖避免使用“上下文”。 如果不是這種情況,那么上述兩種解決方案應該非常適合您,或者如果您試圖避免上下文,恐怕使用帶有簽名 setDataSource(String) 調用的 function 是不可能的。 原因如下,

MediaPlayer setDataSource() function 有以下這些選項,您只對 setDataSource(String) 感興趣,

在此處輸入圖像描述

public void setDataSource(String path)
            throws IOException, IllegalArgumentException, SecurityException, IllegalStateException {
        setDataSource(path, null, null);
    }

如果您檢查 setDataSource(String path, String[] keys, String[] values) 代碼,您將看到以下條件根據其方案過濾路徑,特別是如果它是“文件”方案,它調用 setDataSource(FileDescriptor) 或如果 scheme 不是“文件”,它會調用原生 JNI 媒體 function。

{
        final Uri uri = Uri.parse(path);
        final String scheme = uri.getScheme();
        if ("file".equals(scheme)) {
            path = uri.getPath();
        } else if (scheme != null) {
            // handle non-file sources
            nativeSetDataSource(
                MediaHTTPService.createHttpServiceBinderIfNecessary(path),
                path,
                keys,
                values);
            return;
        }
        final File file = new File(path);
        if (file.exists()) {
            FileInputStream is = new FileInputStream(file);
            FileDescriptor fd = is.getFD();
            setDataSource(fd);
            is.close();
        } else {
            throw new IOException("setDataSource failed.");
        }
}

在上面的代碼中,您的資源文件 URI 方案不會是 null (android.resource://) 和 setDataSource(String) 將嘗試使用本機 JNI function nativeSetDataSource() 認為您的路徑是 http/https/rtsp 顯然call 也會失敗,不會拋出任何異常。 這就是為什么您對 setDataSource(String) 的調用毫無例外地轉義並通過以下異常進行 prepare() 調用。

Prepare failed.: status=0x1

因此 setDataSource(String) 覆蓋無法處理您的資源文件。 您需要為此選擇另一個覆蓋。

另一方面,檢查 setDataSource(Context context, Uri uri) 使用的 setDataSource(Context context, Uri uri, Map headers),它使用 AssetFileDescriptor, ContentResolver from your context 和 openAssetFileDescriptor 打開作為 openAssetFileDescriptor( ) 可以打開您的資源文件,最后生成的 fd 用於調用 setDataSource(FileDescriptor) 覆蓋。

 AssetFileDescriptor fd = null;
    try {
        ContentResolver resolver = context.getContentResolver();
        fd = resolver.openAssetFileDescriptor(uri, "r");
        //  :
        //  :
        //  :
        if (fd.getDeclaredLength() < 0) {
                setDataSource(fd.getFileDescriptor());
            } else {
                setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getDeclaredLength());
            }

總而言之,您不能按原樣使用 setDataSource(String) 覆蓋來使用您的資源 mp3 文件。 相反,如果您想使用字符串播放資源文件,您可以使用 MediaPlayer.create() static function 和上面給出的 getIdentifier() 或更新#1 中給出的 setDataSource(context,uri)。

更多了解請參考這里的完整源代碼: Android MediaPlayer

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM