MediaPlayer start()/ stop()mUri为null

时间:2013-04-30 22:12:51

标签: java android uri android-mediaplayer

我的活动看起来像,

public class TestActivity extends Activity {

    private SensorManager      mSensorManager;
        private ShakeEventListener mSensorListener;
    private MediaPlayer        mPlayer;

    private boolean            mIsPlaying; 

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE); 

        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.testsound);

        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        mSensorListener = new ShakeEventListener();   
        mSensorListener.setOnShakeListener(new ShakeEventListener.OnShakeListener() {

          @Override 
          public void onShake() {
            toggleSound();
          }
        });  
    }

    @Override
    protected void onResume() {
      super.onResume();
      mSensorManager.registerListener(mSensorListener,
          mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
          SensorManager.SENSOR_DELAY_UI);
    }

    @Override
    protected void onPause() {
      super.onPause();
      mSensorManager.unregisterListener(mSensorListener);
      stopSound();
    }    

    protected void toggleSound() {
        if (!mIsPlaying) {
          startSound();
        } else {
          stopSound();  
        }
    }


    protected void startSound() {

        if (mPlayer == null)
          {
            mPlayer = MediaPlayer.create(TestActivity.this, R.raw.test);
            mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); 

            mPlayer.setLooping(true);
            mPlayer.start();  

            mPlayer.setOnCompletionListener(new OnCompletionListener() {

                 @Override
                public void onCompletion(MediaPlayer mp) {
                  stopSound();
                }
            }); 

            mIsPlaying = true;
          }
    }


    protected void stopSound() {
        if (mPlayer != null) {
              if (mPlayer.isPlaying())
              {
                  mPlayer.stop();
              }
              mPlayer.release();
              mPlayer = null;
        }

        mIsPlaying = false;
    }

}

我知道这可能不是最好的方法,但我刚刚开始进行一些随机测试。我没有包含ShakeEventListener,但是这几乎只是检测到一个摇动事件,在循环中播放一个样本声音(是的,它非常烦人!),直到下一次震动。

实际上这确实很有效,除了我在执行start和stop命令时获得带有标签MediaPlayer的LogCat条目,表示start() mUri is nullstop() mUri is null。这让我相信我做错了什么,但我不知道它是什么,当我稍微谷歌时,这也不是一个普遍的错误。

有人会知道这意味着什么吗?对不起,如果我很厚 - 这对我来说仍然有点混乱。

UPDATE(来自评论):我做的一件事就是给它一个实际的Uri(从路径构建),然后它会出现类似的警告,现在说start() mUri is android.resource://com.joris.soundtest/2130968576(D级),此时此前是E级Uri is android.resource//...(点给出相同的uri)。这些LogCat条目是否可能只是“提供信息”,用于调试? E级别听起来不会像......

谢谢! -Joris。

1 个答案:

答案 0 :(得分:0)

更改以下内容,因为mediaplayer.release()方法不会将mediaplayer对象设为null ...

package com.pranav.listviewdemo;

import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

public class MainActivity extends Activity {

    private SensorManager      mSensorManager;
    private ShakeEventListener mSensorListener;
    private MediaPlayer        mPlayer;
    static int i=1;

    private boolean            mIsPlaying=false; 

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE); 

        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_main);

        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        mSensorListener = new ShakeEventListener(this,null);   
        mPlayer = MediaPlayer.create(this, R.raw.test);
        mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); 

        mPlayer.setLooping(true);

        mPlayer.setOnCompletionListener(new OnCompletionListener() {

             @Override
            public void onCompletion(MediaPlayer mp) {
              stopSound();
            }
        }); 
        mSensorListener.setOnShakeListener(new ShakeEventListener.OnShakeListener() {

          @Override 
          public void onShake() {
            toggleSound();
            Toast.makeText(getBaseContext(), "DETECTED"+i, Toast.LENGTH_SHORT).show();
            i++;
          }
        });  
    }

    @Override
    protected void onResume() {
      super.onResume();
      mSensorManager.registerListener(mSensorListener,
          mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
          SensorManager.SENSOR_DELAY_UI);
    }

    @Override
    protected void onPause() {
      super.onPause();
      mSensorManager.unregisterListener(mSensorListener);
      stopSound();
    }    

    protected void toggleSound() {
        if (!mIsPlaying) {
          startSound();
        } else {
          stopSound();  
        }
    }


    protected void startSound() {

        if (mPlayer != null)
          {
            mPlayer.seekTo(0);
            mPlayer.start();  
            mIsPlaying = true;
          }
    }


    protected void stopSound() {
        if (mPlayer != null) {
              if (mPlayer.isPlaying())
              {
                  mPlayer.pause();
                  mIsPlaying = false;
              }

        }


    }

}