簡體   English   中英

MediaPlayer:ArrayList的錯誤(1,-19)

[英]MediaPlayer: Error (1,-19) with ArrayList

我正在嘗試創建一個帶有音頻文件的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();
            }
        });


    }


}

正如你所看到我做了一個Toast以確保我的clicklistener運行良好而且確實如此,但我的問題在於mediaPlayer?

我認為您應該在媒體播放器完成播放后釋放它。 啟動mediaPlayer后,請嘗試以下操作:

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

    };
});

暫無
暫無

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

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