0
にAsyncTask上のMediaPlayerを一時停止し、再開私はAsyncTaskを使用してバックグラウンドで遊んでいるのMediaPlayerを使用してmp3の再生を一時停止し、再開しようとしています。のJavaアンドロイド:エラー活動
public class MainActivity extends Activity implements SensorEventListener
{
private MyGLSurfaceView mGLSurfaceView2;
private SensorManager mSensorManager;
private Sensor mAccelerometer;
private Sensor mGyro;
BackgroundSound mBackgroundSound = new BackgroundSound();
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
this.mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
this.mGyro = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
mGLSurfaceView2 = new MyGLSurfaceView(this);
setContentView(mGLSurfaceView2);
mBackgroundSound.execute();
}
@Override
protected void onResume()
{
super.onResume();
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
mSensorManager.registerListener(this, mGyro, SensorManager.SENSOR_DELAY_NORMAL);
if(mBackgroundSound.player != null)
if(mBackgroundSound.player.isPlaying()==false)
mBackgroundSound.player.start();
mGLSurfaceView2.onResume();
}
@Override
protected void onPause()
{
super.onPause();
mSensorManager.unregisterListener(this);
if(mBackgroundSound.player.isPlaying())
{
mBackgroundSound.player.pause();
}
mBackgroundSound.player.release();
// mBackgroundSound.cancel(true);
mGLSurfaceView2.onPause();
}
public class BackgroundSound extends AsyncTask<Void, Void, Void> {
MediaPlayer player;
@Override
protected Void doInBackground(Void... params) {
AssetFileDescriptor afd;
player = new MediaPlayer();
try {
afd = getAssets().openFd("2/sound.mp3");
if (afd != null) {
player.setDataSource(afd.getFileDescriptor());
player.prepare();
player.setLooping(true);
player.setVolume(1.0f, 1.0f);
//player.start();
} else {
Log.i("Error", "afd is null.");
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
}
logcatエラー:
E/MediaPlayer: start called in state 1
E/MediaPlayer: error (-38, 0)
E/MediaPlayer: Error (-38,0)
私が移動した場合mBackgroundSound.execute();
onResume()
にmp3の再生が始まりますが、onResume()
は明らかに(それが実行された二回目を失敗した。ここ
アクティビティコードですアプリを再開するとき)。
私は(APPが再び再開したときにAPPが最小化されたMP3の一時停止をし、再開または再起動)、それを正しく動作させるには、正しい方法で次のコマンドを入れて助けを必要と:
mBackgroundSound.execute();
mBackgroundSound.cancel(true);
mBackgroundSound.player.start();
mBackgroundSound.player.pause();
mBackgroundSound.player.release();
感謝を!