0

I have posted this question before and no one could answer it so I am trying again as this issue makes my app worthless. I need the sound to keep playing when the screen times out or the user taps the power button. I have read almost every online post about wake locks that I can find and i can not get it to work. below is one of the .Java files that plays a sound based on the user selected input. Everything works great except that when the screen goes dark the sound stops playing. Just a note, I am very new to this so if this code is sloppy or redundant please let me know.

package com.androidsleepmachine.gamble;

import android.app.Activity;
import android.content.Context;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.PowerManager;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

public class Ship extends Activity implements View.OnClickListener {
public static final Integer[] TIME_IN_MINUTES = { 30, 45, 60, 180, 360 };
public MediaPlayer mediaPlayer;
public Handler handler = new Handler();
public Button button2;
public Spinner spinner2;
public PowerManager.WakeLock wl;


// Initialize the activity
@Override
public void onCreate(Bundle savedInstanceState) {
    PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
    PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, 
"Playwhenoff");

    super.onCreate(savedInstanceState);
     wl.acquire();
    setContentView(R.layout.ship);
    button2 = (Button) findViewById(R.id.btn2);
    button2.setOnClickListener(this);
    spinner2 = (Spinner) findViewById(R.id.spinner2);
    ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(this,
            android.R.layout.simple_spinner_item, TIME_IN_MINUTES);

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner2.setAdapter(adapter);

}

// Play the sound and start the timer
private void playSound(int resourceId) {
    // Cleanup any previous sound files
    cleanup();
    // Create a new media player instance and start it

    mediaPlayer = MediaPlayer.create(this, resourceId);
    mediaPlayer.start();
    // Create the timer to stop the sound after x number of milliseconds
    int selectedTime = TIME_IN_MINUTES[spinner2.getSelectedItemPosition()];
    handler.postDelayed(runnable, selectedTime * 60 * 1000);
}

// Handle button callback
@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.btn2:
        playSound(R.raw.ocean_ship);
        break;
    }
}

protected void onStop() {

    cleanup();
    super.onStop();
}

// Stop the sound and cleanup the media player
public void cleanup() {
    if (mediaPlayer != null) {
        mediaPlayer.stop();
        mediaPlayer.release();
        mediaPlayer = null;
        wl.release();
    }
    // Cancel any previously running tasks
    handler.removeCallbacks(runnable);
}

// Runnable task used by the handler to stop the sound
public Runnable runnable = new Runnable() {
    public void run() {
        cleanup();
    }
};


@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    wl.release();
}

}
4

2 回答 2

1

这可能与WakeLock. onStop()当屏幕关闭时,您的活动可能会被调用。

音频播放器通常使用音频播放服务,因此播放可以独立于 UI 问题运行。

于 2013-10-02T21:19:20.797 回答
0

您正在释放您的 wakeLock onPause ->

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    wl.release(); // -> This is the line where you release the wakelock
}

当您的设备“进入睡眠状态”时,它就像 onPause -> onStop。

如果您在暂停时释放唤醒锁,您将无法在屏幕关闭的情况下播放音乐,因为系统不会让您的 CPU 保持唤醒状态。

将它释放到其他地方(也许是 onDestroy?)它应该可以工作。

于 2013-10-02T21:40:33.233 回答