Android8.0打开手机所有的音乐播放器

上篇文章录取的电话音频,这是用来读取播放方法
//path 文件路径
public void play(String path) {

    Intent intent = new Intent(Intent.ACTION_VIEW);
    Log.e("TAG", path);
    intent.addCategory("android.intent.category.DEFAULT");
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION
            | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
    File file = new File(path);
    if (!file.exists()){
        Log.e("TAG","不存在");
        return;
    }

    Uri photoURI = FileProvider.getUriForFile(this, getApplicationContext().getPackageName()+".provider" , file);
    Log.e("TAG",photoURI.toString());
    intent.setDataAndType(photoURI,"audio/*");

    try {
        startActivity(Intent.createChooser(intent,"录音"));
    } catch (Exception e) {
        e.printStackTrace();
    }

//
}

//manifest文件配置

<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.shiliu.callrecording.provider"//你的包名
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
//如果你依赖的第三方库中已经有了android.support.v4.content.FileProvider你可以自定义一个MyFileProvider继承FileProvider
eg:
<provider
android:name=".MyFileProvider"
android:authorities="com.shiliu.callrecording.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,773评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,831评论 25 709
  • 用两张图告诉你,为什么你的 App 会卡顿? - Android - 掘金 Cover 有什么料? 从这篇文章中你...
    hw1212阅读 14,498评论 2 59
  • 大姐在小时候对我很不好,也是,来了个女人,抢走了爸爸,她生的女儿又怎么让人么会喜欢呢。妈妈告诉我小时候大姐都不让我...
    唯左阅读 1,449评论 0 0
  • 文/琴音 2018.4.18 “妈妈,我觉得你跟别人的妈妈都不一样的。” “怎么不一样了?”这家伙又语出惊...
    王燕惠阅读 2,994评论 0 3