媒体播放器响应第二次点击暂停 [英] Mediaplayer respond to second click to pause

查看:185
本文介绍了媒体播放器响应第二次点击暂停的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用ViewPager滑动图像,每个图像点击流不同的MP3。

Used ViewPager for images sliding, each image onclick stream different MP3.

app包括50页(图像)& 50个不同的mp3,所有mp3都存储在应用程序本身。

app including 50 pages(images) & 50 different mp3, all mp3 stored on app itself.

ex。第一页流MP3(一),第二页流MP3(二)等到第五十页。

ex. First-page stream MP3(one), second-page stream MP3(two) and so on till page fifty.

第一:

它的工作正常只有一个问题:

its work fine just only one issue which is:

In any page --> clicking the image --> 

 PLAY MP3(one) --> click again--> PAUSE MP3 -->
 in paused state of MP3(one) SWIPE to next page --> 

--> in next page --> clicking the image -->
  PLAY MP3(two) --> click to pause the MP3(two)

   --> it doesn't respond to first click,it respond to second click to pause MP3(two).

第二:

该应用程序包含50个页面和50个不同的MP3,我是否需要重复50次我已经做过的媒体播放器代码,或者有更好的方法来应用于所有50个媒体播放器MP3的单个代码,因为所有的都有相同的功能周期。

the app contain 50 pages and 50 different MP3, does i need to repeat the mediaplayer code 50 times which i already did , or there is better approach to do that in single code applied to all 50 mediaplayer MP3 , as all has the same function cycle.

请提供任何建议以及如何将其应用于编码。

any advice please and how to apply it in coding .

MainActivity:

MainActivity :

public class MainActivity extends Activity {

private ViewPager mViewPager;
MediaPlayer mp;
private boolean isPaused;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mViewPager= (ViewPager) findViewById(R.id.view_pager);
    ImageAdapter adapter = new ImageAdapter(this);
    mViewPager.setAdapter(adapter);

    final GestureDetector tapGestureDetector = new GestureDetector(this, new TapGestureListener());
    mViewPager.setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            tapGestureDetector.onTouchEvent(event);
            return false;
        }
    });
}

private class TapGestureListener extends GestureDetector.SimpleOnGestureListener {
    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
        if (mViewPager.getCurrentItem() == 0) {
            if (mp != null) {
                if (isPaused) {
                    mp.start();
                    isPaused = false;
                } else {
                    mp.pause();
                    isPaused = true;
                }
            } else {
                mp = MediaPlayer.create(MainActivity.this, R.raw.aa);
                mp.start();

                    }
                }

        if (mViewPager.getCurrentItem() == 1) {
            if (mp != null) {
                if (isPaused) {
                    mp.start();
                    isPaused = false;
                } else {
                    mp.pause();
                    isPaused = true;
                }
            } else {
                mp = MediaPlayer.create(MainActivity.this, R.raw.bb);
                mp.start();                  
            }
        }

        if (mViewPager.getCurrentItem() == 2) {
            if (mp != null) {
                if (isPaused) {
                    mp.start();
                    isPaused = false;
                } else {
                    mp.pause();
                    isPaused = true;
                }
            } else {
                mp = MediaPlayer.create(MainActivity.this, R.raw.cc);
                mp.start();                 
            }
        }

        if (mViewPager.getCurrentItem() == 3) {
            if (mp != null) {
                if (isPaused) {
                    mp.start();
                    isPaused = false;
                } else {
                    mp.pause();
                    isPaused = true;
                }
            } else {
                mp = MediaPlayer.create(MainActivity.this, R.raw.dd);
                mp.start();
            }
        }
      //AND SO ON FOR 50 PAGES//  
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {   
                if (mp == null) {
                    return;
                }
                mp.release();
                mp = null;
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                // TODO Auto-generated method stub                  
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                // TODO Auto-generated method stub
            }
        });

        mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            public void onCompletion(MediaPlayer m) {
                Toast.makeText(MainActivity.this, 
                        "COMPLETED", Toast.LENGTH_LONG).show();
                // Set the MainActivity member to null
                MainActivity.this.mp = null;
            }
        });

        return super.onSingleTapConfirmed(e);
       }
   }
}

ImageAdapter:

ImageAdapter:

  public class ImageAdapter extends PagerAdapter {
Context context;
private int[] GalImages = new int[] {
    R.drawable.a,
    R.drawable.b,
    R.drawable.c,
    R.drawable.d,

};
ImageAdapter(Context context){
    this.context=context;
}
@Override
public int getCount() {
  return GalImages.length;
}

@Override
public boolean isViewFromObject(View view, Object object) {
  return view == ((ImageView) object);
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
  ImageView imageView = new ImageView(context);
  int padding = context.getResources().getDimensionPixelSize(R.dimen.activity_horizontal_margin);
  imageView.setPadding(padding, padding, padding, padding);
  imageView.setScaleType(ImageView.ScaleType.CENTER);
  imageView.setImageResource(GalImages[position]);
  ((ViewPager) container).addView(imageView, 0);
  return imageView;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
  ((ViewPager) container).removeView((ImageView) object);
   }
 }

我是android的新手,我试图修复但它没有成功,它的简单应用程序只包括MainActivity& ImageAdapter。

I'm new to android, I tried to fix it but with no success, its simple app just includes MainActivity & ImageAdapter.

我尝试了以下代码,但问题仍未解决:

i tried the below code also still the issue not resolved:

  mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {   
                if (mp != null) {
                    if (mp.isPlaying()) {
                        mp.stop();
                    }
                    mp.release();
                    mp = null;
                }
            }
            @Override
            public void onPageScrollStateChanged(int arg0) {
                // TODO Auto-generated method stub                  
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                // TODO Auto-generated method stub
            }
        });

更新:

在任何页面中:

让我们说第(1)页--->点击播放MP3 --->(播放)--->点击暂停MP3 --->(暂停)--->滑动到任何页面Rt或Lf --->(例如刷到页面(两个))--->点击播放MP3页面(二)---> (播放)--->点击暂停MP3页面(二)--->(暂停)--->(所有以前的onclick动作正常工作)--->点击继续播放MP3页面(二)已经处于暂停状态 ====> 但是当您单击第(2)页中的图像以恢复播放MP3时,您的手指不是直的,其略微倾斜,因此手指点击导致(点击在第(2)页同时刷到页面(三) ====> 这里是页面(三)MP3开始播放本身的问题开头。

let’s say page (one) ---> click to play MP3 ---> (playing) ---> click to pause MP3 ---> (paused) ---> swipe to any page Rt or Lf --->( swiped to page (two) for example ) ---> click to play MP3 on page (two) ---> (playing) ---> click to pause MP3 on page (two) ---> (paused) ---> (all previous onclick action working correctly) ---> click to resume playing MP3 in page (two) which already in paused state ====> BUT mistakenly when you click the image in page (two) to resume playing the MP3 , your finger not straight , its slightly tilted so the finger click lead to ( click on page (two) and in the same time swipe to page (three) ====> here is the issue the page (three) MP3 start playing by itself from the beginning .

我想要偶尔发生这种情况或者错误地不在页面中播放MP3(三)直到我点击页面中的图像(三)然后开始播放MP3。

I want if this happened occasionally or mistakenly not to start playing the MP3 in page (three) until I click the image in page (three) then start playing the MP3.

任何帮助都将非常感激。

any help will be really appreciated.

推荐答案

第一个问题回答: -

只需复制粘贴此代码即可让我知道没有测试它,但有一种强烈的感觉它会起作用。

Just copy paste this code and let me know I have not tested it but have a strong feeling it will work.

import java.lang.reflect.Field;
    public class MainActivity extends Activity {

        private ViewPager mViewPager;
        MediaPlayer mp;
        private boolean isPaused;
        private ArrayList<Integer> mMp3s;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            mViewPager = (ViewPager) findViewById(R.id.view_pager);
            ImageAdapter adapter = new ImageAdapter(this);
            mMp3s=getAllMp3FilesFromRaw();
            mViewPager.setAdapter(adapter);

            final GestureDetector tapGestureDetector = new GestureDetector(this, new TapGestureListener());
            mViewPager.setOnTouchListener(new View.OnTouchListener() {
                public boolean onTouch(View v, MotionEvent event) {
                    tapGestureDetector.onTouchEvent(event);
                    return false;
                }
            });
        }

        private class TapGestureListener extends GestureDetector.SimpleOnGestureListener {
            @Override
            public boolean onSingleTapConfirmed(MotionEvent e) {

                    if (mp != null) {
                        if (isPaused) {
                            mp.start();
                            isPaused = false;
                        } else {
                            mp.pause();
                            isPaused = true;
                        }
                    } else {
                        mp = MediaPlayer.create(MainActivity.this, mMp3s.get(mViewPager.getCurrentItem()));
                        mp.start();
                        isPaused = false;
                    }


                //AND SO ON FOR 50 PAGES//
                mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                    @Override
                    public void onPageSelected(int position) {
                        if (mp == null) {
                            return;
                        }
                        mp.release();
                        mp = null;
                        isPaused = true;
                    }

                    @Override
                    public void onPageScrollStateChanged(int arg0) {
                        // TODO Auto-generated method stub
                    }

                    @Override
                    public void onPageScrolled(int arg0, float arg1, int arg2) {
                        // TODO Auto-generated method stub
                    }
                });

                mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    public void onCompletion(MediaPlayer m) {
                        Toast.makeText(MainActivity.this, "COMPLETED", Toast.LENGTH_LONG).show();
                        // Set the MainActivity member to null
                        finish();
                        MainActivity.this.mp = null;
                    }
                });

                return super.onSingleTapConfirmed(e);
            }
        }

        /**
        *Do it on background thread if it blocks the UI.
        */
        private ArrayList<Integer> getAllMp3FilesFromRaw() {
            ArrayList<Integer> mp3s = new ArrayList<>();
            try {
                Field fields[] = R.raw.class.getDeclaredFields();
                for (int i = 0; i < fields.length; i++) {
                    Field f = fields[i];

                    int resIdentifier = getResources().getIdentifier(f.getName(), "raw", getPackageName());
                    mp3s.add(resIdentifier);
                }
                return mp3s;

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

逻辑是全部启动应用程序的mp3文件资源ID,如果这个过程花费太多时间在后台线程上做。

The logic is get all the mp3 files resource id when you start the app if this process is taking too much time do it on background thread.

使用这些resorce id现在播放你的歌曲有很多您获取的列表问题可能与您的图片或viewpager编号不匹配,如果您遇到任何错误,则必须应用其他逻辑告诉我。

using those resorce id play your songs now there much be some issue with the list which you fetch like they may not match with your image or viewpager number for that you will have to apply some other logic if you encounter any error let me know.

Sec问题答案: -
现在我假设当你想点击图像同时播放音乐时,页面会被刷过,当它到达其他时页面它立即开始播放该页面的音乐。如果我理解你的问题,因为我仍然不知道你使用的代码,我将引导你完成基于你可以应用逻辑的方法。

Sec Questions answer:- Now I presume that when you want to click on the image to play the music at the same time by mistake page is getting swiped and when it reached to other page it start playing that page's music immediately. If I understood you question as I still don't know which code are you using I ll guide you through the methods based on that you can apply the logic.

当页面然后更改为下一个

when page changes to next one then

onPageChanged(int position)

方法将被称为 https://stackoverflow.com/a/11294494/5492047 在这里你可以应用逻辑。
现在如何,如果音乐正在播放,你在第一页,你刷到第二个,你必须做什么

method will be called https://stackoverflow.com/a/11294494/5492047 here you can apply the logic. Now how, if the music is playing and you are on the first page and you swiped on to the second one what you have to do in

onPageChanged(int position)
    {
     mp.release();
     mp=null;
    //Now initialiase it again with page two's music.
    mp = MediaPlayer.create(MainActivity.this, R.raw.aa);
    }

并且不要调用mp.start();
现在当用户点击图片时

and don't call mp.start(); now when user clicks on the image in

 OnClickListener(View view){
    //use the same instance of mediaplayer and call mp.start();
    if(isPlaying){
       mp.pause();
    }else{
    mp.start();
 }
}

我假设你只使用一个实例MediaPlayer的内容与您在代码中显示的不一样。

这篇关于媒体播放器响应第二次点击暂停的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆