Android使用intent启动音乐播放器

时间:2022-01-02 21:26:18

Is it possible to open the music app from my app in android, or is it best to write a whole new music app inside of mine. I would rather use theirs since the user will already be comfortable with it.

是否有可能从Android中的应用程序打开音乐应用程序,或者最好在我的内部编写一个全新的音乐应用程序。我宁愿使用他们的,因为用户已经习惯了。

5 个解决方案

#1


82  

I found one way to do this.

我找到了一种方法来做到这一点。

Intent intent = new Intent();  
intent.setAction(android.content.Intent.ACTION_VIEW);  
intent.setDataAndType(Uri.parse(YOUR_SONG_PATH), "audio/*");  
startActivity(intent);

#2


19  

To simply launch the music player do:

要简单地启动音乐播放器:

Intent intent = new Intent(MediaStore.INTENT_ACTION_MUSIC_PLAYER);
startActivity(intent);

#3


3  

           if (Build.VERSION.SDK_INT >= 24) {
                try {
                    Method m = StrictMode.class.getMethod("disableDeathOnFileUriExposure");
                    m.invoke(null);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            Intent intent = new Intent();
            intent.setAction(android.content.Intent.ACTION_VIEW);
            File file = new File(YOUR_SONG_URI);
            intent.setDataAndType(Uri.fromFile(file), "audio/*");
            startActivity(intent);

#4


0  

You can also try this one.

你也可以试试这个。

Intent intent = new Intent(MediaStore.INTENT_ACTION_MUSIC_PLAYER);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

#5


0  

There are number of way by which you can achieve default audio player but those are device and OS specific.

有多种方法可以实现默认音频播放器,但这些是设备和操作系统特定的。

With this code snippet you can get default audio player.

使用此代码段,您可以获得默认音频播放器。

try
    {
    Intent myIntent = new Intent(android.content.Intent.ACTION_VIEW);
    File file = new File("audiofilepath"); 
    String extension = android.webkit.MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(file).toString());
    String mimetype = android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
    myIntent.setDataAndType(Uri.fromFile(file),mimetype);
    startActivity(myIntent);
    }
    catch (Exception e) 
    {
         e.printStackTrace();
    }

#1


82  

I found one way to do this.

我找到了一种方法来做到这一点。

Intent intent = new Intent();  
intent.setAction(android.content.Intent.ACTION_VIEW);  
intent.setDataAndType(Uri.parse(YOUR_SONG_PATH), "audio/*");  
startActivity(intent);

#2


19  

To simply launch the music player do:

要简单地启动音乐播放器:

Intent intent = new Intent(MediaStore.INTENT_ACTION_MUSIC_PLAYER);
startActivity(intent);

#3


3  

           if (Build.VERSION.SDK_INT >= 24) {
                try {
                    Method m = StrictMode.class.getMethod("disableDeathOnFileUriExposure");
                    m.invoke(null);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            Intent intent = new Intent();
            intent.setAction(android.content.Intent.ACTION_VIEW);
            File file = new File(YOUR_SONG_URI);
            intent.setDataAndType(Uri.fromFile(file), "audio/*");
            startActivity(intent);

#4


0  

You can also try this one.

你也可以试试这个。

Intent intent = new Intent(MediaStore.INTENT_ACTION_MUSIC_PLAYER);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

#5


0  

There are number of way by which you can achieve default audio player but those are device and OS specific.

有多种方法可以实现默认音频播放器,但这些是设备和操作系统特定的。

With this code snippet you can get default audio player.

使用此代码段,您可以获得默认音频播放器。

try
    {
    Intent myIntent = new Intent(android.content.Intent.ACTION_VIEW);
    File file = new File("audiofilepath"); 
    String extension = android.webkit.MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(file).toString());
    String mimetype = android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
    myIntent.setDataAndType(Uri.fromFile(file),mimetype);
    startActivity(myIntent);
    }
    catch (Exception e) 
    {
         e.printStackTrace();
    }