【问题标题】:Android: playing a song file using default music playerAndroid:使用默认音乐播放器播放歌曲文件
【发布时间】:2012-06-03 04:44:55
【问题描述】:

有没有办法使用默认媒体播放器播放媒体?我可以用以下代码做到这一点:

 Intent intent = new Intent(Intent.ACTION_VIEW);
 MimeTypeMap mime = MimeTypeMap.getSingleton();
 String type = mime.getMimeTypeFromExtension("mp3");
 intent.setDataAndType(Uri.fromFile(new File(songPath.toString())), type);
 startActivity(intent);

但这会启动一个控制较少的播放器,并且不能被推到后台。 我可以使用默认媒体播放器启动播放器吗?

【问题讨论】:

    标签: android android-mediaplayer media


    【解决方案1】:

    试试下面的代码:::

       Intent intent = new Intent(MediaStore.INTENT_ACTION_MUSIC_PLAYER);  
       File file = new File(songPath.toString());  
       intent.setDataAndType(Uri.fromFile(file), "audio/*");  
       startActivity(intent);
    

    更新:: 也试试这个

       Intent intent = new Intent();  
       ComponentName comp = new ComponentName("com.android.music", "com.android.music.MediaPlaybackActivity");
       intent.setComponent(comp);
       intent.setAction(android.content.Intent.ACTION_VIEW);  
       File file = new File(songPath.toString());  
       intent.setDataAndType(Uri.fromFile(file), "audio/*");  
       startActivity(intent);
    

    【讨论】:

    • 谢谢!但它的作用与我的代码相同。我想启动默认媒体播放器。
    • 它会按照我的想法启动默认播放器.. 还更新了我的帖子检查它
    • 第二个选项有效。我们需要在意图中设置组件对象。谢谢!
    • 我在使用第二个选项时遇到了 Activitynotfoundexception。我不知道为什么会这样!!
    • 我收到此日志:无法找到明确的活动类 {com.android.music/com.android.music.MediaPlaybackActivity};您是否在 AndroidManifest.xml 中声明了此活动?
    【解决方案2】:

    最近几天我一直在研究这个,因为我没有股票音乐播放器。似乎很悲惨,不能轻易做到。在查看了各种音乐应用程序的 AndroidManifest.xml 以寻找线索后,我偶然发现了 MediaStore.INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH。

    使用以下方法,只要歌曲在 Android MediaStore 中,我就可以在后台启动三星音乐播放器。您可以指定艺术家、专辑或标题。此方法也适用于 Google Play 音乐,但不幸的是,即使是最新版本的 Android 播放器也没有此意图:

    https://github.com/android/platform_packages_apps_music/blob/master/AndroidManifest.xml

    private boolean playSong(String search){
        try {
            Intent intent = new Intent();
            intent.setAction(MediaStore.INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtra(SearchManager.QUERY, search);
            startActivity(intent);
            return true;
        } catch (Exception ex){
            ex.printStackTrace();
            // Try other methods here
            return false;
        }
    }
    

    很高兴找到使用内容 URI 或 URL 的解决方案,但此解决方案适用于我的应用程序。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多