android developer tiny share-20160825

今天讲使用intent实现通过搜索来播放音乐,比较复杂,简单了解下。

Play music based on a search query

 
To play music based on a search query, use the INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH intent. An app may fire this intent in response to the user's voice command to play music. The receiving app for this intent performs a search within its inventory to match existing content to the given query and starts playing that content.

This intent should include the EXTRA_MEDIA_FOCUS string extra, which specifies the inteded search mode. For example, the search mode can specify whether the search is for an artist name or song name.

Action
    INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH
Data URI Scheme
    None
MIME Type
    None
Extras
    MediaStore.EXTRA_MEDIA_FOCUS (required)
        Indicates the search mode (whether the user is looking for a particular artist, album, song, or playlist). Most search modes take additional extras. For example, if the user is interested in listening to a particular song, the intent might have three additional extras: the song title, the artist, and the album. This intent supports the following search modes for each value of EXTRA_MEDIA_FOCUS:

        Any - "vnd.android.cursor.item/*"

            Play any music. The receiving app should play some music based on a smart choice, such as the last playlist the user listened to.

            Additional extras:

            QUERY (required) - An empty string. This extra is always provided for backward compatibility: existing apps that do not know about search modes can process this intent as an unstructured search.
Unstructured - "vnd.android.cursor.item/*"

            Play a particular song, album or genre from an unstructured search query. Apps may generate an intent with this search mode when they can't identify the type of content the user wants to listen to. Apps should use more specific search modes when possible.

            Additional extras:

            QUERY (required) - A string that contains any combination of: the artist, the album, the song name, or the genre.
Genre - Audio.Genres.ENTRY_CONTENT_TYPE

            Play music of a particular genre.

            Additional extras:

            "android.intent.extra.genre" (required) - The genre.


            QUERY (required) - The genre. This extra is always provided for backward compatibility: existing apps that do not know about search modes can process this intent as an unstructured search.


            Artist - Audio.Artists.ENTRY_CONTENT_TYPE

            Play music from a particular artist.

            Additional extras:

            EXTRA_MEDIA_ARTIST (required) - The artist.


            "android.intent.extra.genre" - The genre.


            QUERY (required) - A string that contains any combination of the artist or the genre. This extra is always provided for backward compatibility: existing apps that do not know about search modes can process this intent as an unstructured search.


            Album - Audio.Albums.ENTRY_CONTENT_TYPE

            Play music from a particular album.

            Additional extras:

            EXTRA_MEDIA_ALBUM (required) - The album.
            EXTRA_MEDIA_ARTIST - The artist.
            "android.intent.extra.genre" - The genre.
            QUERY (required) - A string that contains any combination of the album or the artist. This extra is always provided for backward compatibility: existing apps that do not know about search modes can process this intent as an unstructured search.
Song - "vnd.android.cursor.item/audio"


            Play a particular song.

            Additional extras:

            EXTRA_MEDIA_ALBUM - The album.
            EXTRA_MEDIA_ARTIST - The artist.
            "android.intent.extra.genre" - The genre.
            EXTRA_MEDIA_TITLE (required) - The song name.
            QUERY (required) - A string that contains any combination of: the album, the artist, the genre, or the title. This extra is always provided for backward compatibility: existing apps that do not know about search modes can process this intent as an unstructured search.
Playlist - Audio.Playlists.ENTRY_CONTENT_TYPE


            Play a particular playlist or a playlist that matches some criteria specified by additional extras.

            Additional extras:

            EXTRA_MEDIA_ALBUM - The album.
            EXTRA_MEDIA_ARTIST - The artist.
            "android.intent.extra.genre" - The genre.
            "android.intent.extra.playlist" - The playlist.
            EXTRA_MEDIA_TITLE - The song name that the playlist is based on.
            QUERY (required) - A string that contains any combination of: the album, the artist, the genre, the playlist, or the title. This extra is always provided for backward compatibility: existing apps that do not know about search modes can process this intent as an unstructured search.
Example intent:


If the user wants to listen to music from a particular artist, a search app may generate the following intent:

public void playSearchArtist(String artist) {
    Intent intent = new Intent(MediaStore.INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH);
    intent.putExtra(MediaStore.EXTRA_MEDIA_FOCUS,
                    MediaStore.Audio.Artists.ENTRY_CONTENT_TYPE);
    intent.putExtra(MediaStore.EXTRA_MEDIA_ARTIST, artist);
    intent.putExtra(SearchManager.QUERY, artist);
    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivity(intent);
    }
}

Example intent filter:

<activity ...>
    <intent-filter>
        <action android:name="android.media.action.MEDIA_PLAY_FROM_SEARCH" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

When handling this intent, your activity should check the value of the EXTRA_MEDIA_FOCUS extra in the incoming Intent to determine the search mode. Once your activity has identified the search mode, it should read the values of the additional extras for that particular search mode. With this information your app can then perform the search within its inventory to play the content that matches the search query. For example:

protected void onCreate(Bundle savedInstanceState) {
    ...
    Intent intent = this.getIntent();
    if (intent.getAction().compareTo(MediaStore.INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH) == 0) {

        String mediaFocus = intent.getStringExtra(MediaStore.EXTRA_MEDIA_FOCUS);
        String query = intent.getStringExtra(SearchManager.QUERY);

        // Some of these extras may not be available depending on the search mode
        String album = intent.getStringExtra(MediaStore.EXTRA_MEDIA_ALBUM);
        String artist = intent.getStringExtra(MediaStore.EXTRA_MEDIA_ARTIST);
        String genre = intent.getStringExtra("android.intent.extra.genre");
        String playlist = intent.getStringExtra("android.intent.extra.playlist");
        String title = intent.getStringExtra(MediaStore.EXTRA_MEDIA_TITLE);

        // Determine the search mode and use the corresponding extras
        if (mediaFocus == null) {
            // 'Unstructured' search mode (backward compatible)
            playUnstructuredSearch(query);

        } else if (mediaFocus.compareTo("vnd.android.cursor.item/*") == 0) {
            if (query.isEmpty()) {
                // 'Any' search mode
                playResumeLastPlaylist();
            } else {
                // 'Unstructured' search mode
                playUnstructuredSearch(query);
            }

        } else if (mediaFocus.compareTo(MediaStore.Audio.Genres.ENTRY_CONTENT_TYPE) == 0) {
            // 'Genre' search mode
            playGenre(genre);

        } else if (mediaFocus.compareTo(MediaStore.Audio.Artists.ENTRY_CONTENT_TYPE) == 0) {
            // 'Artist' search mode
            playArtist(artist, genre);

        } else if (mediaFocus.compareTo(MediaStore.Audio.Albums.ENTRY_CONTENT_TYPE) == 0) {
            // 'Album' search mode
            playAlbum(album, artist);

        } else if (mediaFocus.compareTo("vnd.android.cursor.item/audio") == 0) {
            // 'Song' search mode
            playSong(album, artist, genre, title);

        } else if (mediaFocus.compareTo(MediaStore.Audio.Playlists.ENTRY_CONTENT_TYPE) == 0) {
            // 'Playlist' search mode
            playPlaylist(album, artist, genre, playlist, title);
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值