繁体   English   中英

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

[英]Continue playing music when screen orientation is changed in 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;
        }
    }
}

编辑原始代码;

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. 如果按下后退按钮,则重新开始播放歌曲。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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