媒体播放器应用程序中的多个声音崩溃


  • 我开发了应用程序,其中有4个声音文件一起播放
  • 第一声 :
    • 按下下一步按钮-第二声音 :
    • 在上一个按钮按下时
  • 第三声 :
    • 播放项目的声音
  • 第四种声音:

    • 在项目上播放(项目如何说话)
  • 一切正常,直到用户播放和第四个快速的文本到语音引擎崩溃

媒体播放器: 错误 (-19, 0) 安卓

  • 我尝试重置媒体播放器,在下一个和上一个按钮中释放并停止,但在几次来回和第四次后仍然停止了项目的声音。

我做了什么:

public class A extends Activity {

    int imgArr[] = {R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d};
    int soundsrc[] = {R.raw.a, R.raw.b, R.raw.c, R.raw.d};
    String pro[] = new String[]{"a", "b", "c", "d"};
    ImageView iView;
    int count = 0;
    TextToSpeech txtToSpeech1;
    MediaPlayer mp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_names);
        stopService(new Intent(this, PlayMusic.class));

        iView = (ImageView) findViewById(R.id.imageView5);
        iView.setImageResource(R.drawable.befora);
        txtToSpeech1 = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {

            @Override
            public void onInit(int status) {
                if(status != TextToSpeech.ERROR) {
                    txtToSpeech1.setLanguage(Locale.UK);
                    txtToSpeech1.speak(pro[count], TextToSpeech.QUEUE_FLUSH, null);
                }
            }
        });
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            public void run() {
                aSounds();
            }
        }, 1000);


    }
    @Override
    public void onDestroy() {
        // Don't forget to shutdown!
        if (txtToSpeech1 != null) {
            txtToSpeech1.stop();
            txtToSpeech1.shutdown();
        }
        super.onDestroy();
    }
    public void onPause(){
        if(txtToSpeech1 !=null){
            txtToSpeech1.stop();
            txtToSpeech1.shutdown();
        }
        super.onPause();
    }
    public void aSounds() {
        mp = MediaPlayer.create(ANames.this, soundsrc[count]);
        mp.start();
    }

    public void forwardd(View v) {
        buttonSounds(R.raw.multimedia_button_click);
        if (count < imgArr.length && count >= 0) {
            count++;
            if (count == imgArr.length)
                count = 0;
            iView.setImageResource(imgArr[count]);
            if (txtToSpeech1.isSpeaking()) {
                Log.i("Tag", "Stop speaking");
                txtToSpeech1.stop();
            }
            txtToSpeech1.speak(pro[count], TextToSpeech.QUEUE_FLUSH, null);
            // Execute some code after 2 seconds have passed
            Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                public void run() {
                    aSounds();
                }
            }, 1000);

        }
    }
    public void backwardd(View v) {
        buttonSounds(R.raw.multimedia_button_click);
        if (count >= 0 && count < imgArr.length) {
            count--;
            if (count == -1) {
                count = imgArr.length - 1;
            }
            iView.setImageResource(imgArr[count]);
            txtToSpeech1.speak(pro[count], TextToSpeech.QUEUE_FLUSH, null);
            Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                public void run() {
                    aSounds();
                }
            }, 1000);

        }
    }
    public void onImageClick(View v) {
        txtToSpeech1.speak(pro[count], TextToSpeech.QUEUE_FLUSH, null);
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            public void run() {
                aSounds();
            }
        }, 1000);
    }
    public void home(View v) {
        buttonSounds(R.raw.multimedia_button_click);
        Intent ii = new Intent(ANames.this, PlayMusic.class);
        finish();
        startService(ii);
    }
    public void buttonSounds(int src) {
        mp = MediaPlayer.create(ANames.this, R.raw.multimedia_button_click);
        mp.start();
    }

}

-- 我已经尝试了很长时间

- 尝试了几乎所有关于媒体播放器工作的堆栈溢出答案。

- 还有如何在单击按钮时停止,释放,重置,启动媒体播放器,但对我没有任何用处。

-

-所以最后决定把问题放在堆积如山的堆里寻求帮助

-- 任何帮助都非常欢迎,提前感谢

如果按钮播放声音效果,如 1-4 秒。考虑使用 SoundPool 类。据我所知,它应该用于那些东西。媒体播放器错误链接和链接。似乎无论谁收到错误 19,都需要释放他正在播放的资源。

最新更新