【问题标题】:I can't stop my media player in android?我无法在 android 中停止我的媒体播放器?
【发布时间】:2016-08-02 12:18:32
【问题描述】:

我正在使用媒体播放器在计时器中播放滴答声。但它在计时器结束后继续播放,即使在我为媒体播放器调用停止之后也是如此。我在计时器的 ontick() 中播放了如下所示的声音。在 onfinish() 中调用了 mp.Stop(),但是它不起作用。

              mp = MediaPlayer.create(getBaseContext(), sound);
        mp.setOnCompletionListener(new OnCompletionListener() {

            public void onCompletion(MediaPlayer mp) {
        });
        mp.setLooping(false);
        // mp.setVolume(0.1, 0.1);
        mp.start();
}

为我的计时器编码。

final class MyCounter extends CountDownTimer {

    public MyCounter(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
    }

    @Override
    public void onFinish() {
        timer.cancel();

        MediaPlayer.OnCompletionListener onCompletion = new MediaPlayer.OnCompletionListener() {

            public void onCompletion(MediaPlayer mp) {
                if (mp.isPlaying()) {
                    mp.release();
                    mp.stop();
                    mp.reset();
                }

            }
        };
        mp.setOnCompletionListener(onCompletion);
        i++;
        submitAnswer();
        final GameEngine ge = GameEngine.getInstance();

        timeer.setText("Timer Completed.");

        if (ge.getCurrentUnAllocatedAmount() > 0 && i <= 8) {
            final Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                public void run() {
                    Intent intent = new Intent(GameActivity.this,
                            ScoreActivity.class);
                    intent.putExtra("level", i);
                    ge.setCurrentLevel(i);
                    startActivity(intent);
                }
            }, 2200);
        } else if (ge.getCurrentUnAllocatedAmount() <= 0 || i > 8) {
            Intent intent = new Intent(GameActivity.this,
                    ScoreActivity.class);
            intent.putExtra("level", i);
            startActivity(intent);
        }

    }

    public void onTick(long millisUntilFinished) {
        remTime = (millisUntilFinished / 1000);
        timeer.setText("Time:" + (millisUntilFinished / 1000) + "");
        mp = MediaPlayer.create(getBaseContext(), R.raw.beep7);
        if (remTime > 0) {
            mp.start();
            mp.release();
        }
        if (remTime < 10) {
            final Handler handler = new Handler();
            handler.post(new Runnable() {
                public void run() {
                    if (timeer.getVisibility() == View.VISIBLE) {
                        timeer.setVisibility(View.INVISIBLE);
                    } else {
                        timeer.setVisibility(View.VISIBLE);
                    }

                    timeer.setTextColor(Color.RED);

                }
            });
        }
    }
}

【问题讨论】:

  • mp.stop() onCompletion of player
  • 我也尝试调用 mp.pause()。它不工作。
  • 尝试在播放器的 onCompletion 上编写 mp.stop() 和 mp.reset()

标签: android media-player


【解决方案1】:

使用此代码:

if(mediaPlayer.isPlaying())
{
    mediaPlayer.stop();
}

【讨论】:

    【解决方案2】:
    mp.setOnCompletionListener(new OnCompletionListener() {
    
            public void onCompletion(MediaPlayer mp) {
    mp.stop();
        });
    

    【讨论】:

    • 让我保证,它每次都会发生,或者当您保持与您调用媒体播放器相同的意图时也会发生这种情况??
    • 计时器结束后,它移动到另一个活动。但仍会继续播放声音。
    • 如果你必须让玩家停止你的计时器,那你为什么要调用 onCompletion 呢?只需在计时器结束时使用 mp.stop()。
    • 这是用户给出的答案..但没有任何效果..甚至 mp.stop()
    • 请调试您的代码并检查 u stop() mp 对象何时不是新实例或 null
    【解决方案3】:

    试试这个:

    mp.setOnCompletionListener(onCompletion);
    
    private MediaPlayer.OnCompletionListener onCompletion = new MediaPlayer.OnCompletionListener() {
    
                    @Override
                    public void onCompletion(MediaPlayer mp) {
                            if(mp.isPlaying())
                            {
                                 mp.stop();
                                 mp.reset();
                            }
    
                    }
            };
    

    【讨论】:

      【解决方案4】:

      我认为您没有在 onCompletionListener() 本身上调用 release() 方法。

      mp.setOnCompletionListener(new OnCompletionListener() {
          @Override 
          public void onCompletion(MediaPlayer mp) {
             mp.release();
          });
      

      【讨论】:

        【解决方案5】:
        mp.setOnCompletionListener(new OnCompletionListener() {
            @Override 
            public void onCompletion(MediaPlayer mp) {
        
               if (mp.isPlaying()) {
                   mp.stop();
                    }
        
                    mp.release();
                    mp = null;
                }
        
        
            });
        

        【讨论】:

        • 最后一行mp = null 没有用,因为它只影响onCompletion 方法内的变量mp。在onCompletion 结束时,它的引用计数无论如何都会减少。
        【解决方案6】:

        检查定时器停止时的条件。

        if(mPlayer.isPlaying())
        {
            mPlayer.stop();
        }
        

        【讨论】:

        • 发帖前先看看其他用户的回答!!
        猜你喜欢
        • 2013-04-06
        • 1970-01-01
        • 1970-01-01
        • 2013-10-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多