2016-02-12 44 views
0

我在listenview中重現聲音。我有一個自定義的適配器,但它總是失敗,我把int與MP3的資源! ono肯定我perdnto的東西,但不知道什麼!如何解決的建議?謝謝listview中的媒體播放器

public class Adapter_animal extends ArrayAdapter<String> { 
private final Activity context; 
private final String[] animal; 
private final int[] animal_id; 
private MediaPlayer mp; 


public Adapter_animal(Activity context, int mylist, String[] animal, int[] animal_id) { 
    super(context, R.layout.mylist, animal); 
    // TODO Auto-generated constructor stub 
    this.context=context; 
    this.animal = animal; 

    this.animal_id = animal_id; 
} 

public View getView(final int position,View view,ViewGroup parent) { 
    LayoutInflater inflater=context.getLayoutInflater(); 
    View rowView=inflater.inflate(R.layout.mylist, null,true); 

    TextView txtTitle = (TextView) rowView.findViewById(R.id.titolo); 
    txtTitle.setText(animal[position]); 

    FloatingActionButton btn = (FloatingActionButton) rowView.findViewById(R.id.f1_btn); 

    btn.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      stopPlaying(); 
      mp = MediaPlayer.create(Adapter_animal.this, animal_id); //error in this line "cannot resolve method 
      mp.start(); 
     } 

    }); 











    return rowView; 

}; 
private void stopPlaying() { 
    if (mp != null) { 
     mp.stop(); 
     mp.release(); 
     mp = null; 
    } 
} 

static String[] animal={ 
     "animale", 
     "animale2", 
     "animale3", 

}; 

static int[] animal_id={ 
     R.raw.a11_ovation, 


}; 

回答

0

你傳遞一個數組給需要一個單一的資源ID的方法...

Documentation:

public static MediaPlayer create (Context context, int resid)

Co用於爲給定資源ID創建MediaPlayer的便捷方法。成功時,prepare()已經被調用,不能再次調用。

完成MediaPlayer後,您應該撥打release()來釋放資源。如果沒有發佈,太多MediaPlayer實例將導致異常。

注意,由於prepare()在這種方法自動調用,你不能改變音頻流類型(見setAudioStreamType(int)),音頻會話ID(見setAudioSessionId(int))或音頻屬性(見新MediaPlayersetAudioAttributes(AudioAttributes)

參數

context - 在Context使用

resid - 原料資源ID(如果創建失敗

+0

什麼我會改變一個MediaPlayer對象,或者爲null -)的資源作爲數據源使用

返回

MediaPlayer?對不起,但我不是專家,我想弄清楚如何解決! – gilione

+0

使用數組中的索引器傳遞所需的資源ID。即:animal_id [0] –