简体   繁体   中英

Continue playing music when screen orientation is changed in Android

I have a problem where I can't keep the music playing when chaning the screen orientation in my program. I tried adding android:configChanges="keyboardHidden|orientation|screenSize inside the MainActivity in the AndroidManifest.xml file, but although it keeps the music playing, it also disables the different layout for the landscape mode.

Here is the code for the MainActivity:

package com.example.gomoku;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.MenuInflater;
import android.view.MenuItem;

public class MainActivity extends Activity implements OnClickListener{

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

        // Set up click listeners for all the buttons.
        View newButton = findViewById(R.id.new_button);
        newButton.setOnClickListener(this);
        View highScoreButton = findViewById(R.id.high_score_button);
        highScoreButton.setOnClickListener(this);
        View aboutButton = findViewById(R.id.about_button);
        aboutButton.setOnClickListener(this);
        View exitButton = findViewById(R.id.exit_button);
        exitButton.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }

    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.new_button:
            startGame();
            break;
        case R.id.about_button:
            Intent i = new Intent(this, About.class);
            startActivity(i);
            break;
        case R.id.exit_button:
            finish();
            break;
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.settings:
            startActivity(new Intent(this, Prefs.class));
            return true;
        }
        return false;
    }

    @Override
    protected void onResume() {
        super.onResume();
        Music.play(this, R.raw.main);
    }

    @Override
    protected void onPause() {
        super.onPause();
        Music.stop(this);
    }

    private void startGame() {
        Intent intent = new Intent(MainActivity.this, Game.class);
        startActivity(intent);
    }
}

And here is the code for Music:

package com.example.gomoku;

import android.content.Context;
import android.media.MediaPlayer;

public class Music {
    private static MediaPlayer mp = null;

    /** Stop old song and start new one. */
    public static void play(Context context, int resource) {
        stop(context);
        // Start music only if not disabled in preferences.
        if (Prefs.getMusic(context)) {
            mp = MediaPlayer.create(context, resource);
            mp.setLooping(true);
            mp.start();
        }
    }

    /** Stop the music. */
    public static void stop(Context context) {
        if (mp != null) {
            mp.stop();
            mp.release();
            mp = null;
        }
    }
}

Editing the original code;

package com.example.gomoku;

import android.content.Context;
import android.media.MediaPlayer;

public class Music {
    //####First change is here####
    //adding a variable to store the time of music being played.
    private static int pos = 0;
    private static MediaPlayer mp = null;

    /** Stop old song and start new one. */
    public static void play(Context context, int resource) {
        stop(context);
        // Start music only if not disabled in preferences.
        if (Prefs.getMusic(context)) {
            mp = MediaPlayer.create(context, resource);
            mp.setLooping(true);
            mp.start();
            //####Second change is here####
            //this will continue the music from wherever it was paused 
            mp.seekTo(pos);
        }
    }

    /** Stop the music. */
    public static void stop(Context context) {

        if (mp != null) {
            //####Third change is here####
            mp.pause();//to pause the music
            //to store current pause time in pos
            //as pos is static it will retain the value
            pos = mp.getCurrentPosition();
            mp.stop();
            mp.release();
            mp = null;
        }
    }
}

add this code to stop the app when back key is pressed.

@Override
public void onBackPressed() {
    pos = 0;
    super.onBackPressed();
    mp.release();
    System.exit(0);
}

Now this app will:

  1. Play music in loop when it starts.
  2. Continue the music when screen orientation is changed.
  3. Resume music when app is restarted after pressing home button.
  4. Restart song if the back button was pressed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM