具有动态原始文件的Mediaplayer

时间:2019-07-10 03:42:46

标签: android

我正在创建一个学习德语的应用程序,其中我想为每个单词的发音添加mediaPlayer,但要添加mediaplayer。但是我不能播放最多15个音频,之后它不会播放。Word是一个Java文件动态arraylist和wordadapter是我的动态数组适配器。在Word适配器中,我没有对Word进行任何更改,我添加了int资源ID和get方法。我应该如何播放所有计数到20的数字。

public class Numbers extends AppCompatActivity {
   private MediaPlayer mp ;
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mp.release();
        mp = null;

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_numbers);
        final ArrayList<Words> mylist = new ArrayList<>(10);
       mylist.add(new Words("Zero","Null", R.drawable.zero,R.raw.nullf));
        mylist.add(new Words("One","Eins",R.drawable.one,R.raw.eins));
        mylist.add(new Words("Two","zwei",R.drawable.two,R.raw.zwei));
        mylist.add(new Words("Three","drei",R.drawable.three,R.raw.deei));
        mylist.add(new Words("Four","vier",R.drawable.fourf,R.raw.vier));
        mylist.add(new Words("Five","fünf",R.drawable.five,R.raw.funf));
        mylist.add(new Words("six","sechs",R.drawable.six,R.raw.sechs));
        mylist.add(new Words("Seven","sieben",R.drawable.seven,R.raw.sieben));
        mylist.add(new Words("Eight","acht",R.drawable.eight,R.raw.acht));
        mylist.add(new Words("nine","neun",R.drawable.nine,R.raw.neun));
        mylist.add(new Words("Ten","zehn",R.drawable.ten,R.raw.zehn));
        mylist.add(new Words("Eleven","elf",R.drawable.eleven,R.raw.elf));
        mylist.add(new Words("Twelve","zwölf",R.drawable.twelve,R.raw.zwolf));
        mylist.add(new Words("thirteen","dreizehn",R.drawable.thirteenf,R.raw.dreizehn));
        mylist.add(new Words("Fourteen","vierzehn",R.drawable.fourteen,R.raw.vierzehn));
        mylist.add(new Words("Fifteen","fünfzehn",R.drawable.fifteen,R.raw.funfzehn));
        mylist.add(new Words("Sixteen","sechzehn",R.drawable.sixteenf,R.raw.sechzehn));
        mylist.add(new Words("Seventeen","siebzehn",R.drawable.seventeen,R.raw.siebzehn));
        mylist.add(new Words("eighteen","achtzehn",R.drawable.eighteen,R.raw.achtzehn));
        mylist.add(new Words("nineteen","neunzehn",R.drawable.nineteen,R.raw.neunzehn));
        mylist.add(new Words("twenty","zwanzig",R.drawable.twenty,R.raw.zwanzig));


        WordAdapter words = new WordAdapter(this,mylist,R.color.colorblue);
        ListView listView = findViewById(R.id.rootview);
        listView.setAdapter(words);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
               Words word = mylist.get(position);
               mp = mp.create(Numbers.this,word.getAudio());
               mp.start();
            }
        });
    }
}

1 个答案:

答案 0 :(得分:0)

要解决给定的问题,我们必须在播放歌曲之前先释放()媒体播放器,而且还必须在完成时释放。