簡體   English   中英

使用MediaPlayer播放聲音

[英]Playing sound using MediaPlayer

我創建了一個簡單的應用,該應用在屏幕上沒有幾個按鈕。 其中一個按鈕稱為soundButton,當用戶按下該按鈕時,將播放音頻文件。 (例如,像“蘋果”這樣的簡單詞)。 我有大約100個類似的屏幕,當我第一次播放聲音時,我可以聽到聲音,過了一會兒,無論我轉到哪個屏幕,我都再也聽不到它們了(即使是以前的屏幕)不再播放聲音了)這是我的編碼,僅供參考:

package com.example.main;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;

public class EngApple extends Activity{

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.eng_apple);

        ImageButton soundButton = (ImageButton) findViewById(R.id.sound);
        final MediaPlayer mp1 = MediaPlayer.create(this, R.drawable.apple);


        soundButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
            mp1.start();
            }
        });
    }


}

有誰可以幫助我解決這個問題?

您必須在Activity中的onPause()onStop()中調用MediaPlayer方法release()

http://developer.android.com/reference/android/media/MediaPlayer.html#release()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM