如何在媒体播放器android图像视图中实现完成监听器

时间:2014-08-31 13:40:16

标签: java android audio android-imageview android-mediaplayer

我正在研究用户在图像中滑动时播放背景音乐的项目。当用户到达最后一张图像时,musci会停止。当用户倒退时,它也会开始播放。但是,我的歌很短,我想实现完成监听器,这样当第一首音乐结束时,第二首音乐从原始文件夹开始。我的主要问题是我不知道如何在mainactivity.java中实现它。我也阅读了android官方指南,但它没有帮助。请提供一些帮助以及解释......

我的代码如下

Mainactivity.java

    import android.app.Activity;
    import android.content.Intent;
    import android.media.MediaPlayer;
    import android.os.Bundle;
    import android.support.v4.view.ViewPager;
    import android.support.v4.view.ViewPager.OnPageChangeListener;
    import android.view.Menu;

    import android.view.MenuItem;
    import android.widget.ShareActionProvider;

    public class MainActivity extends Activity {

        MediaPlayer oursong;
        ViewPager viewPager;
        ImageAdapter adapter;

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

             oursong = MediaPlayer.create(MainActivity.this, R.raw.a);
             oursong.seekTo(0);
             oursong.start();

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


             viewPager.setOnPageChangeListener(MyViewPagerListener);
        }

        private ShareActionProvider mShareActionProvider;

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
                // Inflate menu resource file.
                getMenuInflater().inflate(R.menu.activity_main, menu);

                // Locate MenuItem with ShareActionProvider
                MenuItem item = menu.findItem(R.id.menu_item_share);

                // Fetch and store ShareActionProvider
                mShareActionProvider = (ShareActionProvider) item.getActionProvider();
                // Return true to display menu
                return true;
        }

        // Call to update the share intent
        private void setShareIntent(Intent shareIntent) {
                if (mShareActionProvider != null) {
                        mShareActionProvider.setShareIntent(shareIntent);
                }
        }

        private int pos = 0;
        @Override
        protected void onPause() {
                super.onPause();

               if(oursong != null){
                   pos = oursong.getCurrentPosition();
                   oursong.release();
                   oursong = null;
                }
        }

        @Override
        protected void onResume(){
              super.onResume();
             oursong = MediaPlayer.create(MainActivity.this, R.raw.a);
             oursong.seekTo(pos); // You will probably want to save an int to restore here
             oursong.start();
        }


       private final OnPageChangeListener MyViewPagerListener = new OnPageChangeListener() {

                @Override
                public void onPageSelected(int pos) {
                  if (pos == adapter.getCount() - 1){
                     // adding null checks for safety
                     if(oursong != null){
                        oursong.pause();
                     }

                   } else if (!oursong.isPlaying()){ 

                    // adding null check for safety
                    if(oursong != null){
                        oursong.start();
                    }
                  }         
                }

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

                }

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

                }
            };

     }

2 个答案:

答案 0 :(得分:3)

您必须在mediaplayer实例上设置完成侦听器以确定播放的歌曲

 mP.setOnCompletionListener(new OnCompletionListener() {
    @Override
    public void onCompletion(MediaPlayer mp) {
        try {

            mp.start();
            mp.setLooping(true);

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

答案 1 :(得分:1)

  1. 基本思路是您需要以正确的顺序更改播放器的状态。 OnCompletion您需要重置,更改音乐,准备异步。 OnPrepared你需要开始玩。
  2. 要做到这一点,请更改此代码:

        oursong = MediaPlayer.create(MainActivity.this, R.raw.a);
        oursong.seekTo(0);
        oursong.start();
    

    进入

        MyTracksCustomClass tracks = new MyTracksCustomClass();
    
        MediaPlayer oursong = MediaPlayer.create(MainActivity.this, R.raw.a);
        oursong.setLooping(false);
        oursong.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mediaPlayer) {
                    mediaPlayer.reset();
                    try {
                        mediaPlayer.setDataSource(MainActivity.this, tracks.getNextTrack);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    try {
                        mediaPlayer.prepareAsync();
                    } catch (IllegalStateException e) {
                        e.printStackTrace();
                    }
                    mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                        @Override
                        public void onPrepared(MediaPlayer mediaPlayer) {
                            mediaPlayer.start();
                        }
                    });
                }
            }
        });
    
    1. 您需要另一个班级来管理下一首曲目" MyTracksCustomClass"你应该在其中实现像:

      class MyTracksCustomClass {
          int trackIndex;
          HashMap<String, Integer> trackResourceIds;
          MyTracksCustomClass()
          {
              trackIndex = 1;
              // in case you only have two tracks
              trackResourceIds = new HashMap<String, Integer>();
              trackResourceIds.put("1",R.raw.a);
              trackResourceIds.put("2",R.raw.b);
          }
          public int getNextTrack() {
              // in case you only have two tracks
              trackIndex = (trackIndex > 3 )? 1 : trackIndex;
              return trackResourceIds.get(trackIndex++ +"");
      
          }
      }