加载从URI的MediaPlayer的在上下文中的资产 [英] load a MediaPlayer from a Uri in a context assets
问题描述
我在我的资产目录中的音频文件。资产/音频/ dance.mp3。
如果我运行context.getAssets()列表(音频)。它显示了。
但是,当我尝试使用MediaPlayer.create(背景下,URI)它总是失败并返回null。
这一切都不似乎工作
私人无效tryLoad(字符串路径,上下文的背景下)
{
URI URI = Uri.parse(路径);
this.audioPlayer = MediaPlayer.create(背景下,URI);
如果(this.audioPlayer == NULL)
{
Log.d(TAG,loadAudio:audioPlayer为null流动资产。+ uri.toString());
}
其他
{
Log.d(TAG,loadAudio:保护正常工作+ uri.toString());
}}
公共无效loadAudio(上下文的背景下)
{
如果(this.audioPlayer!= NULL)
返回;
如果(this.audioFile =空&放大器;!&放大器; this.audioFile.length()大于0)
{
尝试
{
tryLoad(/ dance.mp3,背景);
tryLoad(dance.mp3,背景);
tryLoad(音频/ dance.mp3,背景);
tryLoad(/音频/ dance.mp3,背景);
tryLoad(资产/音频/ dance.mp3,背景);
tryLoad(/资产/音频/ dance.mp3,背景);
tryLoad(\\\\ dance.mp3,背景);
tryLoad(dance.mp3,背景);
tryLoad(音频\\\\ dance.mp3,背景);
tryLoad(\\\\ \\\\音频dance.mp3,背景);
tryLoad(资产\\\\ \\\\音频dance.mp3,背景);
tryLoad(\\\\ \\\\资产\\\\音频dance.mp3,背景);
}
赶上(例外五)
{
Log.d(TAG,loadAudio异常:+ e.getMessage());
}
}
}
我觉得因为的MediaPlayer
期待一个URI你可以做到这一点,这是不可能创建一个URI为一个资产文件。尝试创建一个的MediaPlayer
对象,并使用 MediaPlayer.setDataSource(的FileDescriptor)
方法的数据源设置它。您可以使用 AssetManager.openFd()
方法获得的FileDescriptor
对象,然后调用 AssetFileDescriptor.getFileDescriptor()
方法返回的对象。
我没有试过这种解决方案,所以它只是一个想法。但我希望它会工作。
I have an audio file in my assets directory. assets/audio/dance.mp3.
If I run context.getAssets().list("audio"); it shows up.
But when I try to use MediaPlayer.create(context,uri) it always fails and returns null.
none of this seems to work
private void tryLoad(String path,Context context)
{
Uri uri = Uri.parse(path);
this.audioPlayer = MediaPlayer.create(context,uri);
if (this.audioPlayer == null)
{
Log.d(TAG, "loadAudio: audioPlayer is null. current assets"+ uri.toString()) ;
}
else
{
Log.d(TAG, "loadAudio: WORKED"+ uri.toString()) ;
}
}
public void loadAudio(Context context)
{
if (this.audioPlayer != null)
return;
if (this.audioFile != null && this.audioFile.length() >0)
{
try
{
tryLoad("/dance.mp3",context);
tryLoad("dance.mp3",context);
tryLoad("audio/dance.mp3",context);
tryLoad("/audio/dance.mp3",context);
tryLoad("assets/audio/dance.mp3",context);
tryLoad("/assets/audio/dance.mp3",context);
tryLoad("\\dance.mp3",context);
tryLoad("dance.mp3",context);
tryLoad("audio\\dance.mp3",context);
tryLoad("\\audio\\dance.mp3",context);
tryLoad("assets\\audio\\dance.mp3",context);
tryLoad("\\assets\\audio\\dance.mp3",context);
}
catch (Exception e)
{
Log.d(TAG, "loadAudio exception: " + e.getMessage());
}
}
}
I think you can do it because the MediaPlayer
expects an URI and it's impossible to create an URI for an assets file. Try creating a MediaPlayer
object and setting a data source to it using the MediaPlayer.setDataSource(FileDescriptor)
method. You can get the FileDescriptor
object using the AssetManager.openFd()
method and then calling the AssetFileDescriptor.getFileDescriptor()
method for the returned object.
I haven't tried this solution, so it's just an idea. But I hope it'll work.
这篇关于加载从URI的MediaPlayer的在上下文中的资产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!