如何访问 Android 默认音乐应用程序创建的播放列表并调用音乐应用程序来播放它?

发布于 2024-12-03 15:15:26 字数 241 浏览 2 评论 0原文

我正在编写一个 Android 应用程序,我想访问由 Android 默认音乐应用程序创建的播放列表。

在我的应用程序中,用户应该能够浏览播放列表并选择任何播放列表进行播放。

所以基本上我想知道如何访问它以及当用户选择任何播放列表时,如何将其传递到默认音乐应用程序以在后台播放。

与 ContentProvider 或 mediastore 有关吗?我真的不知道如何访问其他应用程序上的数据。

太感谢了!

I am writing an android app, and I want to access the playlist created by android default music app.

In my app, the user should be able to browse the playlist and select any playlist to play.

So basically I want to know how to access it and when user selects any playlist, how to pass it to default music app to play it in background.

Is it something to do with ContentProvider or mediastore?? I really don't know how to access data on other apps.

Thank you so much!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

黑寡妇 2024-12-10 15:15:26

要播放上面播放列表中的歌曲,我调用该函数

PlaySongsFromAPlaylist( PlayListID ); // 0 播放列表ID<计数


来自上面的 onCreate 方法。其余代码如下所述。

public void PlaySongsFromAPlaylist(int playListID){

    String[] ARG_STRING = {MediaStore.Audio.Media._ID,MediaStore.Audio.Media.DATA,MediaStore.Audio.Media.DISPLAY_NAME,MediaStore.Video.Media.SIZE,android.provider.MediaStore.MediaColumns.DATA};
    Uri membersUri = MediaStore.Audio.Playlists.Members.getContentUri("external", playListID);
    Cursor songsWithingAPlayList = mThis.managedQuery(membersUri, ARG_STRING, null, null, null);
    int theSongIDIwantToPlay = 0; // PLAYING FROM THE FIRST SONG
    if(songsWithingAPlayList != null)
    {
        songsWithingAPlayList.moveToPosition(theSongIDIwantToPlay);
        String DataStream = songsWithingAPlayList.getString(4);
        PlayMusic(DataStream);
        songsWithingAPlayList.close();
    }   
}

 public static void PlayMusic(String DataStream){
    MediaPlayer mpObject = new MediaPlayer();
    if(DataStream == null)
        return;
    try {
        mpObject.setDataSource(DataStream);
        mpObject.prepare();
        mpObject.start();
    } catch (Exception e) {
        e.printStackTrace();
    }
 }

希望这会起作用。 :)

To play the songs from above playlists, I m calling the function

PlaySongsFromAPlaylist( PlayListID ); // 0 < PlayListID < count

from the above onCreate method. And remaining code is as per mentioned below.

public void PlaySongsFromAPlaylist(int playListID){

    String[] ARG_STRING = {MediaStore.Audio.Media._ID,MediaStore.Audio.Media.DATA,MediaStore.Audio.Media.DISPLAY_NAME,MediaStore.Video.Media.SIZE,android.provider.MediaStore.MediaColumns.DATA};
    Uri membersUri = MediaStore.Audio.Playlists.Members.getContentUri("external", playListID);
    Cursor songsWithingAPlayList = mThis.managedQuery(membersUri, ARG_STRING, null, null, null);
    int theSongIDIwantToPlay = 0; // PLAYING FROM THE FIRST SONG
    if(songsWithingAPlayList != null)
    {
        songsWithingAPlayList.moveToPosition(theSongIDIwantToPlay);
        String DataStream = songsWithingAPlayList.getString(4);
        PlayMusic(DataStream);
        songsWithingAPlayList.close();
    }   
}

 public static void PlayMusic(String DataStream){
    MediaPlayer mpObject = new MediaPlayer();
    if(DataStream == null)
        return;
    try {
        mpObject.setDataSource(DataStream);
        mpObject.prepare();
        mpObject.start();
    } catch (Exception e) {
        e.printStackTrace();
    }
 }

Hope this will work. :)

故事灯 2024-12-10 15:15:26

此活动结合了 Ankur Pandya 的两个答案,列出设备上的播放列表,然后播放第一个播放列表中的第一首曲目。

package com.withoutstones.pandyaplaylists;

import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;

/**
 * @author Ankur Pandya
 * @author Tommy Herbert
 */
public class PandyaPlaylistsActivity extends Activity {

    // constants

    private static final String LOGGING_TAG = "PandyaPlaylists";

    // static methods

    /**
     * Create a MediaPlayer and play the specified audio file. Note that a full app would usually
     * call stop() and release() on the player after use.
     * 
     * @param path
     *            to data file
     */
    public static void playAudio(final String path) {
        final MediaPlayer player = new MediaPlayer();
        if (path == null) {
            Log.e(LOGGING_TAG, "Called playAudio with null data stream.");
            return;
        }
        try {
            player.setDataSource(path);
            player.prepare();
            player.start();
        } catch (Exception e) {
            Log.e(LOGGING_TAG, "Failed to start MediaPlayer: " + e.getMessage());
            return;
        }
    }

    // superclass overrides

    @Override
    public void onCreate(final Bundle savedInstanceState) {

        // Create the superclass portion of the object.
        super.onCreate(savedInstanceState);

        // Set up the UI.
        this.setContentView(R.layout.main);

        // Get a cursor over all playlists.
            final ContentResolver resolver = this.getContentResolver();
        final Uri uri = MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI;
        final String idKey = MediaStore.Audio.Playlists._ID;
        final String nameKey = MediaStore.Audio.Playlists.NAME;
        final String[] columns = { idKey, nameKey };
        final Cursor playLists = resolver.query(uri, columns, null, null, null);
        if (playLists == null) {
            Log.e(LOGGING_TAG, "Found no playlists.");
            return;
        }

        // Log a list of the playlists.
        Log.i(LOGGING_TAG, "Playlists:");
        String playListName = null;
        for (boolean hasItem = playLists.moveToFirst(); hasItem; hasItem = playLists.moveToNext()) {
            playListName = playLists.getString(playLists.getColumnIndex(nameKey));
            Log.i(LOGGING_TAG, playListName);
        }

        // Play the first song from the first playlist.
        playLists.moveToFirst();
        final long playlistID = playLists.getLong(playLists.getColumnIndex(idKey));
        this.playTrackFromPlaylist(playlistID);

        // Close the cursor.
        if (playLists != null) {
            playLists.close();
        }
    }

    // PandyaPlaylistsActivity instance methods

    /**
     * Play the first track on the specified playlist.
     * 
     * @param playListID
     *            from the MediaStore database
     */
    public void playTrackFromPlaylist(final long playListID) {
            final ContentResolver resolver = this.getContentResolver();
        final Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", playListID);
        final String dataKey = MediaStore.Audio.Media.DATA;
        Cursor tracks = resolver.query(uri, new String[] { dataKey }, null, null, null);
        if (tracks != null) {
            tracks.moveToFirst();
            final int dataIndex = tracks.getColumnIndex(dataKey);
            final String dataPath = tracks.getString(dataIndex);
            PandyaPlaylistsActivity.playAudio(dataPath);
            tracks.close();
        }
    }
}

This activity combines Ankur Pandya's two answers to list the playlists on the device and then play the first track from the first playlist.

package com.withoutstones.pandyaplaylists;

import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;

/**
 * @author Ankur Pandya
 * @author Tommy Herbert
 */
public class PandyaPlaylistsActivity extends Activity {

    // constants

    private static final String LOGGING_TAG = "PandyaPlaylists";

    // static methods

    /**
     * Create a MediaPlayer and play the specified audio file. Note that a full app would usually
     * call stop() and release() on the player after use.
     * 
     * @param path
     *            to data file
     */
    public static void playAudio(final String path) {
        final MediaPlayer player = new MediaPlayer();
        if (path == null) {
            Log.e(LOGGING_TAG, "Called playAudio with null data stream.");
            return;
        }
        try {
            player.setDataSource(path);
            player.prepare();
            player.start();
        } catch (Exception e) {
            Log.e(LOGGING_TAG, "Failed to start MediaPlayer: " + e.getMessage());
            return;
        }
    }

    // superclass overrides

    @Override
    public void onCreate(final Bundle savedInstanceState) {

        // Create the superclass portion of the object.
        super.onCreate(savedInstanceState);

        // Set up the UI.
        this.setContentView(R.layout.main);

        // Get a cursor over all playlists.
            final ContentResolver resolver = this.getContentResolver();
        final Uri uri = MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI;
        final String idKey = MediaStore.Audio.Playlists._ID;
        final String nameKey = MediaStore.Audio.Playlists.NAME;
        final String[] columns = { idKey, nameKey };
        final Cursor playLists = resolver.query(uri, columns, null, null, null);
        if (playLists == null) {
            Log.e(LOGGING_TAG, "Found no playlists.");
            return;
        }

        // Log a list of the playlists.
        Log.i(LOGGING_TAG, "Playlists:");
        String playListName = null;
        for (boolean hasItem = playLists.moveToFirst(); hasItem; hasItem = playLists.moveToNext()) {
            playListName = playLists.getString(playLists.getColumnIndex(nameKey));
            Log.i(LOGGING_TAG, playListName);
        }

        // Play the first song from the first playlist.
        playLists.moveToFirst();
        final long playlistID = playLists.getLong(playLists.getColumnIndex(idKey));
        this.playTrackFromPlaylist(playlistID);

        // Close the cursor.
        if (playLists != null) {
            playLists.close();
        }
    }

    // PandyaPlaylistsActivity instance methods

    /**
     * Play the first track on the specified playlist.
     * 
     * @param playListID
     *            from the MediaStore database
     */
    public void playTrackFromPlaylist(final long playListID) {
            final ContentResolver resolver = this.getContentResolver();
        final Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", playListID);
        final String dataKey = MediaStore.Audio.Media.DATA;
        Cursor tracks = resolver.query(uri, new String[] { dataKey }, null, null, null);
        if (tracks != null) {
            tracks.moveToFirst();
            final int dataIndex = tracks.getColumnIndex(dataKey);
            final String dataPath = tracks.getString(dataIndex);
            PandyaPlaylistsActivity.playAudio(dataPath);
            tracks.close();
        }
    }
}
夏花。依旧 2024-12-10 15:15:26

为了显示用户在 Android 中的默认音乐应用程序中创建的所有播放列表,我在我的活动中使用以下代码块:

@Override
public void onCreate(Bundle savedInstanceState) {        
    super.onCreate(savedInstanceState);         
    String[] proj = {"*"};
    Uri tempPlaylistURI = MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI;

    // In the next line 'this' points to current Activity.
    // If you want to use the same code in other java file then activity,
    // then use an instance of any activity in place of 'this'.

    Cursor playListCursor= this.managedQuery(tempPlaylistURI, proj, null,null,null);

    if(playListCursor == null){
        System.out.println("Not having any Playlist on phone --------------");
        return;//don't have list on phone
    }

    System.gc();

    String playListName = null;

    System.out.println(">>>>>>>  CREATING AND DISPLAYING LIST OF ALL CREATED PLAYLIST  <<<<<<");

    for(int i = 0; i <playListCursor.getCount() ; i++)
    {
        playListCursor.moveToPosition(i);
        playListName = playListCursor.getString(playListCursor.getColumnIndex("name"));
        System.out.println("> " + i + "  : " + playListName );
    }

    if(playListCursor != null)
        playListCursor.close();

}

在使用之前不要忘记包含这些代码

import android.net.Uri;
import android.provider.MediaStore;
import android.database.Cursor;

此代码经过测试并且在 target = android- 上运行良好8

To display all the playlist created by user in default music application in Android, I am using following block of code in my Activity:

@Override
public void onCreate(Bundle savedInstanceState) {        
    super.onCreate(savedInstanceState);         
    String[] proj = {"*"};
    Uri tempPlaylistURI = MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI;

    // In the next line 'this' points to current Activity.
    // If you want to use the same code in other java file then activity,
    // then use an instance of any activity in place of 'this'.

    Cursor playListCursor= this.managedQuery(tempPlaylistURI, proj, null,null,null);

    if(playListCursor == null){
        System.out.println("Not having any Playlist on phone --------------");
        return;//don't have list on phone
    }

    System.gc();

    String playListName = null;

    System.out.println(">>>>>>>  CREATING AND DISPLAYING LIST OF ALL CREATED PLAYLIST  <<<<<<");

    for(int i = 0; i <playListCursor.getCount() ; i++)
    {
        playListCursor.moveToPosition(i);
        playListName = playListCursor.getString(playListCursor.getColumnIndex("name"));
        System.out.println("> " + i + "  : " + playListName );
    }

    if(playListCursor != null)
        playListCursor.close();

}

Don't forget to include these before using

import android.net.Uri;
import android.provider.MediaStore;
import android.database.Cursor;

This code is Tested and working fine over target = android-8

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文