این وبسایت یک نسخه آزمایشی از سایت developer android می‌باشد که در حال ترجمه می‌باشد.
برای پیوستن به تیم ترجمه و خواندن مستندات اینجا کلیک کنید.
MediaBrowserService / src / com.example.android.mediabrowserservice / utils /

MediaIDHelper.java

1
/*
2
 * Copyright (C) 2014 The Android Open Source Project
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
 
17
package com.example.android.mediabrowserservice.utils;
18
 
19
import android.media.MediaMetadata;
20
 
21
/**
22
 * Utility class to help on queue related tasks.
23
 */
24
public class MediaIDHelper {
25
 
26
    private static final String TAG = "MediaIDHelper";
27
 
28
    // Media IDs used on browseable items of MediaBrowser
29
    public static final String MEDIA_ID_ROOT = "__ROOT__";
30
    public static final String MEDIA_ID_MUSICS_BY_GENRE = "__BY_GENRE__";
31
 
32
    public static final String createTrackMediaID(String categoryType, String categoryValue,
33
              MediaMetadata track) {
34
        // MediaIDs are of the form <categoryType>/<categoryValue>|<musicUniqueId>, to make it easy to
35
        // find the category (like genre) that a music was selected from, so we
36
        // can correctly build the playing queue. This is specially useful when
37
        // one music can appear in more than one list, like "by genre -> genre_1"
38
        // and "by artist -> artist_1".
39
        return categoryType + "/" + categoryValue + "|" +
40
                track.getString(MediaMetadata.METADATA_KEY_MEDIA_ID);
41
    }
42
 
43
    public static final String createBrowseCategoryMediaID(String categoryType, String categoryValue) {
44
        return categoryType + "/" + categoryValue;
45
    }
46
 
47
    /**
48
     * Extracts unique musicID from the mediaID. mediaID is, by this sample's convention, a
49
     * concatenation of category (eg "by_genre"), categoryValue (eg "Classical") and unique
50
     * musicID. This is necessary so we know where the user selected the music from, when the music
51
     * exists in more than one music list, and thus we are able to correctly build the playing queue.
52
     *
53
     * @param musicID
54
     * @return
55
     */
56
    public static final String extractMusicIDFromMediaID(String musicID) {
57
        String[] segments = musicID.split("\\|", 2);
58
        return segments.length == 2 ? segments[1] : null;
59
    }
60
 
61
    /**
62
     * Extracts category and categoryValue from the mediaID. mediaID is, by this sample's
63
     * convention, a concatenation of category (eg "by_genre"), categoryValue (eg "Classical") and
64
     * mediaID. This is necessary so we know where the user selected the music from, when the music
65
     * exists in more than one music list, and thus we are able to correctly build the playing queue.
66
     *
67
     * @param mediaID
68
     * @return
69
     */
70
    public static final String[] extractBrowseCategoryFromMediaID(String mediaID) {
71
        if (mediaID.indexOf('|') >= 0) {
72
            mediaID = mediaID.split("\\|")[0];
73
        }
74
        if (mediaID.indexOf('/') == 0) {
75
            return new String[]{mediaID, null};
76
        } else {
77
            return mediaID.split("/", 2);
78
        }
79
    }
80
 
81
    public static final String extractBrowseCategoryValueFromMediaID(String mediaID) {
82
        String[] categoryAndValue = extractB