媒体播放器服务无响应

时间:2013-10-16 17:22:46

标签: android audio service

该活动正在调用该服务,但它无法启动。它应该提示用户时间,然后玩这段时间。我在logcat中没有任何错误。

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, "Howaya");

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


// 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:
    startService(new Intent(this, Shipservice.class));
    break;
}
}

protected void onDestroy() {

cleanup();
super.onDestroy();
}

// Stop the sound and cleanup the media player
public void cleanup() {
if (mediaPlayer != null) {
    mediaPlayer.stop();
    mediaPlayer.release();
    mediaPlayer = null;

}
// 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();
}

}

这是应该播放的服务类。

public class Shipservice extends Service  { 
static final String TAG=Shipservice.class.getSimpleName();
MediaPlayer player;
@Override
public IBinder onBind(Intent intent) {
return null;
// TODO Auto-generated method stub

}

@Override
public void onCreate() {
// TODO Auto-generated method stub
player = MediaPlayer.create(this, R.raw.ocean_ship);
player.setLooping(false);
Log.d(TAG, "onCreate'd");
}

@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.d(TAG, "onDestroy'd");
}

@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
Log.d(TAG, "onStart'd");
}
class ship extends Thread{

@Override
public void run() {
// TODO Auto-generated method stub
super.run();
}

}
}

1 个答案:

答案 0 :(得分:0)

您没有编写播放媒体文件的代码。

MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(//path to file);
mediaPlayer.prepare();
mediaPlayer.start();

如果要使用服务播放媒体,请在服务类的onStartCommand()方法中编写上述代码。注意 - 即使您的活动处于后台,直到系统杀死它,服务播放的媒体仍将继续播放。有关如何使用服务,请参阅此guide。并考虑这个guide,如何使用媒体播放器。