Spotify播放器未在模拟器中显示

时间:2016-06-22 23:43:03

标签: android controls media spotify

我正在使用spotify api,我准备播放一首歌。当我打开我的应用程序时,歌曲播放但我看不到spotify播放器(即媒体控制)。对此有何见解?我是android studio的新手。这是我的一些代码。它将用户作为高级用户进行身份验证并播放曲目。

public class MainActivity extends Activity implements
        PlayerNotificationCallback, ConnectionStateCallback {

    //redirect uri and other stuff

    private Player mPlayer;

    private static final int REQUEST_CODE = 1337;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        AuthenticationRequest.Builder builder = new AuthenticationRequest.Builder(CLIENT_ID,
                AuthenticationResponse.Type.TOKEN,
                REDIRECT_URI);
        builder.setScopes(new String[]{"user-read-private", "streaming"});
        AuthenticationRequest request = builder.build();

        AuthenticationClient.openLoginActivity(this, REQUEST_CODE, request);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);

        // Check if result comes from the correct activity
        if (requestCode == REQUEST_CODE) {
            AuthenticationResponse response = AuthenticationClient.getResponse(resultCode, intent);
            if (response.getType() == AuthenticationResponse.Type.TOKEN) {
                Config playerConfig = new Config(this, response.getAccessToken(), CLIENT_ID);
                Spotify.getPlayer(playerConfig, this, new Player.InitializationObserver() {
                    @Override
                    public void onInitialized(Player player) {
                        mPlayer = player;
                        mPlayer.addConnectionStateCallback(MainActivity.this);
                        mPlayer.addPlayerNotificationCallback(MainActivity.this);
                        mPlayer.play("spotify:track:3G69vJMWsX6ZohTykad2AU
    ");
                    }

                    @Override
                    public void onError(Throwable throwable) {
                        Log.e("MainActivity", "Could not initialize player: " + throwable.getMessage());
                    }
                });
            }
        }
    }
    @Override
    public void onLoggedIn() {
        Log.d("MainActivity", "User logged in");
    }

    @Override
    public void onLoggedOut() {
        Log.d("MainActivity", "User logged out");
    }

    @Override
    public void onLoginFailed(Throwable error) {
        Log.d("MainActivity", "Login failed");
    }

    @Override
    public void onTemporaryError() {
        Log.d("MainActivity", "Temporary error occurred");
    }

    @Override
    public void onConnectionMessage(String message) {
        Log.d("MainActivity", "Received connection message: " + message);
    }

    @Override
    public void onPlaybackEvent(EventType eventType, PlayerState playerState) {
        Log.d("MainActivity", "Playback event received: " + eventType.name());
    }

    @Override
    public void onPlaybackError(ErrorType errorType, String errorDetails) {
        Log.d("MainActivity", "Playback error received: " + errorType.name());
    }

    @Override
    protected void onDestroy() {
        Spotify.destroyPlayer(this);
        super.onDestroy();
    }
}

编辑:我没有包括任何UI的实现。我通过编写以下xml进一步解决了这个问题。

<EditText
        android:id="@+id/songName"
        android:layout_width="358dp"
        android:layout_height="61dp"
        android:inputType="text"
        android:hint="Song"
        android:ems="10"
        android:gravity="center_horizontal"
        android:textAppearance="@style/TextAppearance.AppCompat.Display1"
        android:layout_marginTop="8dp"
        app:layout_constraintTop_toBottomOf="@+id/appBarLayout3"
        android:layout_marginStart="16dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginLeft="16dp" />

    <EditText
        android:id="@+id/artistName"
        android:layout_width="358dp"
        android:layout_height="64dp"
        android:inputType="text"
        android:ems="10"
        android:gravity="center_horizontal"
        android:hint="Artist"
        android:textAppearance="@style/TextAppearance.AppCompat.Display1"
        app:layout_constraintTop_toBottomOf="@+id/songName"
        android:layout_marginStart="16dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginLeft="16dp" />

    <fragment
        android:id="@+id/fragment"
        android:name="com.example.joeberg.jamstest.fragments.LyricsFragment"
        android:layout_width="358dp"
        android:layout_height="400dp"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="56dp"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/artistName"
        tools:layout="@layout/fragment_lyrics" />

    <Button
        android:id="@+id/playButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginStart="104dp"
        android:layout_marginTop="0dp"
        android:onClick="playSongButton"
        android:text="Play/Pause"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/artistName" />

    <Button
        android:text="Pause"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/pauseButton"
        android:onClick="pauseSongButton"
        app:layout_constraintLeft_toRightOf="@+id/playButton"
        app:layout_constraintTop_toBottomOf="@+id/artistName"
        android:layout_marginTop="0dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp" />

    <Button
        android:id="@+id/pauseButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="0dp"
        android:onClick="pauseSongButton"
        android:text="Reset"
        app:layout_constraintLeft_toRightOf="@+id/playButton"
        app:layout_constraintTop_toBottomOf="@+id/artistName" />

这使我能够通过播放和暂停按钮控制歌曲播放。它还允许我搜索歌曲。

1 个答案:

答案 0 :(得分:1)

当您使用Spotify的API播放音乐时,您只能播放音乐。您没有包含任何UI。你必须自己构建它。