我为我的一个项目创建了一个后台流音乐播放服务。它的工作正常但在播放时我接到了电话,媒体并没有像其他应用那样停止播放。当我接听电话时,音乐仍在播放。
package mypackage;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.IBinder;
import android.support.v7.app.NotificationCompat;
import android.widget.Toast;
import mypackage.R;
public class StreamPlayer extends Service implements OnCompletionListener {
MediaPlayer mediaPlayer;
Notification noti;
android.support.v4.app.NotificationCompat.Builder noticom;
NotificationManager notiman;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {
@Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
Toast.makeText(StreamPlayer.this, "Buffering", Toast.LENGTH_LONG).show();
}
});
try {
mediaPlayer.setDataSource("http://149.56.185.83:8138/stream");
mediaPlayer.prepare();
}catch (Exception ex){
}
mediaPlayer.setOnCompletionListener(this);
noticom = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setCategory(MEDIA_SESSION_SERVICE)
.setAutoCancel(true)
.setContentTitle("Sample Streamed Music Player")
.setOngoing(true)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setContentText("Now Playing")
.setCategory(NotificationCompat.CATEGORY_EVENT);
notiman = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (!mediaPlayer.isPlaying()) {
mediaPlayer.start();
Toast.makeText(StreamPlayer.this, "Now Playing", Toast.LENGTH_SHORT).show();
noti = noticom.build();
notiman.notify(2017,noti);
}
return START_STICKY;
}
public void onDestroy() {
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
noticom.setOngoing(false);
noticom.setContentText("Stopped");
Toast.makeText(StreamPlayer.this, "Stopped", Toast.LENGTH_SHORT).show();
noti = noticom.build();
notiman.notify(2017,noti);
}
mediaPlayer.release();
}
public void onCompletion(MediaPlayer _mediaPlayer) {
stopSelf();
}
}
有人知道如何解决这个问题吗?
答案 0 :(得分:0)
首先将其作为 <style>
.box-item {
background-color: cornflowerblue;
width: 100px;
height: 120px;
}
.box-text {
color:white;
}
</style>
<div layout="row" style="padding: 32px;" ng-cloak>
<md-whiteframe class="md-whiteframe-2dp box-item" md-colors="[enter image description here][1]background:'blue-400'}"
flex-sm="45" flex-gt-sm="35" flex-gt-md="25" layout
layout-align="end center" layout-margin>
<span class="md-display-1 box-text">1349</span>
<span class="box-text">New Feedbacks</span>
</md-whiteframe>
</div>
返回
第二次致电Service.START_REDELIVER_INTENT;
这可以是notificationFunction()
startForeground(int id, notificationFunction());
对于音乐播放器,您需要Tensorflow documentation
答案 1 :(得分:0)
我找到了解决方案。 感谢您的支持。 :)
public class MyActivity extends Activity implements OnAudioFocusChangeListener {
private AudioManager mAudioManager;
@Override
public void onCreate(Bundle savedInstanceState) {
...
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
mAudioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
...
}
@Override
public void onDestroy(){
super.onDestroy();
...
mAudioManager.abandonAudioFocus(this);
...
}
@Override
public void onAudioFocusChange(int focusChange) {
if(focusChange<=0) {
//LOSS -> PAUSE
} else {
//GAIN -> PLAY
}
}