MediaPlayer: ошибка (1,-19) с ArrayList

#java #android #arraylist #android-mediaplayer

#java #Android #arraylist #android-mediaplayer

Вопрос:

Я пытаюсь создать arraylist с аудиофайлами, которые должны запускаться при нажатии определенной кнопки. Я получаю сообщение об ошибке MediaPlayer: ошибка (1,-19)

Вот код моего MainObject:

 /**
 * Displays text to the user.
 */
public class MainObject {


    //First we Set the Stat of our Class :

    // Audio File Id

    private  int en_AudioResourceID;

    private  int tz_AudioResourceID;

    // Icons Id

    private int imageResourceId;

    // English Translation
    private   String englishTranslation;

    //Tamazight Translation
    private   String tamazightTranslation;

    /**
     * Create a new MainObject Object :
     *
     * @param englishTranslation   is for ENGLISH NUMBERS
     * @param tamazightTranslation is for TAMAZIGHT NUMBERS
     * @param imageResourceId is the drawable Resource id for images assets
     */

    //Constructor
    public MainObject(String englishTranslation, String tamazightTranslation,
                      int imageResourceId ,int en_AudioResourceID, int tz_AudioResourceID) {

        this.imageResourceId = imageResourceId;
        this.englishTranslation = englishTranslation;
        this.tamazightTranslation = tamazightTranslation;
        this.en_AudioResourceID = en_AudioResourceID;
        this.tz_AudioResourceID = tz_AudioResourceID;

    }

    //Getter
    public  String getEnglishTranslation() {
        return englishTranslation;
    }


    public  String getTamazightTranslation() {
        return tamazightTranslation;
    }



    public Integer getImageResourceId(){return imageResourceId;}

    public Integer getEnAudioResourceId(){return en_AudioResourceID;}

    public Integer getTzAudioResourceId(){return tz_AudioResourceID;}

}
  

И вот код моего адаптера:

 public class ColorsAdapter extends ArrayAdapter<MainObject> {

    // Create ColorsAdapter's Constructor
    public ColorsAdapter(Context context, ArrayList<MainObject> Colors) {
        super(context, 0, Colors);
    }

    //Override GetView Method
    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

        View listItemView = convertView;
        if (listItemView == null) {
            listItemView = LayoutInflater.from(getContext()).inflate(R.layout.activities_layout, parent, false);
        }
        //Get The MainObject object at the the exact Position in the list
        MainObject currentColor = getItem(position);


        //find the TextView in the list_item.xml file by ID
        TextView tamazight_item = (TextView) listItemView.findViewById(R.id.tamazight_text_view);
        //find the TextView in the list_item.xml file by ID
        TextView english_item = (TextView) listItemView.findViewById(R.id.english_text_view);

        //find the ImageView in the list_item.xml file by ID
        ImageView icon_item = (ImageView) listItemView.findViewById(R.id.icon_view);

        //find the Icon id from our NumbersObject object and set the Text on the TextView
        icon_item.setImageResource(currentColor.getImageResourceId());


        //find the EnglishTranslation from our MainObject object and set the Text on the TextView
        tamazight_item.setText(currentColor.getEnglishTranslation());

        //find TamazightTranslation from our MainObject object and set the Text on the TextView

        english_item.setText(currentColor.getTamazightTranslation());





        //Return the whole list item Layout
        return listItemView;

    }
}
  

И, наконец, код моей ColorActivity, в котором есть мой ArrayList:

 public class ColorsActivity extends AppCompatActivity {

    //Add English MainObject to The ARRAYList

    //First we create a new Colors Object
    final ArrayList<MainObject> Colors = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.word_list);



        Colors.add(new MainObject("Black", "Asggan",R.drawable.black ,R.raw.black ,R.raw.asgan));
        Colors.add(new MainObject("Red", "Azgwagh",R.drawable.red ,R.raw.red ,R.raw.azgwar));
        Colors.add(new MainObject("Yellow", "Awragh",R.drawable.yellow ,R.raw.yellow ,R.raw.awragh));
        Colors.add(new MainObject("Blue", "Anili",R.drawable.blue ,R.raw.bleu ,R.raw.anili));
        Colors.add(new MainObject("White", "Umlil/AmlLal",R.drawable.white ,R.raw.white ,R.raw.amllal));
        Colors.add(new MainObject("Green", "Azgzaw",R.drawable.green ,R.raw.green ,R.raw.azgwar));
        Colors.add(new MainObject("Orange", "Alimuni/Alitchini",R.drawable.orange ,R.raw.orange ,R.raw.alimoni));
        Colors.add(new MainObject("Gray", "Abrighdi/Amllighdi",R.drawable.gray ,R.raw.gray ,R.raw.abrighdi));
        Colors.add(new MainObject("Purple", "Amkzay",R.drawable.purple ,R.raw.purple ,R.raw.amkzay));
        Colors.add(new MainObject("Brown", "Admman",R.drawable.brown ,R.raw.brown ,R.raw.adman));
        Colors.add(new MainObject("Pink", "Azwawagh",R.drawable.pink ,R.raw.pink ,R.raw.azwawagh));


        //Colors.remove(0);
        //Colors.size();
        // Colors.get(1);
        //Create a NEW TV object




        /**Creating an ArrayAdapter (DATA HOLDER)
         @param Context / the ACTIVITY concerned
         @param Android.R.layout : an XML layout file contains TextView predefined by Android
         @param Items to display */


        ColorsAdapter itemsAdapter = new ColorsAdapter (this, Colors);

        // Linking the ListView object to a Variable
        ListView colorsListView = (ListView) findViewById(R.id.word_list);
        //Calling setAdapter Method on VerbsListView with "itemsAdapter
        colorsListView.setAdapter(itemsAdapter);

        colorsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                MainObject listViewItem = Colors.get(position);

                MediaPlayer mMediaPlayer = MediaPlayer.create(ColorsActivity.this, listViewItem.getEnAudioResourceId());

                mMediaPlayer.start();
                Toast.makeText(ColorsActivity.this, "ItemClicked", Toast.LENGTH_LONG).show();
            }
        });


    }


}
  

Как вы можете видеть, я поднял тост, чтобы убедиться, что мой clicklistener работает хорошо, и это так, но моя проблема в MediaPlayer?

Ответ №1:

Я думаю, вам следует освободить свой медиаплеер после завершения воспроизведения. Попробуйте это после запуска вашего MediaPlayer:

 mMediaPlayer.start();
mMediaPlayer.setOnCompletionListener(new 
OnCompletionListener() {
    public void onCompletion(MediaPlayer mp) {
        mp.release();

    };
});