コード例 #1
0
    @Override
    public void onAudioFocusChange(int i) {

      if (musicFocusable == null) {
        return;
      }

      switch (i) {
        case AudioManager.AUDIOFOCUS_GAIN:
          Logger.debug(TAG, ">>>" + "onAudioFocusChange AUDIOFOCUS_GAIN");
          musicFocusable.onGainedAudioFocus();
          break;

        case AudioManager.AUDIOFOCUS_LOSS:
        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
          Logger.debug(TAG, ">>>" + "onAudioFocusChange AUDIOFOCUS_LOSS");
          musicFocusable.onLostAudioFocus(false);
          break;
        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
          Logger.debug(TAG, ">>>" + "onAudioFocusChange AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK");
          musicFocusable.onLostAudioFocus(true);
          break;
        default:
      }
    }
コード例 #2
0
  @Override
  public void onReceive(Context context, Intent intent) {
    Logger.debug(TAG, ">>>" + "onReceive>>>>>>>>>>>>");

    // get data from daily hello chao
    //        AppAPI.getInstance().getHcDaily();
  }
コード例 #3
0
  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    String action = intent.getAction();
    Logger.debug(TAG, ">>>" + "onStartCommand action:" + action);

    if (ACTION_TOGGLE_PLAYBACK.equals(action)) {

    } else if (ACTION_PAUSE.equals(action)) {

    } else if (ACTION_PLAY.equals(action)) {

    } else if (ACTION_REWIND.equals(action)) {

    } else if (ACTION_SKIP.equals(action)) {

    } else if (ACTION_STOP.equals(action)) {

    } else if (ACTION_URL.equals(action)) {
      //

    }

    return START_STICKY; // we started the service, but we don't want it restart if it was killed by
                         // system,
  }
コード例 #4
0
  public void playNextSong(MusicItem musicItem) {
    String url = musicItem.audio;
    Logger.debug(TAG, ">>>" + "playNextSong:" + url);
    if (musicPlayBackCallback != null) {
      musicPlayBackCallback.onPreparing(currentItem);
    }
    mState = State.Stopped;

    relaxResource(false);
    try {
      setupMediaPlayerIfNeeded();

      mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
      mediaPlayer.setDataSource(url);
      isStreaming = url.startsWith("http:") || url.startsWith("https:");

      songTitle = url;

      mState = State.Preparing;
      setupAsForeGround(musicItem);

      /** @see onPreparedListener */
      mediaPlayer.prepareAsync();

      if (isStreaming) {
        wifiLock.acquire();
      } else if (wifiLock.isHeld()) {
        wifiLock.release();
      }

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
コード例 #5
0
 private void tryToGetAudioFocus() {
   Logger.debug(TAG, ">>>" + "tryToGetAudioFocus");
   if (audioFocus != AudioFocus.Focused
       && audioFocusHelper != null
       && audioFocusHelper.requestFocus()) {
     audioFocus = AudioFocus.Focused;
   }
 }
コード例 #6
0
 public void processPauseRequest() {
   Logger.debug(TAG, ">>>" + "processPauseRequest");
   if (mState == State.Playing) {
     mState = State.Paused;
     mediaPlayer.pause();
     relaxResource(false);
   }
 }
コード例 #7
0
  public void processAddRequest(MusicItem musicItem) {
    Logger.debug(TAG, ">>>" + "processAddRequest");
    listMusic.clear();
    currentPos = 0;
    this.currentItem = musicItem;

    tryToGetAudioFocus();
    playNextSong(musicItem);
  }
コード例 #8
0
 @Override
 public void onPrepared(MediaPlayer mediaPlayer) {
   Logger.debug(TAG, ">>>" + "onPrepared");
   if (musicPlayBackCallback != null) {
     musicPlayBackCallback.onPlaying(currentItem);
   }
   mState = State.Playing;
   updateNotification(currentItem);
   configAndStartMediaPlayer();
 }
コード例 #9
0
 public void processPlayRequest() {
   Logger.debug(TAG, ">>>" + "processPlayRequest");
   tryToGetAudioFocus();
   if (mState == State.Stopped) {
     if (!listMusic.isEmpty() && currentItem != null) {
       currentItem = listMusic.get(currentPos);
       playNextSong(currentItem);
     }
   } else if (mState == State.Paused) {
     mState = State.Playing;
     setupAsForeGround(currentItem);
     configAndStartMediaPlayer();
   }
 }
コード例 #10
0
  @Override
  public void onCreate() {
    super.onCreate();
    Logger.debug(TAG, ">>>" + "onCreate");
    audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
    notificationManagerCompat = NotificationManagerCompat.from(getApplicationContext());

    audioFocusHelper = new AudioFocusHelper(getApplicationContext(), musicFocusable);
    componentName = new ComponentName(this, MusicIntentReceiver.class);

    wifiLock =
        ((WifiManager) getSystemService(WIFI_SERVICE))
            .createWifiLock(WifiManager.WIFI_MODE_FULL, "myLock");
  }
コード例 #11
0
  private void setupMediaPlayerIfNeeded() {
    Logger.debug(TAG, ">>>" + "setupMediaPlayerIfNeeded");
    if (mediaPlayer == null) {
      mediaPlayer = new MediaPlayer();
      mediaPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);

      mediaPlayer.setOnPreparedListener(onPreparedListener);
      mediaPlayer.setOnCompletionListener(onCompletionListener);
      mediaPlayer.setOnErrorListener(onErrorListener);

    } else {
      mediaPlayer.reset();
    }
  }
コード例 #12
0
  @Override
  protected void initLayout(View view) {

    getAActivity().setSupportActionBar(toolbar);
    getAActivity().getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getAActivity().getSupportActionBar().setTitle("Practice");
    collapsingToolbarLayout.setTitle("My Practice");
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
    linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);

    recyclerView.setLayoutManager(linearLayoutManager);
    recyclerView.setHasFixedSize(true);
    list.clear();
    list.addAll(ResourceManager.getInstance().getListHelloChaoDaily());
    Logger.debug(TAG, ">>>" + "size:" + list.size());
    adapterFull = new AdapterFull(getContext(), list);
    recyclerView.setAdapter(adapterFull);
  }
コード例 #13
0
 private void updateNotification(MusicItem musicItem) {
   Logger.debug(TAG, ">>>" + "updateNotification:" + musicItem);
   if (musicItem == null) {
     return;
   }
   Intent intent = new Intent(getApplicationContext(), ScrollingActivity.class);
   intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
   PendingIntent pi =
       PendingIntent.getActivity(
           getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
   Notification builder =
       new NotificationCompat.Builder(getApplicationContext())
           .setContentText(musicItem.text)
           .setOngoing(false)
           .setContentIntent(pi)
           .setTicker(musicItem.text)
           .setContentTitle("Daily Practice!")
           .setSmallIcon(R.mipmap.ic_launcher)
           .build();
   notificationManagerCompat.notify(12, builder);
 }
コード例 #14
0
 @Override
 public IBinder onBind(Intent intent) {
   Logger.debug(TAG, ">>>" + "onBind");
   return localBinder;
 }
コード例 #15
0
 public MusicService() {
   Logger.debug(TAG, ">>>" + "MusicService");
 }