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);
        }
    }
}


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

ios developer tiny share-20160825

今天讲Objective-C的运行时动态特性。 Objective-C Is a Dynamic Language As mentioned earlier, you need to use ...
  • zheng3stone
  • zheng3stone
  • 2016年09月01日 10:32
  • 97

android developer tiny share-20170204

好久没有写博客了,距离上次写博客已经过去快3个月了,好的习惯要继续保持,从今天起,每天继续坚持写tiny share,加油! 今天讲android Service组件的生命周期,包括star...
  • zheng3stone
  • zheng3stone
  • 2017年02月04日 17:36
  • 61

android developer tiny share-20160720

今天分享下android把permission分为两类,即Normal Permissions和Dangerous Permissions。 具体什么Normal Permissions,什么是Da...
  • zheng3stone
  • zheng3stone
  • 2016年07月20日 20:15
  • 113

android developer tiny share-20170216

今天讲AIDL接口的定义步骤,一共三步,比较简单。另外,会讲下AIDL接口的兼容问题。 下面是android developer官方的说明: 定义AIDL接口 您必须使用 Java 编程...
  • zheng3stone
  • zheng3stone
  • 2017年02月23日 16:18
  • 123

android developer tiny share-20170224

今天开始,我们讲android四大组件的最后一个组件——Content Provider,即内容提供程序。今天先大概Overview下。 以下是android developer的讲解: ...
  • zheng3stone
  • zheng3stone
  • 2017年03月03日 16:30
  • 64

android developer tiny share-20161008

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

android developer tiny share-201704018

今天继续讲android的设计自己的ContentProvider,讲如何设计URI,包括授权、路径结构、ID。 以下是android developer官方的讲解: 设计内容 URI ...
  • zheng3stone
  • zheng3stone
  • 2017年04月18日 16:37
  • 113

android developer tiny share-20170209

今天讲android绑定服务的第二种方法,使用Messenger。Message方式也能实现进程间通信(IPC),与AIDL方式相比,Messenger方式更加简单,但是Messenger方式不支持服...
  • zheng3stone
  • zheng3stone
  • 2017年02月15日 14:48
  • 61

android developer tiny share-20161108

今天继续讲android的Service,讲前台服务。前台服务优先级很高,即便内存不足时,也不会考虑回收它。但是,前台服务必须在状态栏中提示用户。这种前台服务主要用于像播放器等功能。 下面是...
  • zheng3stone
  • zheng3stone
  • 2016年11月22日 15:23
  • 108

android developer tiny share-20160721

今天分享android权限组(Permission groups)的概念,以及在android 6.0运行时提示访问权限的机制。 All dangerous Android system permi...
  • zheng3stone
  • zheng3stone
  • 2016年07月22日 10:43
  • 85
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:android developer tiny share-20160825
举报原因:
原因补充:

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