我正在尝试开发一个无线流应用程序使用服务,使它在后台播放。我用过前台、wakelock和wifilock,但我注意到收音机在20-30分钟后停止播放。我使用了电池优化和不同的链接,但问题仍然存在。请看一下我的代码,告诉我我做错了什么。我对此进行了一些研究,但没有找到解决办法。代码是在androidstudio上用java编写的,我已经在galaxys8和nexus5x上测试了这个应用程序。我的代码如下:
`My_code_in_MyService.jave_Section`
MediaPlayer mediaPlayer;
String url = "http://s28.myradiostream.com:11618/listen.mp3";
ConnectionDetector connectionDetector;
int Notification_ID =10;
WifiManager wMgr;
WifiManager.WifiLock wifiLock;
public MyService_8()
{
}
@Override
public void onCreate() {
connectionDetector = new ConnectionDetector(this);
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try
{
mediaPlayer.setDataSource(url);
mediaPlayer.prepareAsync();
mediaPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
}
catch (IOException e)
{
Toast.makeText(MyService_8.this, "Something went wrong, please try again", Toast.LENGTH_SHORT).show();
}
catch (IllegalStateException illegalStateException)
{
Toast.makeText(MyService_8.this, "Something went wrong, please try again", Toast.LENGTH_SHORT).show();
}
catch (IllegalArgumentException illegalArgumentException)
{
Toast.makeText(MyService_8.this, "Something went wrong, please try again", Toast.LENGTH_SHORT).show();
}
catch (SecurityException securityException)
{
Toast.makeText(MyService_8.this, "Something went wrong, please try again", Toast.LENGTH_SHORT).show();
}
catch (Exception e)
{
Toast.makeText(MyService_8.this, "Something went wrong, please try again", Toast.LENGTH_SHORT).show();
}
super.onCreate();
}
@override public int onstartcommand(intent intent,int flags,int startid){//返回super.onstartcommand(intent,flags,startid);
// return super.onStartCommand(intent, flags, startId);
createNotificationChannel();
Intent intent1 = new Intent(this, MyService_8.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent1,0);
Notification notification = new NotificationCompat.Builder(this, "chanellId")
.setContentTitle("FM RADIO APP")
.setContentText("Latest Music and News...")
.setSmallIcon(R.drawable.radioicon)
.setAutoCancel(true)
.setContentIntent(pendingIntent).build();
startForeground(Notification_ID, notification);
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
mp.setLooping(true);
Toast.makeText(MyService_8.this, "Radio now playing", Toast.LENGTH_LONG).show();
wMgr = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
wifiLock = wMgr.createWifiLock(WifiManager.WIFI_MODE_FULL, "MyWifiLock");
wifiLock.acquire();
}
});
return START_STICKY;
}
public void createNotificationChannel()
{
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O )
{
NotificationChannel notificationChannel = new NotificationChannel("chanellId", "Foreground Notification", NotificationManager.IMPORTANCE_DEFAULT);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(notificationChannel);
}
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
stopForeground(true);
stopSelf();
mediaPlayer.stop();
mediaPlayer.release();
wifiLock.release();
super.onDestroy();
}
`Manifest_Section`
`<uses-permission android:name="android.permission.INTERNET"/>`
`<uses-permission android:name="android.permission.WAKE_LOCK"/>`
`<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>`
` <service android:name=".MyService_8"/>`
`MainActivity_Section`
` MyIntent = new Intent(MainActivity.this, MyService_8.class);`
` if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
startForegroundService(MyIntent);
}
else {
startService(MyIntent);
}`
暂无答案!
目前还没有任何答案,快来回答吧!