【问题标题】:Launching the default music player启动默认音乐播放器
【发布时间】:2013-02-09 21:53:29
【问题描述】:

我正在尝试启动音乐播放器,以便它启动并立即开始播放第一首歌曲。我正在使用 Intent,但它不起作用...它说“没有找到处理 Intent 的活动”。

    Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
       //"songsList" is an array with paths of all the songs in the sdcard
    Uri uri = Uri.parse(songsList.get(0));
    String type = "audio/mp3";
    intent.setDataAndType(uri, type);
    startActivity(intent);

【问题讨论】:

  • 尝试媒体类型audio/* - 这是我使用的,它工作正常。也许没有任何东西将 audio/mp3 注册为特定的 mime 类型。
  • Uri 的值是多少?
  • 第一首歌的路径

标签: android android-intent android-music-player


【解决方案1】:

为什么不使用android.intent.action.MUSIC_PLAYER

Intent intent = new Intent("android.intent.action.MUSIC_PLAYER");
startActivity(intent);

请注意,这已从 API 15 中弃用,从 API 开始,您可以使用 android.intent.category.APP_MUSIC

【讨论】:

  • 这个真的很奇怪,它只启动了一个名为“playerpro”的音乐播放器,所以我从手机中删除了 playerpro,然后它就崩溃了
  • 您是否安装了其他音乐播放器?
  • 我是否需要在我的应用中添加任何其他内容才能启动音乐播放器...?
  • 现在我可以从列表中选择要使用的应用程序,但我看不到我所有的音乐播放器
  • 意图意图 = new Intent("android.intent.action.MUSIC_PLAYER");在 Nexus 4 上的 Android 4.2.2 上为我工作
【解决方案2】:
        Intent intent = new Intent();
        intent.setAction(android.content.Intent.ACTION_VIEW);
        File file = new File(AUDIO_PATH);
        intent.setPackage("com.google.android.music");          
        intent.setDataAndType(Uri.fromFile(file), "audio/*");
        mContext.startActivity(intent);

【讨论】:

    【解决方案3】:

    要启动默认音乐播放器,请尝试以下代码。

    try {
        Intent intent = Intent.makeMainSelectorActivity(Intent.ACTION_MAIN, Intent.CATEGORY_APP_MUSIC);
        getContext().startActivity(intent);
    } catch (Exception e) {
        Log.d(TAG, "Exception for launching music player "+e);
    }
    

    【讨论】:

    • 这是正确的方法。截至目前,所有其他建议的方法都已过时。
    【解决方案4】:

    好的,所以我找到了这个有效的代码

                    Intent intent = new Intent();  
                    intent.setAction(android.content.Intent.ACTION_VIEW);  
                    File file = new File(songsList.get(0));  
                    intent.setDataAndType(Uri.fromFile(file), "audio/*");  
                    startActivity(intent);
    

    但问题是,假设用户点击后退按钮,然后点击音乐播放器的按钮,它重新启动播放器并再次开始播放第一首歌曲...... 那么我如何在没有其他任何东西的情况下启动音乐播放器...?

    【讨论】:

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