关闭屏幕时,使媒体播放器在后台工作

时间:2018-09-24 20:07:08

标签: java android background android-mediaplayer

我正在使用Media Player播放流音频(来自服务器)。当我关闭屏幕并且系统进入睡眠模式时,它可以工作,但是几分钟后电话会停止播放音乐。当手机连接到电源(USB电缆)时,不会发生这种情况。因此,由于电源管理或内存管理,系统必须使应用程序崩溃。

服务类别:

public class MyService extends Service {

    PowerManager powerManager;
    PowerManager.WakeLock wakeLock;
    WifiManager.WifiLock wifiLock;

    private MediaPlayer mediaPlayer;
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
       return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
        wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Lock");
        wifiLock = ((WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE)).createWifiLock(WifiManager.WIFI_MODE_FULL, "mylock");

    wakeLock.acquire();
    wifiLock.acquire();

        mediaPlayer = new MediaPlayer();
 mediaPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);

        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

        try {
            mediaPlayer.setDataSource("http://mediacontrol.jargon.com.ar:8168/;");
        } catch (IOException e) {
            e.printStackTrace();
        }


        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mediaPlayer.start();
            }
        });
        mediaPlayer.prepareAsync();
        return START_REDELIVER_INTENT;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mediaPlayer.stop();
        mediaPlayer.release();
        wakeLock.release();
        wifiLock.release();
    }
}

这是实现服务的类:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    setRetainInstance(true); // con esto retenemos los valores pero se elimina el view
    View view = inflater.inflate(R.layout.fragment_blank_fragment4, container, false);

    play = (ImageButton) view.findViewById(R.id.imageButton);
    imagen=(ImageView) view.findViewById((R.id.imageView));

   if(comenzar) {
       play.setImageResource(R.drawable.play);

           }
   else {
      play.setImageResource(R.drawable.stop);
    }
       play.setOnClickListener(new View.OnClickListener() {
           public void onClick(View v) {
               if (comenzar) {
                  if(isConnectedMobile(getActivity())|| 
isConnectedWifi(getActivity())) {
                       play.setImageResource(R.drawable.stop);

getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
                   getActivity().startService(new Intent(getActivity(), MyService.class));
                   showNotification();
                   comenzar = false;
                   release=true;
               }
               else
               {
                   AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                   builder.setTitle("No hay conexión a internet");
                   builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {
                           dialog.cancel();
                       }
                   });

                   AlertDialog dialog = builder.create();
                   dialog.show();
               }
            } else {
                play.setImageResource(R.drawable.play);

                getActivity().stopService(new Intent(getActivity(), MyService.class));

                comenzar = true;
                NotificationManager mNotificationManager = (NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE);

                //getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);                
                release=false;
    }
        }

    });
    return view;
}

我也在清单中设置了此权限

 <uses-permission android:name="android.permission.WAKE_LOCK" />
 <uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />

我不能很好地调试它,因为问题是在未连接手机但音频崩溃时发生的,然后我连接了它,当我打开屏幕并停止后,它又运行了一秒钟。

我明白了:

09-24 18:14:21.480 8539-8539/jaodev.utnfrp W/MediaPlayer: mediaplayer went away with unhandled events
09-24 18:14:24.070 8539-8551/jaodev.utnfrp I/MediaHTTPConnection: proxyName: 0.0.0.0 0
09-24 18:14:24.970 8539-8539/jaodev.utnfrp D/MediaPlayer: setSubtitleAnchor in MediaPlayer
09-24 18:17:57.413 8539-8552/jaodev.utnfrp I/MediaHTTPConnection: proxyName: 0.0.0.0 0
09-24 18:18:30.437 8539-8551/jaodev.utnfrp I/MediaHTTPConnection: proxyName: 0.0.0.0 0
09-24 18:18:34.187 8539-9040/jaodev.utnfrp W/MediaPlayer: info/warning (703, 0)
09-24 18:18:34.188 8539-9040/jaodev.utnfrp W/MediaPlayer: info/warning (701, 0)
09-24 18:19:03.462 8539-9040/jaodev.utnfrp I/MediaHTTPConnection: proxyName: 0.0.0.0 0
09-24 18:19:03.941 8539-9040/jaodev.utnfrp W/MediaHTTPConnection: readAt 1507328 / 32768 => java.net.ProtocolException
09-24 18:19:04.202 8539-8551/jaodev.utnfrp W/MediaPlayer: info/warning (703, 0)
09-24 18:21:51.201 8539-8544/jaodev.utnfrp I/art: Do partial code cache collection, code=40KB, data=62KB
09-24 18:21:51.203 8539-8544/jaodev.utnfrp I/art: After code cache collection, code=40KB, data=62KB
    Increasing code cache capacity to 256KB

0 个答案:

没有答案