I am trying to create a radio streaming app, but i noticed that after using service, foreground, wakelockwifilock, it stops playing after 20-30mint. The playback is in service. Can someone help me with an idea.This is code from Service.java
`First_section_of_code` @Override public void onCreate() { mediaPlayer = new MediaPlayer(); mediaPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); try { mediaPlayer.setDataSource(url55); mediaPlayer.prepare(); wifiLock = ((WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE)) .createWifiLock(WifiManager.WIFI_MODE_FULL, "mylock"); wifiLock.acquire(); wifiLock.isHeld(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } super.onCreate(); } `The_end_of_oncreate_method`
@Override public int onStartCommand(Intent intent, int flags, int startId) { //return super.onStartCommand(intent, flags, startId);
createNotificationChannel(); Intent intent1 = new Intent(this, MyService_55.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent1,0); Notification notification = new NotificationCompat.Builder(this, "chanellId") .setContentTitle("NAIJA FM RADIO APP") .setContentText("Latest Nigerian Music and News...") .setSmallIcon(R.drawable.radiokwara) .setAutoCancel(true) .setContentIntent(pendingIntent).build(); startForeground(Notification_ID, notification); mediaPlayer.start(); 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); } } @Override public void onDestroy() { super.onDestroy(); if (mediaPlayer != null) { mediaPlayer.release(); wifiLock.release(); } }
Manifest <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.WAKE_LOCK"/> <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
https://stackoverflow.com/questions/65533187/despite-using-service-foreground-wakelock-and-wifilock-online-streaming-stil January 02, 2021 at 03:55AM
没有评论:
发表评论