【问题标题】:How To Launch the default Music App in android to play a particular song?如何在 android 中启动默认音乐应用程序来播放特定歌曲?
【发布时间】:2017-06-02 12:19:26
【问题描述】:

我想通过默认媒体播放器播放 url 中的特定歌曲

我可以使用Play song on default music player - android播放它

但它并没有完全打开应用程序来播放它

我也可以使用以下代码打开音乐播放器

Intent intent = Intent.makeMainSelectorActivity(Intent.ACTION_MAIN,Intent.CATEGORY_APP_MUSIC);


startActivity(intent);

我怎样才能做到这两点?

【问题讨论】:

  • 你的意思是“它没有完全打开应用程序来播放它”请解释一下。
  • 它推出了一个紧凑版本的播放器,正如stackoverflow.com/questions/30937370/… 中提到的那样,您可以在该帖子中看到屏幕截图。

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


【解决方案1】:

每个制造商都会有其默认的音乐播放器,但如果您仍想打开谷歌提供的默认安卓音乐播放器,即 Google Play 音乐,那么您可以使用以下代码打开它:-

如果已安装此代码将打开 Google Play 音乐,否则将打开其他音乐播放器。

protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    //Check for Google Play Music exist
    if (isPackageInstalled("com.google.android.music", getPackageManager())) 
    {
        Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.google.android.music");
        startActivity(LaunchIntent);
    } 
    else
    {
    else
    {
        //Your previous code goes here
        Intent intent = new Intent();
        intent.setAction(android.content.Intent.ACTION_VIEW);
        File file = new File(Environment.getExternalStorageDirectory().getPath()+"/alarm.mp3");
        if (file!=null)
        {
            intent.setDataAndType(Uri.fromFile(file), "audio/*");
            startActivity(intent);
        }
        else
        {
            Toast.makeText(Music.this,"Sound Track missing",Toast.LENGTH_LONG);
        }
    }

}

private boolean isPackageInstalled(String packagename, PackageManager packageManager) 
{
    try 
    {
        packageManager.getPackageInfo(packagename, 0);
        return true;
    } 
    catch (PackageManager.NameNotFoundException e) 
    {
        return false;
    }
}

【讨论】:

  • 这可能会打开默认音乐播放器。但实际的问题是如何在该音乐播放器中播放特定歌曲,例如 File file = new File(Environment.getExternalStorageDirectory().getPath()+"/在地平线上.mp3");
  • 它推出了一个紧凑版本的播放器,正如stackoverflow.com/questions/30937370/… 中提到的那样,您可以在该帖子中看到屏幕截图。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-14
相关资源
最近更新 更多