列表视图中的媒体播放器
media player in listview
我在 listeview 中重现声音。我有一个自定义适配器,但它总是在我将 int 与 mp3 资源放在一起的那一行失败! ono 肯定我 perdnto 的东西,但不知道是什么!请教如何解决?谢谢
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,
};
您正在将数组传递给需要单个资源 ID 的方法...
public static MediaPlayer create (Context context, int resid)
Convenience method to create a MediaPlayer
for a given resource id. On success, prepare()
will already have been called and must not be called again.
When done with the MediaPlayer
, you should call release()
, to free the resources. If not released, too many MediaPlayer
instances will result in an exception.
Note that since prepare()
is called automatically in this method, you cannot change the audio stream type (see setAudioStreamType(int)
), audio session ID (see setAudioSessionId(int)
) or audio attributes (see setAudioAttributes(AudioAttributes)
of the new MediaPlayer
.
Parameters
context
- the Context
to use
resid
- the raw resource id (R.raw.<something>
) for the resource to use as the datasource
Returns
MediaPlayer
- a MediaPlayer
object, or null if creation failed
我在 listeview 中重现声音。我有一个自定义适配器,但它总是在我将 int 与 mp3 资源放在一起的那一行失败! ono 肯定我 perdnto 的东西,但不知道是什么!请教如何解决?谢谢
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,
};
您正在将数组传递给需要单个资源 ID 的方法...
public static MediaPlayer create (Context context, int resid)
Convenience method to create a
MediaPlayer
for a given resource id. On success,prepare()
will already have been called and must not be called again.When done with the
MediaPlayer
, you should callrelease()
, to free the resources. If not released, too manyMediaPlayer
instances will result in an exception.Note that since
prepare()
is called automatically in this method, you cannot change the audio stream type (seesetAudioStreamType(int)
), audio session ID (seesetAudioSessionId(int)
) or audio attributes (seesetAudioAttributes(AudioAttributes)
of the newMediaPlayer
.Parameters
context
- theContext
to use
resid
- the raw resource id (R.raw.<something>
) for the resource to use as the datasourceReturns
MediaPlayer
- aMediaPlayer
object, or null if creation failed