在Android应用程序中关闭适配器时停止音频播放器

时间:2016-07-30 06:21:58

标签: android performance android-studio audio flipview

我正在开发一个Android应用程序,用户可以在其中阅读书籍并收听该书的录制音频。我正在使用翻转视图在页面之间切换。

这是我的主要活动:

public class FlipAdapter extends BaseAdapter implements OnClickListener {

public interface Callback {
}

private LayoutInflater inflater;
private Callback callback;
private ArrayList<HashMap<String, String>> data;
public lazyloading.ImageLoader imageLoader;
private Activity activity;

MediaPlayer mp;

public FlipAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
    activity = a;

    data = d;
    inflater = (LayoutInflater) activity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader = new lazyloading.ImageLoader(activity.getApplicationContext());

}

public void setCallback(Callback callback) {
    this.callback = callback;
}

@Override
public int getCount() {
    return data.size();
}

@Override
public Object getItem(int position) {
    return position;
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public boolean hasStableIds() {
    return true;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;

    if(convertView == null){
        holder = new ViewHolder();
        convertView = inflater.inflate(R.layout.page_flip, parent, false);

        HashMap<String, String> hashmaparray = new HashMap<String, String>();
        hashmaparray = data.get(position);


        holder.BookPages = (ImageView) convertView.findViewById(R.id.book_page);
        holder.ShowVideo = (VideoView) convertView.findViewById(R.id.show_video);
        holder.ShowVideo.setVisibility(View.INVISIBLE);

        if(activity.toString().contains("Personal_Library_Click")){

            String image =hashmaparray.get("image");

            imageLoader.DisplayImage(image, holder.BookPages);

        }

        if(activity.toString().contains("AdminBookFlip")){

            String im = hashmaparray.get("image");
            imageLoader.DisplayImage(im, holder.BookPages);

            String v = hashmaparray.get("video");

            mp=new MediaPlayer();

            try{
                mp.setDataSource(v);//Write your location here
                mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
                mp.prepare();
                mp.start();

            }catch(Exception e) {
                e.printStackTrace();
            }

        }

        if(activity.toString().contains("ViewBookVideo")) {

            String im = hashmaparray.get("image");
            imageLoader.DisplayImage(im, holder.BookPages);
        }

        convertView.setTag(holder);
    }else{

        holder = (ViewHolder) convertView.getTag();

    }

    //TODO set a text with the id as well

    return convertView;
}

static class ViewHolder{
    ImageView BookPages;
    VideoView ShowVideo;
}

@Override
public void onClick(View v) {
    switch(v.getId()){
    }
}

}

她的是我的适配器:

"

当我打开此活动时,它会完美运行。但是当我关闭活动时,音频会在后台运行。当我从这个活动回来时,我需要停止音频。我试图使适配器为null onBackPressed(),但这不起作用。任何人都可以指导我解决这个问题吗?谢谢

2 个答案:

答案 0 :(得分:0)

在适配器类对象中传递MediaPlayer对象,并在适配器

中使用该对象
MediaPlayer mp=new MediaPlayer();

flipView.setAdapter(new FlipAdapter(Personal_Library_Click.this, arrayVideos, mp));//mp is Mediaplayer object


 //Stop Audio

  @Override
  public void onPause(){
     //media player stops
     super.onPause();
 }

答案 1 :(得分:0)

首先,我认为你不应该在适配器中这样做。 片段/活动是更好的地方。 然后你有 onPause onResume 之类的方法,这是启动和停止MediaPlayer的最佳位置。

private MediaPlayer mediaPlayer;
@Override
public void onPause(){
    //media player stops
    super.onPause();
}
@Override
public void onResume(){
    //mediaPlayer initialize and start
    super.onResume();
}