Android 在 MediaPlayer 中将数据源设置为原始 ID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1283499/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:54:10  来源:igfitidea点击:

Setting data source to an raw ID in MediaPlayer

androidandroid-mediaplayer

提问by russoue

In MediaPlayer.create method an id to a Raw file can be used but how to use that in setDataSource method?

在 MediaPlayer.create 方法中,可以使用原始文件的 id 但如何在 setDataSource 方法中使用它?

采纳答案by stealthcopter

You can load the raw audio into an input stream and load it into a MediaPlayer as you would a normal stream:

您可以将原始音频加载到输入流中,然后将其加载到 MediaPlayer 中,就像加载普通流一样:

InputStream ins = getResources().openRawResource(R.raw.example);

and then follow a streaming tutorial like pocketjourney

然后按照像口袋旅行这样的流媒体教程

But this is overly complicated as you can just call

但这太复杂了,因为你可以打电话

mp = MediaPlayer.create(counterstrikesb.this, R.raw.example);

回答by Chris.Zou

Refer to the source android.media.MediaPlayer

参考源码android.media.MediaPlayer

AssetFileDescriptor afd = context.getResources().openRawResourceFd(resid);
if (afd == null) return;
mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
afd.close();

You may want to add try-catch to the block.

您可能希望将 try-catch 添加到块中。

回答by Dheeraj Bhaskar

paraphrasing @Kartik's answer here Get URI of .mp3 file stored in res/raw folder in android

在此处解释@Kartik 的回答Get URI of .mp3 file stored in res/raw folder in android

If you want to get any resource URI then there are two ways :

如果您想获取任何资源 URI,则有两种方法:

  1. Using Resource Name
  1. 使用资源名称

Syntax : android.resource://[package]/[res type]/[res name]

Example : Uri.parse("android.resource://com.my.package/drawable/icon");

语法:android.resource://[package]/[res type]/[res name]

示例:Uri.parse("android.resource://com.my.package/drawable/icon");

  1. Using Resource Id
  1. 使用资源 ID

Syntax : android.resource://[package]/[resource_id]

Example : Uri.parse("android.resource://com.my.package/" + R.drawable.icon);

语法:android.resource://[package]/[resource_id]

示例:Uri.parse("android.resource://com.my.package/" + R.drawable.icon);

These were the examples to get the URI of any image file stored in drawable folder. Similarly you can get URIs of res/raw folder.

这些是获取存储在 drawable 文件夹中的任何图像文件的 URI 的示例。同样,您可以获得 res/raw 文件夹的 URI。

IMO the second way would be preferred as renaming the resource etc can be easily refactored.

IMO 首选第二种方式,因为重命名资源等可以轻松重构。

Set the data source like so:

像这样设置数据源:

CONSTANTS.RES_PREFIX = "android.resource://com.my.package/"
mp.setDataSource(getApplicationContext(),
              Uri.parse(CONSTANTS.RES_PREFIX + R.raw.id));
mp.setDataSource(getApplicationContext(),
              Uri.parse(CONSTANTS.RES_PREFIX + R.raw.id));