Skip to content

Most visited

Recently visited

navigation
MediaBrowserService / src / com.example.android.mediabrowserservice /

MusicPlayerActivity.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
package com.example.android.mediabrowserservice;
17
 
18
import android.app.Activity;
19
import android.media.browse.MediaBrowser;
20
import android.os.Bundle;
21
 
22
/**
23
 * Main activity for the music player.
24
 */
25
public class MusicPlayerActivity extends Activity
26
        implements BrowseFragment.FragmentDataHelper {
27
 
28
    @Override
29
    public void onCreate(Bundle savedInstanceState) {
30
        super.onCreate(savedInstanceState);
31
        setContentView(R.layout.activity_player);
32
        if (savedInstanceState == null) {
33
            getFragmentManager().beginTransaction()
34
                    .add(R.id.container, BrowseFragment.newInstance(null))
35
                    .commit();
36
        }
37
    }
38
 
39
    @Override
40
    public void onMediaItemSelected(MediaBrowser.MediaItem item) {
41
        if (item.isPlayable()) {
42
            getMediaController().getTransportControls().playFromMediaId(item.getMediaId(), null);
43
            QueueFragment queueFragment = QueueFragment.newInstance();
44
            getFragmentManager().beginTransaction()
45
                    .replace(R.id.container, queueFragment)
46
                    .addToBackStack(null)
47
                    .commit();
48
        } else if (item.isBrowsable()) {
49
            getFragmentManager().beginTransaction()
50
                    .replace(R.id.container, BrowseFragment.newInstance(item.getMediaId()))
51
                    .addToBackStack(null)
52
                    .commit();
53
        }
54
    }
55
}
This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Follow Google Developers on WeChat

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience.
(Sep 2017 survey)