获取URI的音频元数据

huangapple go评论66阅读模式
英文:

Android:Get audio metadata from URI

问题

目的:
我使用Flutter创建了一个音乐播放器。我想从另一个应用程序播放音频文件。
为此,我使用了方法通道在Flutter和Android之间进行通信。我从Intent中获取到以下URI。

接收到的URI:content://com.mi.android.globalFileexplorer.myprovider/external_files/song.mp3
接收到的路径:/external_files/song.mp3


**我的代码**

```java
private MediaMetadataRetriever song details;
private MetadataDetails[] songDetails;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    songDetails = new MediaMetadataRetriever();
    Intent intent = getIntent();

    new MethodChannel(AudioServicePlugin.getFlutterEngine(this).getDartExecutor().getBinaryMessenger(), CHANNEL).setMethodCallHandler(
            (call, result) -> {
                if (call.method.equals("song")) {
                    if(intent != null && intent.getData() !=null) {
                        songDetails.setDataSource(intent.getData().getPath());
                        result.success(songDetails);
                    }
                    else {
                        result.success(null);
                    }
                }
            });
}

当我获取songDetails时,我遇到以下错误。

错误:setDataSource失败:状态=0xFFFFFFEA

我尝试了下面的代码。

FileInputStream file = new FileInputStream(intent.getData().getPath());
songDetails.setDataSource(file.getFD());

但对我来说没有起作用。

有人能指导我如何做吗?


<details>
<summary>英文:</summary>

Aim:
I have created a music player using flutter. And I want to play audio files from another app.
For this purpose, I have used method channel for communication between flutter and Android.And I am getting the below URI from Intent.

Received URI:content://com.mi.android.globalFileexplorer.myprovider/external_files/song.mp3
Received PATH:/external_files/song.mp3

**My Code**


private MediaMetadataRetriever song details;
private MetadataDetails[] songDetails;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    songDetails = new MediaMetadataRetriever();
    Intent intent = getIntent();

    new MethodChannel(AudioServicePlugin.getFlutterEngine(this).getDartExecutor().getBinaryMessenger(), CHANNEL).setMethodCallHandler(
            (call, result) -&gt; {
                if (call.method.equals(&quot;song&quot;)) {
                    if(intent != null &amp;&amp; intent.getData() !=null) {
                        songDetails.setDataSource(intent.getData().getPath());
                        result.success(songDetails);
                    }
                    else {
                        result.success(null);
                    }
                }
            });
}
When I am fetching songDetails, I am getting the below error.


ERROR:setDataSource failed: status = 0xFFFFFFEA


 

I have tried below code.


FileInputStream file = new FileInputStream(intent.getData().getPath());
songDetails.setDataSource(file.getFD());


But it didn&#39;t work for me.

Can anyone guide me on how to do it?

</details>


# 答案1
**得分**: 5

请检查以下函数:

```java
private String generatePath(Uri uri, Context context) {
    if ("content".equalsIgnoreCase(uri.getScheme())) {
        String[] projection = { "_data" };
        Cursor cursor = null;

        try {
            cursor = context.getContentResolver().query(uri, projection, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow("_data");
            if (cursor.moveToFirst()) {
                return cursor.getString(column_index);
            }
        } catch (Exception e) {
            // 忽略异常
        }
    } else if ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    }
    return null;
}

将您的文件传递为:

Uri selectedFileUri = intent.getData();
File filePath = new File(generatePath(selectedFileUri, this));
英文:

Please check with following function

 private String generatePath(Uri uri, Context context){
        if (&quot;content&quot;.equalsIgnoreCase(uri.getScheme())) {
            String[] projection = { &quot;_data&quot; };
            Cursor cursor = null;

            try {
                cursor = context.getContentResolver().query(uri, projection, null, null, null);
                int column_index = cursor.getColumnIndexOrThrow(&quot;_data&quot;);
                if (cursor.moveToFirst()) {
                    return cursor.getString(column_index);
                }
            } catch (Exception e) {
                // Eat it
            }
        }
        else if (&quot;file&quot;.equalsIgnoreCase(uri.getScheme())) {
            return uri.getPath();
        }
        return null;
    }

Pass your file as

 Uri selectedFileUri = intent.getData();
 File filePath = new File(generatePath(selectedFileUri,this));

huangapple
  • 本文由 发表于 2023年1月9日 15:02:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75054035.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定