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

时间:2022-03-01 18:59:31

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

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

3 个解决方案

#1


13  

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

然后按照像pocketjourney这样的流媒体教程

But this is overly complicated as you can just call

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

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

#2


40  

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添加到块中。

#3


13  

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

释义@Kartik的答案获取存储在android中res / raw文件夹中的.mp3文件的URI

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

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

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

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

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

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

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

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

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

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

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

示例: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));

#1


13  

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

然后按照像pocketjourney这样的流媒体教程

But this is overly complicated as you can just call

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

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

#2


40  

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添加到块中。

#3


13  

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

释义@Kartik的答案获取存储在android中res / raw文件夹中的.mp3文件的URI

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

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

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

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

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

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

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

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

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

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

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

示例: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));