媒体播放器出现问题,无法播放大量音频文件

时间:2018-06-21 13:44:46

标签: android listview debugging adapter android-mediaplayer

我正在开发一个Android应用程序。 好吧,在此应用程序中,我有四个活动,每个活动显示一个单词列表及其翻译,如果您单击项目上的一个按钮,则还可以播放音频文件。 我已经定义了一个word.java类,这是代码:

public class Word  {
    private String defaultTransaltion;
    private String miwokTranslation;
    private Context mcontext;
    private int  mImageResourceId=NO_IMAGE_PROVIDED;
    public static final int NO_IMAGE_PROVIDED=-1;
    private int mSongId;

    public Word(String text1,String text2){
        setDefaultTransaltion(text1);
        setMiwokTranslation(text2);
    }
    public Word(String t1,String t2,int y){
        defaultTransaltion=t1;
        miwokTranslation=t2;
        mSongId=y;
    }
    public Word(String t1,String t2,int x,int y){
        defaultTransaltion=t1;
        miwokTranslation=t2;
        mImageResourceId =x;
        mSongId=y;
    }
    public boolean hasImage(){
        return( mImageResourceId!=-1);
    }

    public void setDefaultTransaltion(String text){
        defaultTransaltion=text;
    }
    public void setMiwokTranslation(String text){
        miwokTranslation=text;
    }
    public String getDefaultTransaltion(){
        return defaultTransaltion;
    }
    public String getMiwokTranslation(){
        return miwokTranslation;
    };
    public int getmImageResourceId(){return mImageResourceId;};
    public void setImageResourceId(int x){ mImageResourceId = x;};
    public int getmSongId(){return mSongId;};
    public void setmSongId(int x){mSongId=x;};
}

问题是,当启动我的应用程序时,它不会播放所有音频文件,而是播放15个音频文件,之后单击按钮时,我们听不到任何声音。 这是Wordadapter的代码,我已经更新了媒体播放器,因此如果单击该按钮,它将播放正确的音频文件:

public class WordAdapter extends ArrayAdapter<Word>{
    public int mColorRessourceId;
    private static final String LOG_TAG = WordAdapter.class.getSimpleName();
    public WordAdapter(Activity context, ArrayList<Word> wordAdapter, int z) {
        // Here, we initialize the ArrayAdapter's internal storage for the context and the list.
        // the second argument is used when the ArrayAdapter is populating a single TextView.
        // Because this is a custom adapter for two TextViews and an ImageView, the adapter is not
        // going to use this second argument, so it can be any value. Here, we used 0.
        super(context, 0, wordAdapter);
        mColorRessourceId=z;
    }
    private MediaPlayer mediaPlayer ;

    @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.list_item, parent, false);
        }
        LinearLayout rootitems = (LinearLayout) listItemView.findViewById(R.id.rootitems) ;
        final Word currentWord = getItem(position);
        TextView defaultTranslation = (TextView) listItemView.findViewById(R.id.item1);
        defaultTranslation.setText(currentWord.getMiwokTranslation());
        TextView miwokTranslation = (TextView) listItemView.findViewById(R.id.item2);
        miwokTranslation.setText(currentWord.getDefaultTransaltion());
        ImageView icone = (ImageView) listItemView.findViewById(R.id.icone_miwok);
        Log.i("NumbersActivity","Here is the ressource id "+currentWord.getmImageResourceId());
        if (currentWord.hasImage()==true) {icone.setImageResource(currentWord.getmImageResourceId());}
        else icone.setVisibility(View.GONE);
        int color= ContextCompat.getColor(getContext(),mColorRessourceId);
        rootitems.setBackgroundColor(color);
        ImageView playbutton = (ImageView) listItemView.findViewById(R.id.play);
        playbutton.setBackgroundColor(color);
        playbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mediaPlayer = MediaPlayer.create(getContext(),currentWord.getmSongId());
            mediaPlayer.start();
            }
        });
        return listItemView;
    }
}

0 个答案:

没有答案