英文:
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) -> {
if (call.method.equals("song")) {
if(intent != null && 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'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 ("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) {
// Eat it
}
}
else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
return null;
}
Pass your file as
Uri selectedFileUri = intent.getData();
File filePath = new File(generatePath(selectedFileUri,this));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论