MediaPlayer stops playing the sounds - Android

心已入冬 提交于 2019-12-19 09:07:25

问题


Here is a simple piano app and it works but there is a problem. After about 20 clicks (sometimes it is exactly 28 clicks) even I click the buttons it doesn't play any sound. The app doesnt crash or doesn't warn me about anything. It is just nothing . There is no sound. Do you have any idea?

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



    }

回答1:


Looks like you're creating a new MediaPlayer instance to play each sound. You should either reuse them or clean them up.

From the documentation of the MediaPlayer.create() method:

Convenience method to create a MediaPlayer for a given resource id. On success, prepare() will already have been called and must not be called again.

When done with the MediaPlayer, you should call release(), to free the resources. If not released, too many MediaPlayer instances will result in an exception.



来源:https://stackoverflow.com/questions/23665250/mediaplayer-stops-playing-the-sounds-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!