代码之家  ›  专栏  ›  技术社区  ›  gilione

列表视图中的媒体播放器

  •  0
  • gilione  · 技术社区  · 9 年前

    我在listeview中再现声音。我有一个自定义的适配器,但它总是在我将int与mp3的资源放在一起的那一行失败!我不确定我听到了什么,但不知道是什么!如何解决的建议?非常感谢。

    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,
    
    
    };
    
    1 回复  |  直到 9 年前
        1
  •  0
  •   Cory Charlton    9 年前

    您正在将一个数组传递给一个需要单个资源id的方法。。。

    Documentation:

    public static MediaPlayer create (Context context, int resid)

    创建一个 MediaPlayer 对于给定的资源id。成功后, prepare() 将已被调用,且不得再次调用。

    完成后 媒体播放器 ,你应该打电话 release() ,以释放资源。如果未释放,则过多 媒体播放器 实例将导致异常。

    请注意,自 准备() 则无法更改音频流类型(请参见 setAudioStreamType(int) ),音频会话ID(请参见 setAudioSessionId(int) )或音频属性(请参见 setAudioAttributes(AudioAttributes) 新的 媒体播放器 .

    参数

    context - Context 使用

    resid -原始资源id( R.raw.<something> )用于用作数据源的资源

    退换商品

    媒体播放器 -一个 媒体播放器 对象,如果创建失败则为空