我正在尝试通过使用服务的媒体播放器播放mp3。当我使用Error (-38,0)and Media player receives message, message type: 100.
设置媒体播放器的URL时,媒体播放器会继续进行准备,并且不会调用“暂停媒体播放器”方法。出现媒体播放器错误:
public class MyPlayService extends Service implements MediaPlayer.OnCompletionListener,
MediaPlayer.OnPreparedListener, MediaPlayer.OnErrorListener, MediaPlayer.OnSeekCompleteListener,
MediaPlayer.OnInfoListener, MediaPlayer.OnBufferingUpdateListener {
MediaPlayer mediaPlayer=new MediaPlayer();
private String sntAudioLink;
private static final int NOTIFICATION_ID=1;
private boolean ispausedincall=false;
private PhoneStateListener phoneStateListener;
private TelephonyManager telephonyManager;
static final String BROADCAST_BUFFER="broadcastbufferheadphones";
Intent bufferintent;
private int headsetswitch=1;
boolean headsetconnected;
Intent seekintent;
String sntseekposstr;
int seekpos;
int mediapos;
int mediamaxlen;
private Handler handler=new Handler();
private static int songend;
public static final String BROADCAST_ACTION="seekbarupdate";
@Override
public void onCreate() {
bufferintent=new Intent(BROADCAST_BUFFER);
seekintent=new Intent(BROADCAST_ACTION);
mediaPlayer.setOnCompletionListener(this);
mediaPlayer.setOnErrorListener(this);
mediaPlayer.setOnBufferingUpdateListener(this);
mediaPlayer.setOnSeekCompleteListener(this);
mediaPlayer.setOnInfoListener(this);
mediaPlayer.reset();
registerReceiver(headsetrecevier,new IntentFilter(Intent.ACTION_HEADSET_PLUG));
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
public int onStartCommand(Intent intent, int flags, int startId) {
//seek bar change manually
registerReceiver(broadcastReceiver_seekchange,new IntentFilter(Play_screen_with_service.BROADCAST_SEEKBAR));
//notification
initnotification();
sntAudioLink = intent.getExtras().getString("sendAudioLink");
//mediaPlayer.reset();
if(!mediaPlayer.isPlaying()) {
try {
Log.d("asdfas","absdb url "+sntAudioLink);
sendbufferingbroadcast();
Uri myUri = Uri.parse(sntAudioLink);
Log.d("myUride","media player myUri "+myUri);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(this,myUri);
Log.d("asdvasdvas","media player syc "+mediaPlayer);
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
Log.d("medialoko","media playyer prepared");
sendbuffercompletebroadcast();
playMedia();
}
});
} catch (IllegalArgumentException var5) {
var5.printStackTrace();
} catch (IllegalStateException var6) {
var6.printStackTrace();
} catch (IOException var7) {
var7.printStackTrace();
}
}
setuphandler();
return START_STICKY;
}
private void setuphandler() {
handler.removeCallbacks(sendupdateUl);
handler.postDelayed(sendupdateUl,1000);
}
private Runnable sendupdateUl=new Runnable() {
@Override
public void run() {
Longmediaposition();
handler.postDelayed(this,1000);
}
};
private void Longmediaposition() {
if (mediaPlayer.isPlaying()){
mediapos=mediaPlayer.getCurrentPosition();
mediamaxlen=mediaPlayer.getDuration();
seekintent.putExtra("counter",String.valueOf(mediapos));
seekintent.putExtra("mediamax",String.valueOf(mediamaxlen));
seekintent.putExtra("songended",String.valueOf(songend));
sendBroadcast(seekintent);
}
}
private BroadcastReceiver broadcastReceiver_seekchange=new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
updateseekpos(intent);
}
};
private void updateseekpos(Intent intent) {
int seekposmanul=intent.getIntExtra("seekpos",0);
if (mediaPlayer.isPlaying()){
handler.removeCallbacks(sendupdateUl);
mediaPlayer.seekTo(seekposmanul);
setuphandler();
}
}
private void sendbufferingbroadcast() {
Log.d("asbabskd","buffering started called ");
bufferintent.putExtra("bufferring","1");
sendBroadcast(bufferintent);
}
private void sendbuffercompletebroadcast(){
Log.d("asbabskd","buffering completed called ");
bufferintent.putExtra("bufferring","0");
sendBroadcast(bufferintent);
}
private BroadcastReceiver headsetrecevier=new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.hasExtra("state")){
if (headsetconnected && intent.getIntExtra("state",0)==0){
headsetconnected=false;
headsetswitch=0;
}else if (!headsetconnected && intent.getIntExtra("state",1)==1 ){
headsetconnected=true;
headsetswitch=1;
}
}
switch (headsetswitch){
case 0:
stopMedia();
stopSelf();
break;
case 1:
break;
}
}
};
private void reset_headsetbroadcast_playpause(){
Log.d("headsetbroadcast","headsetbroadcast called ");
bufferintent.putExtra("buffering","2");
sendBroadcast(bufferintent);
}
private void pausemedia() {
if (mediaPlayer.isPlaying()){
mediaPlayer.pause();
}
}
public void onDestroy() {
super.onDestroy();
if (mediaPlayer != null){
if (mediaPlayer.isPlaying()){
mediaPlayer.stop();
}
mediaPlayer.release();
}
if (phoneStateListener != null){
telephonyManager.listen(phoneStateListener,PhoneStateListener.LISTEN_NONE);
}
cancelnotification();
unregisterReceiver(headsetrecevier);
unregisterReceiver(broadcastReceiver_seekchange);
reset_headsetbroadcast_playpause();
handler.removeCallbacks(sendupdateUl);
}
private void initnotification() {
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager)this.getSystemService(ns);
int icon = R.drawable.playbuttonimage;
CharSequence tickerText = "Audio play at back ground";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
notification.flags = Notification.FLAG_ONGOING_EVENT;
Context context = getApplicationContext();
CharSequence contentTitle = "Music In Service App Tutorial";
CharSequence contentText = "Listen To Music While Performing Other Tasks";
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
Notification.Builder builder = new Notification.Builder(context)
.setContentIntent(contentIntent)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(contentTitle);
notification = builder.build();
mNotificationManager.notify(NOTIFICATION_ID, notification);
}
private void cancelnotification() {
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager)this.getSystemService(ns);
mNotificationManager.cancel(NOTIFICATION_ID);
}
@Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
Log.d("baksbvk","buffering pdate listner "+mp +" percant "+percent);
}
@Override
public void onCompletion(MediaPlayer mp) {
stopMedia();
stopSelf();
}
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
switch(what) {
case 1:
Toast.makeText(this, "MEDIA ERROR UNKNOWN " + extra, Toast.LENGTH_SHORT).show();
break;
case 100:
Toast.makeText(this, "MEDIA ERROR SERVER DIED " + extra, Toast.LENGTH_SHORT).show();
break;
case 200:
Toast.makeText(this, "MEDIA ERROR NOT VALID FOR PROGRESSIVE PLAYBACK " + extra, Toast.LENGTH_SHORT).show();
}
return false;
}
@Override
public boolean onInfo(MediaPlayer mp, int what, int extra) {
return false;
}
private void playMedia() {
if (!mediaPlayer.isPlaying()){
mediaPlayer.start();
}
}
private void stopMedia() {
if (mediaPlayer.isPlaying()){
mediaPlayer.stop();
}
}
@Override
public void onSeekComplete(MediaPlayer mp) {
if (!mediaPlayer.isPlaying()){
playMedia();
}
}
}
我找不到错误。这是我设置和准备的媒体播放器的媒体播放器服务代码:
{{1}}