按下后退或主页按钮时如何停止媒体播放器

时间:2013-06-04 14:59:12

标签: android android-mediaplayer android-lifecycle

我的问题是当用户按下后面或主页按钮时如何停止媒体播放器?以下是代码。有人可以给一个代码,并告诉我在活动中将其纳入何处。帮助将不胜感激。

package com.example.dictationary;

import java.util.LinkedList;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Level1 extends Activity {

    final LinkedList<Word> wordList = new LinkedList<Word>();

    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.level1);

        MediaPlayer n1  = MediaPlayer.create(this,R.raw.assassin); 
        MediaPlayer n2  = MediaPlayer.create(this,R.raw.accessible);
        MediaPlayer n3  = MediaPlayer.create(this,R.raw.bashfully);
        MediaPlayer n4  = MediaPlayer.create(this,R.raw.blistering);
        MediaPlayer n5  = MediaPlayer.create(this,R.raw.butter);
        MediaPlayer n6  = MediaPlayer.create(this,R.raw.campaign);
        MediaPlayer n7  = MediaPlayer.create(this,R.raw.circumstances);
        MediaPlayer n8  = MediaPlayer.create(this,R.raw.clinching);
        MediaPlayer n9  = MediaPlayer.create(this,R.raw.deferential);
        MediaPlayer n10 = MediaPlayer.create(this,R.raw.distinguished);
        MediaPlayer n11 = MediaPlayer.create(this,R.raw.embarrass);
        MediaPlayer n12 = MediaPlayer.create(this,R.raw.etiquette);
        MediaPlayer n13 = MediaPlayer.create(this,R.raw.fatigue);
        MediaPlayer n14 = MediaPlayer.create(this,R.raw.feasible);
        MediaPlayer n15 = MediaPlayer.create(this,R.raw.glitch);
        MediaPlayer n16 = MediaPlayer.create(this,R.raw.graffiti);
        MediaPlayer n17 = MediaPlayer.create(this,R.raw.hippopotamus);
        MediaPlayer n18 = MediaPlayer.create(this,R.raw.hypocrisy);
        MediaPlayer n19 = MediaPlayer.create(this,R.raw.illegible);
        MediaPlayer n20 = MediaPlayer.create(this,R.raw.illiteracy);
        MediaPlayer n21 = MediaPlayer.create(this,R.raw.jaundice);
        MediaPlayer n22 = MediaPlayer.create(this,R.raw.kidnapped);
        MediaPlayer n23 = MediaPlayer.create(this,R.raw.kinetic);
        MediaPlayer n24 = MediaPlayer.create(this,R.raw.laboratory);
        MediaPlayer n25 = MediaPlayer.create(this,R.raw.lacrimation);
        MediaPlayer n26 = MediaPlayer.create(this,R.raw.macaroni);
        MediaPlayer n27 = MediaPlayer.create(this,R.raw.maintenance);
        MediaPlayer n28 = MediaPlayer.create(this,R.raw.mediocre);
        MediaPlayer n29 = MediaPlayer.create(this,R.raw.metaphor);
        MediaPlayer n30 = MediaPlayer.create(this,R.raw.nocturnal);
        MediaPlayer n31 = MediaPlayer.create(this,R.raw.nutritious);
        MediaPlayer n32 = MediaPlayer.create(this,R.raw.obligatory);
        MediaPlayer n33 = MediaPlayer.create(this,R.raw.oblique);
        MediaPlayer n34 = MediaPlayer.create(this,R.raw.opaque);
        MediaPlayer n35 = MediaPlayer.create(this,R.raw.pageant);
        MediaPlayer n36 = MediaPlayer.create(this,R.raw.personnel);
        MediaPlayer n37 = MediaPlayer.create(this,R.raw.quartz);
        MediaPlayer n38 = MediaPlayer.create(this,R.raw.quash);
        MediaPlayer n39 = MediaPlayer.create(this,R.raw.racquetball);
        MediaPlayer n40 = MediaPlayer.create(this,R.raw.recommend);
        MediaPlayer n41 = MediaPlayer.create(this,R.raw.rhyme);
        MediaPlayer n42 = MediaPlayer.create(this,R.raw.schedule);
        MediaPlayer n43 = MediaPlayer.create(this,R.raw.souvenir);
        MediaPlayer n44 = MediaPlayer.create(this,R.raw.strength);
        MediaPlayer n45 = MediaPlayer.create(this,R.raw.tombstone);
        MediaPlayer n46 = MediaPlayer.create(this,R.raw.tricycle);
        MediaPlayer n47 = MediaPlayer.create(this,R.raw.unanswerable);
        MediaPlayer n48 = MediaPlayer.create(this,R.raw.unconscious);
        MediaPlayer n49 = MediaPlayer.create(this,R.raw.vacuum);
        MediaPlayer n50 = MediaPlayer.create(this,R.raw.villain);
        MediaPlayer n51 = MediaPlayer.create(this,R.raw.weigh);
        MediaPlayer n52 = MediaPlayer.create(this,R.raw.weird);
        MediaPlayer n53 = MediaPlayer.create(this,R.raw.yacht);
        MediaPlayer n54 = MediaPlayer.create(this,R.raw.yolk);
        MediaPlayer n55 = MediaPlayer.create(this,R.raw.aberration);
        MediaPlayer n56 = MediaPlayer.create(this,R.raw.abject);
        MediaPlayer n57 = MediaPlayer.create(this,R.raw.abrogation);
        MediaPlayer n58 = MediaPlayer.create(this,R.raw.atrophy);
        MediaPlayer n59 = MediaPlayer.create(this,R.raw.blandishment);
        MediaPlayer n60 = MediaPlayer.create(this,R.raw.contentious);
        MediaPlayer n61 = MediaPlayer.create(this,R.raw.dogmatic);
        MediaPlayer n62 = MediaPlayer.create(this,R.raw.embezzlement);
        MediaPlayer n63 = MediaPlayer.create(this,R.raw.engender);
        MediaPlayer n64 = MediaPlayer.create(this,R.raw.fallacious);
        MediaPlayer n65 = MediaPlayer.create(this,R.raw.fatuous);
        MediaPlayer n66 = MediaPlayer.create(this,R.raw.flagrant);
        MediaPlayer n67 = MediaPlayer.create(this,R.raw.fortuitous);
        MediaPlayer n68 = MediaPlayer.create(this,R.raw.impetuous);
        MediaPlayer n69 = MediaPlayer.create(this,R.raw.inexorable);
        MediaPlayer n70 = MediaPlayer.create(this,R.raw.inveterate);
        MediaPlayer n71 = MediaPlayer.create(this,R.raw.irreverence);
        MediaPlayer n72 = MediaPlayer.create(this,R.raw.mendacious);
        MediaPlayer n73 = MediaPlayer.create(this,R.raw.multifarious);
        MediaPlayer n74 = MediaPlayer.create(this,R.raw.munificent);
        MediaPlayer n75 = MediaPlayer.create(this,R.raw.paradigm);
        MediaPlayer n76 = MediaPlayer.create(this,R.raw.plaudit);
        MediaPlayer n77 = MediaPlayer.create(this,R.raw.proclivity);
        MediaPlayer n78 = MediaPlayer.create(this,R.raw.promulgate);
        MediaPlayer n79 = MediaPlayer.create(this,R.raw.reprobate);
        MediaPlayer n80 = MediaPlayer.create(this,R.raw.surreptitious);
        MediaPlayer n81 = MediaPlayer.create(this,R.raw.tirade);
        MediaPlayer n82 = MediaPlayer.create(this,R.raw.umbrage);
        MediaPlayer n83 = MediaPlayer.create(this,R.raw.veracity);
        MediaPlayer n84 = MediaPlayer.create(this,R.raw.virtuoso);
        MediaPlayer n85 = MediaPlayer.create(this,R.raw.vitriolic);

        Word w1  = new Word(R.raw.assassin,"assassin");
        Word w2  = new Word(R.raw.accessible,"accessible");
        Word w3  = new Word(R.raw.bashfully,"bashfully");
        Word w4  = new Word(R.raw.blistering,"blistering");
        Word w5  = new Word(R.raw.butter,"butter");
        Word w6  = new Word(R.raw.campaign,"campaign");
        Word w7  = new Word(R.raw.circumstances,"circumstances");
        Word w8  = new Word(R.raw.clinching,"clinching");
        Word w9  = new Word(R.raw.deferential,"deferential");
        Word w10  = new Word(R.raw.distinguished,"distinguished");
        Word w11 = new Word(R.raw.embarrass,"embarrass");
        Word w12 = new Word(R.raw.etiquette,"etiquette");
        Word w13 = new Word(R.raw.fatigue,"fatigue");
        Word w14 = new Word(R.raw.feasible,"feasible");
        Word w15 = new Word(R.raw.glitch,"glitch");
        Word w16 = new Word(R.raw.graffiti,"graffiti");
        Word w17 = new Word(R.raw.hippopotamus,"hippopotamus");
        Word w18 = new Word(R.raw.hypocrisy,"hypocrisy");
        Word w19 = new Word(R.raw.illegible,"illegible");
        Word w20 = new Word(R.raw.illiteracy,"illiteracy");
        Word w21 = new Word(R.raw.jaundice,"jaundice");
        Word w22 = new Word(R.raw.kidnapped,"kidnapped");
        Word w23 = new Word(R.raw.kinetic,"kinetic");
        Word w24 = new Word(R.raw.laboratory,"laboratory");
        Word w25 = new Word(R.raw.lacrimation,"lacrimation");
        Word w26 = new Word(R.raw.macaroni,"macaroni");
        Word w27 = new Word(R.raw.maintenance,"maintenance");
        Word w28 = new Word(R.raw.mediocre,"mediocre");
        Word w29 = new Word(R.raw.metaphor,"metaphor");
        Word w30 = new Word(R.raw.nocturnal,"nocturnal");
        Word w31 = new Word(R.raw.nutritious,"nutritious");
        Word w32 = new Word(R.raw.obligatory,"obligatory");
        Word w33 = new Word(R.raw.oblique,"oblique");
        Word w34 = new Word(R.raw.opaque,"opaque");
        Word w35 = new Word(R.raw.pageant,"pageant");
        Word w36 = new Word(R.raw.personnel,"personnel");
        Word w37 = new Word(R.raw.quartz,"quartz");
        Word w38 = new Word(R.raw.quash,"quash");
        Word w39 = new Word(R.raw.racquetball,"racquetball");
        Word w40 = new Word(R.raw.recommend,"recommend");
        Word w41 = new Word(R.raw.rhyme,"rhyme");
        Word w42 = new Word(R.raw.schedule,"schedule");
        Word w43 = new Word(R.raw.souvenir,"souvenir");
        Word w44 = new Word(R.raw.strength,"strength");
        Word w45 = new Word(R.raw.tombstone,"tombstone");
        Word w46 = new Word(R.raw.tricycle,"tricycle");
        Word w47 = new Word(R.raw.unanswerable,"unanswerable");
        Word w48 = new Word(R.raw.unconscious,"unconscious");
        Word w49 = new Word(R.raw.vacuum,"vacuum");
        Word w50 = new Word(R.raw.villain,"villain");
        Word w51 = new Word(R.raw.weigh,"weigh");
        Word w52 = new Word(R.raw.weird,"weird");
        Word w53 = new Word(R.raw.yacht,"yacht");
        Word w54 = new Word(R.raw.yolk,"yolk");
        Word w55 = new Word(R.raw.aberration,"aberration");
        Word w56 = new Word(R.raw.abject,"abject");
        Word w57 = new Word(R.raw.abrogation,"abrogation");
        Word w58 = new Word(R.raw.atrophy,"atrophy");
        Word w59 = new Word(R.raw.blandishment,"blandishment");
        Word w60 = new Word(R.raw.contentious,"contentious");
        Word w61 = new Word(R.raw.dogmatic,"dogmatic");
        Word w62 = new Word(R.raw.embezzlement,"embezzlement");
        Word w63 = new Word(R.raw.engender,"engender");
        Word w64 = new Word(R.raw.fallacious,"fallacious");
        Word w65 = new Word(R.raw.fatuous,"fatuous");
        Word w66 = new Word(R.raw.flagrant,"flagrant");
        Word w67 = new Word(R.raw.fortuitous,"fortuitous");
        Word w68 = new Word(R.raw.impetuous,"impetuous");
        Word w69 = new Word(R.raw.inexorable,"inexorable");
        Word w70 = new Word(R.raw.inveterate,"inveterate");
        Word w71 = new Word(R.raw.irreverence,"irreverence");
        Word w72 = new Word(R.raw.mendacious,"mendacious");
        Word w73 = new Word(R.raw.multifarious,"multifarious");
        Word w74 = new Word(R.raw.munificent,"munificent");
        Word w75 = new Word(R.raw.paradigm,"paradigm");
        Word w76 = new Word(R.raw.plaudit,"plaudit");
        Word w77 = new Word(R.raw.proclivity,"proclivity");
        Word w78 = new Word(R.raw.promulgate,"promulgate");
        Word w79 = new Word(R.raw.reprobate,"reprobate");
        Word w80 = new Word(R.raw.surreptitious,"surreptitious");
        Word w81 = new Word(R.raw.tirade,"tirade");
        Word w82 = new Word(R.raw.umbrage,"umbrage");
        Word w83 = new Word(R.raw.veracity,"veracity");
        Word w84 = new Word(R.raw.virtuoso,"virtuoso");
        Word w85 = new Word(R.raw.vitriolic,"vitriolic");

        wordList.add(w1);
        wordList.add(w2);
        wordList.add(w3);
        wordList.add(w4);
        wordList.add(w5);
        wordList.add(w6);
        wordList.add(w7);
        wordList.add(w8);
        wordList.add(w9);
        wordList.add(w10);
        wordList.add(w11);
        wordList.add(w12);
        wordList.add(w13);
        wordList.add(w14);
        wordList.add(w15);
        wordList.add(w16);
        wordList.add(w17);
        wordList.add(w18);
        wordList.add(w19);
        wordList.add(w20);
        wordList.add(w21);
        wordList.add(w22);
        wordList.add(w23);
        wordList.add(w24);
        wordList.add(w25);
        wordList.add(w26);
        wordList.add(w27);
        wordList.add(w28);
        wordList.add(w29);
        wordList.add(w30);
        wordList.add(w31);
        wordList.add(w32);
        wordList.add(w33);
        wordList.add(w34);
        wordList.add(w35);
        wordList.add(w36);
        wordList.add(w37);
        wordList.add(w38);
        wordList.add(w39);
        wordList.add(w40);
        wordList.add(w41);
        wordList.add(w42);
        wordList.add(w43);
        wordList.add(w44);
        wordList.add(w45);
        wordList.add(w46);
        wordList.add(w47);
        wordList.add(w48);
        wordList.add(w49);
        wordList.add(w50);
        wordList.add(w51);
        wordList.add(w52);
        wordList.add(w53);
        wordList.add(w54);
        wordList.add(w55);
        wordList.add(w56);
        wordList.add(w57);
        wordList.add(w58);
        wordList.add(w59);
        wordList.add(w60);
        wordList.add(w61);
        wordList.add(w62);
        wordList.add(w63);
        wordList.add(w64);
        wordList.add(w65);
        wordList.add(w66);
        wordList.add(w67);
        wordList.add(w68);
        wordList.add(w69);
        wordList.add(w70);
        wordList.add(w71);
        wordList.add(w72);
        wordList.add(w73);
        wordList.add(w74);
        wordList.add(w75);
        wordList.add(w76);
        wordList.add(w77);
        wordList.add(w78);
        wordList.add(w79);
        wordList.add(w80);
        wordList.add(w81);
        wordList.add(w82);
        wordList.add(w83);
        wordList.add(w84);
        wordList.add(w85);



        final LinkedList<MediaPlayer> list = new LinkedList<MediaPlayer>();
        list.add( n1  = MediaPlayer.create(this,R.raw.assassin));
        list.add( n2 = MediaPlayer.create(this,R.raw.bashfully));
        list.add( n3 = MediaPlayer.create(this,R.raw.accessible));
        list.add( n4 = MediaPlayer.create(this,R.raw.blistering));
        list.add( n5 = MediaPlayer.create(this,R.raw.butter));
        list.add( n6 = MediaPlayer.create(this,R.raw.campaign));
        list.add( n7 = MediaPlayer.create(this,R.raw.circumstances));
        list.add( n8 = MediaPlayer.create(this,R.raw.clinching));
        list.add( n9 = MediaPlayer.create(this,R.raw.deferential));
        list.add( n10 = MediaPlayer.create(this,R.raw.distinguished));
        list.add( n11 = MediaPlayer.create(this,R.raw.embarrass));
        list.add( n12 = MediaPlayer.create(this,R.raw.etiquette));
        list.add( n13 = MediaPlayer.create(this,R.raw.fatigue));
        list.add( n14 = MediaPlayer.create(this,R.raw.feasible));
        list.add( n15 = MediaPlayer.create(this,R.raw.glitch));
        list.add( n16 = MediaPlayer.create(this,R.raw.graffiti));
        list.add( n17 = MediaPlayer.create(this,R.raw.hippopotamus));
        list.add( n18 = MediaPlayer.create(this,R.raw.hypocrisy));
        list.add( n19 = MediaPlayer.create(this,R.raw.illegible));
        list.add( n20 = MediaPlayer.create(this,R.raw.illiteracy));
        list.add( n21 = MediaPlayer.create(this,R.raw.jaundice));
        list.add( n22 = MediaPlayer.create(this,R.raw.kidnapped));
        list.add( n23 = MediaPlayer.create(this,R.raw.kinetic));
        list.add( n24 = MediaPlayer.create(this,R.raw.laboratory));
        list.add( n25 = MediaPlayer.create(this,R.raw.lacrimation));
        list.add( n26 = MediaPlayer.create(this,R.raw.macaroni));
        list.add( n27 = MediaPlayer.create(this,R.raw.maintenance));
        list.add( n28 = MediaPlayer.create(this,R.raw.mediocre));
        list.add( n29 = MediaPlayer.create(this,R.raw.metaphor));
        list.add( n30 = MediaPlayer.create(this,R.raw.nocturnal));
        list.add( n31 = MediaPlayer.create(this,R.raw.nutritious));
        list.add( n32 = MediaPlayer.create(this,R.raw.obligatory));
        list.add( n33 = MediaPlayer.create(this,R.raw.oblique));
        list.add( n34 = MediaPlayer.create(this,R.raw.opaque));
        list.add( n35 = MediaPlayer.create(this,R.raw.pageant));
        list.add( n36 = MediaPlayer.create(this,R.raw.personnel));
        list.add( n37 = MediaPlayer.create(this,R.raw.quartz));
        list.add( n38 = MediaPlayer.create(this,R.raw.quash));
        list.add( n39 = MediaPlayer.create(this,R.raw.racquetball));
        list.add( n40 = MediaPlayer.create(this,R.raw.recommend));
        list.add( n41 = MediaPlayer.create(this,R.raw.rhyme));
        list.add( n42 = MediaPlayer.create(this,R.raw.schedule));
        list.add( n43 = MediaPlayer.create(this,R.raw.souvenir));
        list.add( n44 = MediaPlayer.create(this,R.raw.strength));
        list.add( n45 = MediaPlayer.create(this,R.raw.tombstone));
        list.add( n46 = MediaPlayer.create(this,R.raw.tricycle));
        list.add( n47 = MediaPlayer.create(this,R.raw.unanswerable));
        list.add( n48 = MediaPlayer.create(this,R.raw.unconscious));
        list.add( n49 = MediaPlayer.create(this,R.raw.vacuum));
        list.add( n50 = MediaPlayer.create(this,R.raw.villain));
        list.add( n51 = MediaPlayer.create(this,R.raw.weigh));
        list.add( n52 = MediaPlayer.create(this,R.raw.weird));
        list.add( n53 = MediaPlayer.create(this,R.raw.yacht));
        list.add( n54 = MediaPlayer.create(this,R.raw.yolk));
        list.add( n55 = MediaPlayer.create(this,R.raw.aberration));
        list.add( n56 = MediaPlayer.create(this,R.raw.abject));
        list.add( n57 = MediaPlayer.create(this,R.raw.abrogation));
        list.add( n58 = MediaPlayer.create(this,R.raw.atrophy));
        list.add( n59 = MediaPlayer.create(this,R.raw.blandishment));
        list.add( n60 = MediaPlayer.create(this,R.raw.contentious));
        list.add( n61 = MediaPlayer.create(this,R.raw.dogmatic));
        list.add( n62 = MediaPlayer.create(this,R.raw.embezzlement));
        list.add( n63 = MediaPlayer.create(this,R.raw.engender));
        list.add( n64 = MediaPlayer.create(this,R.raw.fallacious));
        list.add( n65 = MediaPlayer.create(this,R.raw.fatuous));
        list.add( n66 = MediaPlayer.create(this,R.raw.flagrant));
        list.add( n67 = MediaPlayer.create(this,R.raw.fortuitous));
        list.add( n68 = MediaPlayer.create(this,R.raw.impetuous));
        list.add( n69 = MediaPlayer.create(this,R.raw.inexorable));
        list.add( n70 = MediaPlayer.create(this,R.raw.inveterate));
        list.add( n71 = MediaPlayer.create(this,R.raw.irreverence));
        list.add( n72 = MediaPlayer.create(this,R.raw.mendacious));
        list.add( n73 = MediaPlayer.create(this,R.raw.multifarious));
        list.add( n74 = MediaPlayer.create(this,R.raw.munificent));
        list.add( n75 = MediaPlayer.create(this,R.raw.paradigm));
        list.add( n76 = MediaPlayer.create(this,R.raw.plaudit));
        list.add( n77 = MediaPlayer.create(this,R.raw.proclivity));
        list.add( n78 = MediaPlayer.create(this,R.raw.promulgate));
        list.add( n79 = MediaPlayer.create(this,R.raw.reprobate));
        list.add( n80 = MediaPlayer.create(this,R.raw.surreptitious));
        list.add( n81 = MediaPlayer.create(this,R.raw.tirade));
        list.add( n82 = MediaPlayer.create(this,R.raw.umbrage));
        list.add( n83 = MediaPlayer.create(this,R.raw.veracity));
        list.add( n84 = MediaPlayer.create(this,R.raw.virtuoso));
        list.add( n85 = MediaPlayer.create(this,R.raw.vitriolic));


        int count = wordList.size();
        while(count>0){
            new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                nextWord();
            }
            }, 18000 * count);
            --count;
        } 




    }

    public void nextWord(){
        Random r = new Random();
        final int pointer = r.nextInt(wordList.size());
        final Word currentWord = wordList.remove(pointer);
        MediaPlayer.create(this, currentWord.getMedia()).start();

        //currentSentence.getMedia().start();
        final EditText enteredText = (EditText) findViewById(R.id.editText2);
        Button enterButton1 = (Button) findViewById(R.id.button);


        //Toast.makeText(getApplicationContext(), "Text is "+  currentSentence.getSentence(), Toast.LENGTH_LONG).show();

        enterButton1.setOnClickListener(new OnClickListener() {
            private static final int LONG_DELAY = 3500;
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                if(enteredText.getText().toString().equalsIgnoreCase(currentWord.getWord()) ){

                    Toast.makeText(getApplicationContext(), "You got it right!", Toast.LENGTH_LONG).show();
                    ((EditText) findViewById(R.id.editText2)).setText("");
                }
                else{

                    Toast.makeText(getApplicationContext(), "Wrong!", Toast.LENGTH_LONG).show();
                    Toast.makeText(getApplicationContext(), ""+  currentWord.getWord(), Toast.LENGTH_LONG).show();
                    ((EditText) findViewById(R.id.editText2)).setText("");
                    //Toast.makeText(getApplicationContext(), enteredText.getText().toString() + " is not " + currentSentence.getSentence(), Toast.LENGTH_SHORT).show();
                }
            }

    });



    }


    }

2 个答案:

答案 0 :(得分:1)

如果您覆盖onPause()方法,则可以放置代码以停止MediaPlayers。它将执行whenever your Activity goes into the background.为了实现此功能,您可能需要将MediaPlayers声明为Level1活动的成员变量,而不是onCreate()中的本地变量。您还可以覆盖onResume()方法以恢复播放,如果您还希望这样做。

编辑:

onPause()lifecycle method,类似于onCreate()。这意味着操作系统将在某些触发器之后调用该方法。在onPause()的情况下,每当Activity进入后台时都会发生这种情况,这就是当按下后退或回家时发生的情况。 onPause()的覆盖方式与onCreate()相同。

为了使onPause()对您的目的有用,它需要引用您要停止的MediaPlayers。您可以在活动级别存储这些引用,例如您放置wordList的位置,以便onCreateonPause都可以访问它们。您的代码看起来像这样:

public class Level1 extends Activity 
{
    MediaPlayer n1, n2, n3; // ... Through n85.  That's an awful lot of MediaPlayers, by the way.

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

        n1  = MediaPlayer.create(this,R.raw.assassin); 
        // ...init all media players...
    }

    @Override
    protected void onPause() 
    {
        n1.pause();
        n2.pause();
        // ...pause all media players...
    }
}

覆盖onResume()将以相同的方式工作,但请记住,当您的活动首次启动时,onResume()会被调用,因此请确保您的MediaPlayers不会以不正确的状态结束。希望这有帮助,如果您需要进一步解释,请告诉我。

答案 1 :(得分:0)

另一种选择是覆盖onBackPressed()并停止那里的媒体播放器。