MediaPlayerService.java 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * Copyright (C) 2015 Bilibili
  3. * Copyright (C) 2015 Zhang Rui <bbcallen@gmail.com>
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package com.edufound.mobile.ijkplayer.services;
  18. import android.app.Service;
  19. import android.content.Context;
  20. import android.content.Intent;
  21. import android.os.IBinder;
  22. import android.support.annotation.Nullable;
  23. import tv.danmaku.ijk.media.player.IMediaPlayer;
  24. public class MediaPlayerService extends Service {
  25. private static IMediaPlayer sMediaPlayer;
  26. public static Intent newIntent(Context context) {
  27. Intent intent = new Intent(context, MediaPlayerService.class);
  28. return intent;
  29. }
  30. public static void intentToStart(Context context) {
  31. context.startService(newIntent(context));
  32. }
  33. public static void intentToStop(Context context) {
  34. context.stopService(newIntent(context));
  35. }
  36. @Nullable
  37. @Override
  38. public IBinder onBind(Intent intent) {
  39. return null;
  40. }
  41. public static void setMediaPlayer(IMediaPlayer mp) {
  42. if (sMediaPlayer != null && sMediaPlayer != mp) {
  43. if (sMediaPlayer.isPlaying())
  44. sMediaPlayer.stop();
  45. sMediaPlayer.release();
  46. sMediaPlayer = null;
  47. }
  48. sMediaPlayer = mp;
  49. }
  50. public static IMediaPlayer getMediaPlayer() {
  51. return sMediaPlayer;
  52. }
  53. }