在Android中更改屏幕方向时继续播放音乐

时间:2013-11-30 12:42:43

标签: android

我遇到一个问题,在我的程序中查看屏幕方向时,我无法播放音乐。我尝试在AndroidManifest.xml文件的MainActivity中添加android:configChanges="keyboardHidden|orientation|screenSize,但是虽然它可以保持音乐播放,但它也会禁用横向模式的不同布局。

以下是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);
    }
}

以下是音乐代码:

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

1 个答案:

答案 0 :(得分:1)

编辑原始代码;

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

添加此代码以在按下后退键时停止应用。

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

现在这个应用程序将:

  1. 启动时循环播放音乐。
  2. 更改屏幕方向时继续播放音乐。
  3. 按主页按钮后重新启动应用程序时恢复音乐。
  4. 如果按下后退按钮,则重新启动歌曲。