MediaPlayer和地图发布android应用程序(noob问题)

MediaPlayer和地图发布android应用程序(noob问题),android,map,media-player,Android,Map,Media Player,我只是在这里学习。我正在尝试制作一个大约有40种声音的音板,但我在如何使用maphash使其工作时遇到了一些问题。有人能救我吗 --------------音板------------------------- import android.app.Activity; import android.content.Intent; import android.media.MediaPlayer; import android.os.Bundle; import android.view.View

我只是在这里学习。我正在尝试制作一个大约有40种声音的音板,但我在如何使用maphash使其工作时遇到了一些问题。有人能救我吗

--------------音板-------------------------

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import java.util.HashMap;
import java.util.Map;

public class main extends Activity {

MediaPlayer mp=null;

\\\if I put put "MediaPlayer mp;" here it only plays one sound\\\

ImageButton Button;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Map<Integer, Integer> map = new HashMap<Integer, Integer>();
    map.put(R.id.button1, R.raw.sound1);
    map.put(R.id.button2, R.raw.sound2);

    for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
        mp = MediaPlayer.create(this, entry.getValue()); 


 \\\if I put "final MediaPlayer mp = MediaPlayer.create(this, entry.getValue());" here I cant stop MediaPlayer with onpause and onstop overrides.\\\


        ImageButton button = (ImageButton) findViewById(entry.getKey());
        button.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                mp.start();
            }

        });
    }
}

    @Override
    protected void onStop() {
        super.onStop();
        if(mp.isPlaying()){ 
            mp.stop();
            mp.release();
        }
    }

    @Override
     public void onDestroy(){
     super.onDestroy();
        mp.release();
    }

}
导入android.app.Activity;
导入android.content.Intent;
导入android.media.MediaPlayer;
导入android.os.Bundle;
导入android.view.view;
导入android.widget.ImageButton;
导入java.util.HashMap;
导入java.util.Map;
公共类主要扩展活动{
MediaPlayer mp=null;
\\\如果我把“MediaPlayer mp;”放在这里,它只播放一个声音\\\
图像按钮;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Map Map=newhashmap();
地图放置(R.id.button1,R.raw.sound1);
地图放置(R.id.button2,R.raw.sound2);
对于(Map.Entry:Map.entrySet()){
mp=MediaPlayer.create(这个,entry.getValue());
\\\如果我把“final MediaPlayer mp=MediaPlayer.create(this,entry.getValue());”放在这里,我就不能用onpause和onstop覆盖停止MediaPlayer\\\
ImageButton=(ImageButton)findViewById(entry.getKey());
setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
mp.start();
}
});
}
}
@凌驾
受保护的void onStop(){
super.onStop();
如果(mp.isPlaying()){
mp.stop();
mp.release();
}
}
@凌驾
公共空间{
super.ondestory();
mp.release();
}
}
正如我在中所建议的,不要创建所有这些mediaplayer实例,原因有两个:

  • 您将丢失所有实例,并且只有最后一个实例。总是
  • onCreate()无缘无故成为非常长的方法
  • 相反,删除
    mp=MediaPlayer.create(这是entry.getValue())从for循环,并将其移动到侦听器内部,类似于(未测试…):

    因此,您将仅在需要时创建mediaplayer实例


    顺便说一句,main并不是一个好名字。

    你面临的问题是什么?@MByD我不知道该怎么说,因为我还是个新手,但如果你不知道的话。如果我把“MediaPlayer mp;”放在活动之后但在“oncreate”之前,我只能播放我的许多声音中的一种。如果我将“final MediaPlayer mp”放在“oncreate”之后,应用程序关闭后声音将继续播放。我正试图让它双向运行,这样所有的声音都能正常工作,如果应用程序移到后台,我可以暂停/停止/释放。哦,谢谢你在我上一篇文章中的帮助,我已经为此工作了几个小时。好的,我明白你的意思了!一开始我没听懂。我现在就去。谢谢你,谢谢你在这方面的帮助。@Colby-你不应该把它声明为
    final
    ,差一点我就要命了。哈哈,我在“mp=MediaPlayer行”mp=MediaPlayer.create(这个,声音)方面遇到了问题;error=“MediaPlayer类型中的方法create(Context,int)不适用于参数(new View.OnClickListener(){},int)”mp=MediaPlayer.create(getApplicationContext(),sound);error=无法分配最终局部变量mp,因为它是在封闭类型中定义的
    button.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
                int sound = map.get(v.getId());
                mp = MediaPlayer.create(main.this, sound);
                mp.start();
            }
        });