MediaPlayer停止播放声音 - Android

时间:2014-05-14 21:13:58

标签: android audio media playback

这是一个简单的钢琴应用程序,它可以工作,但有一个问题。大约20次点击后(有时它只是28次点击),即使我点击它没有播放任何声音的按钮。该应用程序没有崩溃或没有任何警告我。这没什么。没有声音。你有什么想法吗?

package com.example.playaudio;

import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


    public class MainActivity extends Activity implements OnClickListener{
        private MediaPlayer mp;
        private MediaPlayer mp2;
        private MediaPlayer mp3;
        private MediaPlayer mp4;
        private MediaPlayer mp5;
        private MediaPlayer mp6;
        private MediaPlayer mp7;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            setVolumeControlStream(AudioManager.STREAM_MUSIC);
            Button button1=(Button)findViewById(R.id.button_1);
            Button button2=(Button)findViewById(R.id.button_2);
            Button button3=(Button)findViewById(R.id.button_3);
            Button button4=(Button)findViewById(R.id.button_4);
            Button button5=(Button)findViewById(R.id.button_5);
            Button button6=(Button)findViewById(R.id.button_6);
            Button button7=(Button)findViewById(R.id.button_7);

            button1.setOnClickListener(this);
            button2.setOnClickListener(this);
            button3.setOnClickListener(this);
            button4.setOnClickListener(this);
            button5.setOnClickListener(this);
            button6.setOnClickListener(this);
            button7.setOnClickListener(this);

        }

        public void onClick(View v) {
            int resId;
            int resId2;
            int resId3;
            int resId4;
            int resId5;
            int resId6;
            int resId7;


            switch (v.getId()) {
            case R.id.button_1:
                resId = R.raw.a;
                mp = MediaPlayer.create(this, resId);

                mp.start();
                break;
            case R.id.button_2:
                resId2 = R.raw.b;
                mp2 = MediaPlayer.create(this, resId2);
                mp2.start();
                break;
            case R.id.button_3:
                resId3 = R.raw.c;
                mp3 = MediaPlayer.create(this, resId3);
                mp3.start();
                break;
            case R.id.button_4:
                resId4 = R.raw.d;
                mp4 = MediaPlayer.create(this, resId4);
                mp4.start();
                break;
            case R.id.button_5:
                resId5 = R.raw.e;
                mp5 = MediaPlayer.create(this, resId5);
                mp5.start();
                break;
            case R.id.button_6:
                resId6 = R.raw.f;
                mp6 = MediaPlayer.create(this, resId6);
                mp6.start();
                break;
            case R.id.button_7:
                resId7 = R.raw.p;
                mp7 = MediaPlayer.create(this, resId7);
                mp7.start();
                break;

            }


        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.activity_main, menu);
            return true;
        }



    }

2 个答案:

答案 0 :(得分:6)

看起来您正在创建一个新的MediaPlayer实例来播放每个声音。您应该重复使用它们或清理它们。

来自documentation of the MediaPlayer.create()方法:

  

为给定资源ID创建MediaPlayer的便捷方法。上   成功,prepare()已经被调用,绝不可以   再次打电话。

     

完成MediaPlayer后,您应该调用release()来释放   资源。如果没有发布,将导致过多的MediaPlayer实例   在一个例外中。

答案 1 :(得分:0)

简单的方法是添加onComplationListener

mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
    @Override
    public void onCompletion(MediaPlayer p1) {
        p1.release();
    }
});