1

我有一个看起来像的活动,

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,但这几乎只是检测到一个抖动事件,在该事件上循环播放一个样本声音(是的,这很烦人!)直到下一次抖动。

这实际上工作得很好,除了我得到带有标签 MediaPlayer 的 LogCat 条目,start() mUri is null或者stop() mUri is null当启动和停止命令被执行时。这让我相信我做错了什么,但是当我用谷歌搜索一下时,我无法找出它是什么,这显然也不是一个流行的错误。

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

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

谢谢!-乔里斯。

4

1 回答 1

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;
              }

        }


    }

}
于 2013-05-01T08:18:23.083 回答