0

I am creating an app to learn german in which i want to add mediaPlayer for pronunciation of each word but by adding mediaplayer.But i am able to play audio only upto 15 after that its not playing Here Word is a java file for dynamic arraylist and wordadapter is my dynamic array adapter . In word adapter i did not do any change in Word i added int resource id and get method.How should i play all counting upto 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();
            }
        });
    }
}
  • You never initialized `mp` – Andreas Jul 10 '19 at 04:13
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Andreas Jul 10 '19 at 04:13

1 Answers1

0

To solve the given problem we have to release() mediaplayer before playing song and also have to release on completion.