【问题标题】:How to start the default music player from my app?如何从我的应用程序启动默认音乐播放器?
【发布时间】:2011-06-07 13:27:16
【问题描述】:

我用ListView 制作了一个应用程序。当我点击 ListView 项目时,.ogg 声音文件应该开始播放。不在我的应用程序中,而是在用户的默认音乐播放器应用程序中。我该怎么做?

【问题讨论】:

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


    【解决方案1】:

    试试这个

    Intent it = new Intent(Intent.ACTION_VIEW);
    Uri uri = Uri.parse("file:///sdcard/song.mp3");
    it.setDataAndType(uri, "audio/mp3");
    startActivity(it);
    

    或者

    Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");   
    Intent it = new Intent(Intent.ACTION_VIEW, uri);   
    startActivity(it);  
    

    摘自http://snipt.net/Martin/android-intent-usage/

    我没有测试过自己。

    【讨论】:

    • 这只会播放内存中的歌曲或 sd 卡中的特定歌曲。如果我将所有歌曲从 SD 卡加载到媒体播放器,该怎么做。
    【解决方案2】:

    不知道这是否直接适用于该问题,但这是一种从您自己的应用打开 Play 音乐应用的方法:

    Intent i;
    PackageManager manager = getPackageManager();
    try {
    i = manager.getLaunchIntentForPackage("com.google.android.music");
    if (i == null)
         throw new PackageManager.NameNotFoundException();
    i.addCategory(Intent.CATEGORY_LAUNCHER);
    startActivity(i);
    } catch (PackageManager.NameNotFoundException e) {
    }
    

    【讨论】:

      【解决方案3】:

      试试这个:

          Intent intent = new Intent();
          intent.setAction(android.content.Intent.ACTION_VIEW);
          File file = new File(uri);
          intent.setDataAndType(Uri.fromFile(file), "audio/*");
          ActivityChatView.mContext.startActivity(intent);
      

      【讨论】:

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