android developer tiny share-20160825

原创 2016年08月29日 19:04:08

今天讲使用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);
        }
    }
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

android developer tiny share-20170601

今天继续讲android的日历事件ContentProvider,讲添加事件。 以下是android developer官网的讲解: 添加事件 当您的应用插入新事件时,我们建议您按照...

android developer tiny share-20160811

今天讲在AndroidManifest.xml中设置了后,以及该节点下设置的、、等子节点,一个隐式的intent是否可以通过该intent-filter。这里的规则比较复杂,具体看今天讲的。 Int...

android developer tiny share-20160722

今天分享自定义permission,在AndroidManifest.xml中,你可以定义一个标签,来自定义一个permission。具体如下: Defining and Enforcing Per...

android developer tiny share-20170505

今天继续讲android的ContentProvider,讲设计自己的ContentProvider,需要继承ContentProvider类,实现6个方法,分别是query()、insert()、u...

android developer tiny share-20170222

今天讲android AIDL的IPC传递对象,可以通过AIDL来实现跨进程传递对象。另外,会讲Parcelable接口,如果要实现在IPC传递对象,对象必须实现Parcelable接口。 以下是a...

android developer tiny share-20161008

今天讲android的任务和返回栈。 任务和返回栈 应用通常包含多个Activity。每个 Activity 均应围绕用户可以执行的特定操作设计,并且能够启动其他 Activity。 例如...

android developer tiny share-20160920

今天继续讲fragment,讲管理fragment和执行fragment事务。 管理片段 要想管理您的 Activity 中的片段,您需要使用 FragmentManager。要想获取它,...

android developer tiny share-20161101

今天讲IntentService,它是android实现的Service + Thread的一个轻量级框架,你可以使用它实现创建一个队列式的多个线程任务,在Service中运行,运行完成后,Servi...

android developer tiny share-20161010

今天继续讲Task,涉及“保存Activity状态”和“管理任务”两个小话题。 保存 Activity 状态 正如上文所述,当 Activity 停止时,系统的默认行为会保留其状态。 这样...

android developer tiny share-20160711

今天讲一下AndroidManifest.xml中的下面这行代码的用法: ... uses-feature,这也是android compatibility话题里涉...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:android developer tiny share-20160825
举报原因:
原因补充:

(最多只允许输入30个字)