Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/223.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Mediaplayer服务没有响应_Android_Audio_Service - Fatal编程技术网

Android Mediaplayer服务没有响应

Android Mediaplayer服务没有响应,android,audio,service,Android,Audio,Service,该活动正在调用该服务,但未启动。它应该提示用户输入时间,然后播放该时间。我在日志中没有任何错误 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 = ne

该活动正在调用该服务,但未启动。它应该提示用户输入时间,然后播放该时间。我在日志中没有任何错误

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

}

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

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

如果要使用服务播放媒体,请在服务类的onStartCommand()方法中编写上述代码。注意-即使您的活动处于后台,服务播放的媒体仍将继续播放,直到系统终止。有关如何使用服务,请参见此。考虑一下这一点,如何使用MealApple。

我刚刚看到你的回复,很抱歉没有及时回复。我使用了您的代码,并在.setDataSource路径中放置了R.id.sound,它在setDataSource下有一条红线,告诉我将文件设置为字符串,但这样做会告诉我将其更改回int。告诉我您的声音文件存储在哪里。我建议您将声音文件存储在原始文件夹中。请尝试R.raw.sound_file_name注意:文件名应仅为小写字母。它存储在原始文件夹中。声音名为ocean_ship,因此在我输入的文件路径(R.raw.ocean_ship)中,它在setDataSource下面加下划线,并告诉我将其设置为字符串。当我这样做时,它会告诉我将其更改回int.MediaPlayer=MediaPlayer.create(这个,R.raw.ocean_ship);mediaPlayer.start();如果声音文件存储在原始文件夹中,请使用此过程。我在答案中发布的上述过程是播放声音文件,其路径以Sting或URI的形式可用
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(//path to file);
mediaPlayer.prepare();
mediaPlayer.start();