单击图像按钮时如何停止3个媒体播放器?

时间:2016-07-13 13:11:16

标签: java android media-player android-mediaplayer imagebutton

我有3个媒体播放器,当我点击图像按钮时,我想要所有三个媒体播放器停在一起。

这是我的代码:

private MediaPlayer mp,mp1,mp2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.page1);
    final TextView myText = (TextView) findViewById(R.id.textView1 );
        MediaPlayer mp = MediaPlayer.create(Page1Activity.this, R.raw.two);
            mp.start();
            myText.setText("2");
    final TextView myText2 = (TextView) findViewById(R.id.textView2 );
        MediaPlayer mp1 = MediaPlayer.create(Page1Activity.this, R.raw.two);
            mp1.start();
            myText2.setText("2");
    final TextView myText3 = (TextView) findViewById(R.id.textView3 );
        MediaPlayer mp2 = MediaPlayer.create(Page1Activity.this, R.raw.four);
            mp2.start();
            myText3.setText("4");
    ImageButton btn1 = (ImageButton) findViewById(R.id.imageButton1);
    btn1.setOnClickListener(new OnClickListener()
    {   public void onClick(View v)
        {  Intent i = new Intent(Page1Activity.this, Page2Activity.class);     
           startActivity(i);

            if (mp != null) { mp.stop(); mp.release(); mp = null;}
            if (mp1 != null) { mp1.stop(); mp1.release(); mp1 = null;}
            if (mp2 != null) { mp2.stop(); mp2.release(); mp2 = null;}
        } });

如何将所有三个媒体播放器停在一起?

1 个答案:

答案 0 :(得分:1)

private MediaPlayer mp,mp1,mp2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.page1);
    final TextView myText = (TextView) findViewById(R.id.textView1 );
        mp = MediaPlayer.create(Page1Activity.this, R.raw.two);
            mp.start();
            myText.setText("2");
    final TextView myText2 = (TextView) findViewById(R.id.textView2 );
        mp1 = MediaPlayer.create(Page1Activity.this, R.raw.two);
            mp1.start();
            myText2.setText("2");
    final TextView myText3 = (TextView) findViewById(R.id.textView3 );
        mp2 = MediaPlayer.create(Page1Activity.this, R.raw.four);
            mp2.start();
            myText3.setText("4");
    ImageButton btn1 = (ImageButton) findViewById(R.id.imageButton1);
    btn1.setOnClickListener(new OnClickListener()
    {   public void onClick(View v)
        {  Intent i = new Intent(Page1Activity.this, Page2Activity.class);     
           startActivity(i);

            if (mp != null) { mp.stop(); mp.release(); mp = null;}
            if (mp1 != null) { mp1.stop(); mp1.release(); mp1 = null;}
            if (mp2 != null) { mp2.stop(); mp2.release(); mp2 = null;}
        } });

老兄..你宣布这两次了。请尝试此代码。