2016-05-31 62 views
0

我試圖找到解決方案,但無法找到它。Android如何在Recycleview中啓動新聲音時停止媒體播放器

我的問題是,我有很多recycleview中的項目。每個項目都有圖像,當我點擊該圖像時,就會爲該圖像開始播放聲音。 問題是當我開始新的聲音先前的聲音不停止,它繼續播放,直到完成聲音。我點擊15後,20聲音的應用程序得到墜毀,我收到此錯誤信息:

E/MediaPlayer的:錯誤(1,-19)

不知何故,我需要實現以前的聲音的釋放和停止時,我點擊新的。我試圖找到解決方案,但我無法找到它的recycleview。

這裏是我的適配器代碼:

import android.content.Context; 
 
import android.media.MediaPlayer; 
 
import android.support.v7.widget.RecyclerView; 
 
import android.view.LayoutInflater; 
 
import android.view.View; 
 
import android.view.ViewGroup; 
 
import android.widget.ImageView; 
 
import android.widget.TextView; 
 

 
import com.bumptech.glide.Glide; 
 

 
import java.util.List; 
 

 

 
public class ZivotinjeAdapter extends RecyclerView.Adapter<ZivotinjeAdapter.ViewHolder> { 
 

 
    @Override 
 
    public ZivotinjeAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
 
     Context context = parent.getContext(); 
 
     LayoutInflater inflater = LayoutInflater.from(context); 
 

 
     // Inflate the custom layout 
 
     View contactView = inflater.inflate(R.layout.item_zivotinje, parent, false); 
 

 
     // Return a new holder instance 
 
     ViewHolder viewHolder = new ViewHolder(contactView); 
 
     return viewHolder; 
 
    } 
 

 
    @Override 
 
    public void onBindViewHolder(ZivotinjeAdapter.ViewHolder viewHolder, int position) { 
 

 

 
     Zivotinje zivotinjeIme = mZivotinje.get(position); 
 

 
     // Set item views based on the data model 
 
     TextView textView = viewHolder.nameTextView; 
 
     textView.setText(zivotinjeIme.getmAnimal_name()); 
 

 
     //THIS IS WHERE I HANDALE IMAGE VIEW. Using glide for images 
 
     ImageView slika_source = viewHolder.imageView; 
 
     Glide.with(slika_source.getContext()) 
 
       .load((Integer) zivotinjeIme.getmAnimal_slika()) 
 
       .into(slika_source); 
 

 

 

 
     viewHolder.setItem(mZivotinje.get(position)); 
 

 
    } 
 

 

 
    @Override 
 
    public int getItemCount() { 
 
     return mZivotinje.size(); 
 
    } 
 

 
    public static class ViewHolder extends RecyclerView.ViewHolder { 
 

 
     public ImageView imageView; 
 
     public TextView nameTextView; 
 
     private Zivotinje mItem; 
 

 

 

 
     public void setItem(Zivotinje item) { 
 
      this.mItem = item; 
 
     } 
 

 

 

 
     public ViewHolder(final View itemView) { 
 
      super(itemView); 
 

 
      nameTextView = (TextView) itemView.findViewById(R.id.zivotinjaIme_id); 
 
      imageView = (ImageView) itemView.findViewById(R.id.slika_id); 
 

 
      
 
//this is click event for each item and here i play sound 
 
      itemView.setOnClickListener(new View.OnClickListener() { 
 
       @Override 
 
       public void onClick(View v) { 
 
        
 
        MediaPlayer mediaPlayer = MediaPlayer.create(v.getContext(), mItem.getmSound()); 
 
        mediaPlayer.start(); 
 

 
       } 
 
      }); 
 

 
     } 
 

 

 
    } 
 

 
    private List<Zivotinje> mZivotinje; 
 

 
    public ZivotinjeAdapter(List<Zivotinje> animals) { 
 
     mZivotinje = animals; 
 
    } 
 

 

 

 

 
}

回答

0

補充一點:

@Override 
public void onClick(View v) { 
stopPlaying(); <-------    
MediaPlayer mediaPlayer = MediaPlayer.create(v.getContext(),mItem.getmSound()); 
mediaPlayer.start();} 

這:

....... 

public ZivotinjeAdapter(List<Zivotinje> animals) { 
    mZivotinje = animals; 
} 

private void stopPlaying() { 

    if(mediaPlayer != null && mediaPlayer.isPlaying()) 
    { 
     mediaPlayer.stop(); 
     mediaPlayer.reset(); 
     mediaPlayer.release(); 
     mediaPlayer = null; 
    } 
} 
+0

它是所有紅色,我不能呼叫sto從點擊pPlaying方法。 – beginner

+0

嘗試編輯... – Srdan

+0

我仍然無法達到stopPlaying方法。它寫入非靜態metod stopPlaying()不能從靜態上下文引用 – beginner

相關問題