gpt4 book ai didi

android - Mediaplayer服务无响应

转载 作者:行者123 更新时间:2023-12-03 02:16:32 25 4
gpt4 key购买 nike

该 Activity 正在调用该服务,但该服务无法启动。它应该提示用户该时间,然后播放该时间。我在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();
}

}
}

最佳答案

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

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

如果要使用服务播放媒体,请在服务类的onStartCommand()方法中编写以上代码。注意-服务播放的媒体将继续播放,即使您的 Activity 处于后台,直到系统将其杀死为止。有关如何使用服务的信息,请参见 guide。并考虑一下 guide,如何使用mediaplayer。

关于android - Mediaplayer服务无响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19409781/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com