如何获取已安装媒体播放器的列表 [英] How to get a list of installed media players

查看:13
本文介绍了如何获取已安装媒体播放器的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有一个菜单项,我想打开用户首选的媒体播放器,只对音频感兴趣.理想情况下,用户第一次选择这个项目时,如果他们只安装了一个媒体播放器,它会选择手机上唯一的媒体播放器,或者如果他们有多个媒体播放器,则向他们显示一个列表.然后我会保存他们的选择,以便下次打开那个.

In my app I have a menu item that I want to open up the user's preferred media player, only interested in audio. Ideally the first time the user chooses this item it would choose the only media player on the phone if they only have one installed, or present them with a list if they have more than one. I would then save off their choice so next time it would open that one.

据我所知,Android 没有默认的媒体播放器.我有原始的 Droid,它有一个媒体播放器,但我知道其他运营商使用他们自己的,或者用户可以卸载标准的.

As I understand it, Android has no default media player. I have the original Droid and it has a media player, but I understand other carriers use their own, or the user can uninstall the standard one.

我尝试了一些东西,但没有任何效果.

I have tried a few things but can't get anything to work.

我试过这段代码,它应该得到支持意图的包列表.它适用于诸如application/pdf"和video/*"之类的东西.当我用 "audio/*" 尝试它时,我得到一个空列表,即使我已经安装了库存的 Android 媒体播放器和 MixZing.也用media/*"试过了,一无所获.

I tried this code, which is supposed to get a list of packages that support the intent. It works for some things like "application/pdf" and "video/*". When I try it with "audio/*" I get an empty list, even though I have both the stock Android media player and MixZing installed. Have also tried it with "media/*" and get nothing.

PackageManager packageManager = getPackageManager();
Intent testIntent = new Intent(Intent.ACTION_VIEW);
testIntent.setType("audio/*");
List list = packageManager.queryIntentActivities(testIntent, 0);

我已经看到这段代码可以工作并使用默认播放器打开音频文件,但是我不想打开文件,我只想打开音频应用程序,就像用户从应用程序抽屉打开它一样.

I have seen this code which works and opens an audio file with the default player, however I don't want to open a file, I just want to open up the audio app as if the user opened it from the application drawer.

Intent i = new Intent(Intent.ACTION_VIEW);
Uri u = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI,"1"); 
i.setData(u); 
startActivity(i); 

我唯一能想到的就是去获取最流行的媒体播放器的包名并将它们硬编码并搜索手机以查看安装了哪些,但这似乎不是最好的方法.我不明白为什么第一部分代码不起作用.可能没有为这些应用正确设置意图过滤器,或者我使用了错误的代码或 MIME 类型.

The only other thing I can think to do is go out and get the package names of the most popular media players and hard code them in and search the phone to see which ones are installed, but this doesn't seem like the best approach. I don't understand why the first bit of code doesn't work. Maybe the intent filters aren't properly set for those apps or I am using the wrong code or mime types.

推荐答案

这里是我向操作系统询问已安装音频播放器列表的方式.

OK here is how I ask the OS for a list of installed audio players.

Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI,"1"); 
intent.setData(uri);
List<ResolveInfo> playerList;
playerList = packageManager.queryIntentActivities(intent, 0);

playerList 然后将为手机上声称处理音频的每个应用程序填充 ResolveInfo 对象.从 ResolveInfo 中,您可以获得诸如包名称之类的内容以启动活动,您可以获得图标和应用程序名称.我使用它来填充一个自定义对话框,该对话框的外观和行为类似于用于选择要启动的活动的股票 Android 对话框.我还可以让用户选择将所选应用设置为默认值.

playerList will then be populated with ResolveInfo objects for every app on the phone that claims to handle audio. From the ResolveInfo you can get things like package name to launch the activity, you can get icons and app names. I use it to populate a custom dialog that looks and acts like the stock Android dialog for choosing an activity to launch. I also have the option for the user to set the chosen app as default.

这并不完美.您受操作系统和应用程序的 intent 过滤器的支配.我在几部手机上尝试过,以下应用程序正常显示:原始 Droid 的库存媒体播放器、HTC 手机的库存媒体播放器、doubleTwist 和 MixZing.但是,我安装了 Zimly 并没有显示出来.我怀疑他们的意图过滤器没有为音频设置.

This isn't perfect. You are at the mercy of the OS and the app's intent filters. I have tried this with a few phones and the following apps show up properly: Stock media player for original Droid, stock media player for HTC phones, doubleTwist and MixZing. However, I installed Zimly and it does not show up. I suspect their intent filters aren't set up for audio.

我还担心可能会出现 MediaStore.Audio.Media.INTERNAL_CONTENT_URI 没有任何音频的情况.我在重新启动后立即尝试了此代码并且它有效.我还没有在没有安装用户媒体的手机上试过.

I was also worried there may be cases when MediaStore.Audio.Media.INTERNAL_CONTENT_URI does not have any audio. I have tried this code immediately following a reboot and it works. I have not tried it on a phone with no user media installed.

这篇关于如何获取已安装媒体播放器的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆