Пример #1
0
 private void playSong() {
   try {
     mMediaPlayer.seekTo(0);
     mMediaPlayer.start();
   } catch (IllegalStateException e) {
     e.printStackTrace();
   } catch (IllegalArgumentException e) {
     e.printStackTrace();
   }
 }
Пример #2
0
 private void setupSong() {
   try {
     mWasPaused = false;
     mMediaPlayer.reset();
     mMediaPlayer.setDataSource(mSelectedDataSource);
     mMediaPlayer.prepare();
   } catch (IllegalStateException e) {
     e.printStackTrace();
   } catch (IllegalArgumentException e) {
     e.printStackTrace();
   } catch (IOException e) {
     e.printStackTrace();
   }
 }
Пример #3
0
 public boolean onKeyDown(int keyCode, KeyEvent event) {
   switch (keyCode) {
     case KeyEvent.KEYCODE_VOLUME_UP:
       volume += 0.1F;
       if (volume > 1) volume = 1;
       mMediaPlayer.setVolume(volume, volume);
       return true;
     case KeyEvent.KEYCODE_VOLUME_DOWN:
       volume -= 0.1F;
       if (volume < 0) volume = 0;
       mMediaPlayer.setVolume(volume, volume);
       return true;
   }
   return false;
 }
Пример #4
0
  public void init() {
    mMediaPlayer.setAllJoynAudioServiceListener(
        new AllJoynAudioServiceListener() {
          @Override
          public void SinkFound(
              final String speakerName,
              final String speakerPath,
              final String friendlyName,
              final short speakerPort) {
            mActivity.runOnUiThread(
                new Runnable() {
                  public void run() {
                    mSinkSelectDialog.AddSink(speakerName, speakerPath, friendlyName, speakerPort);
                  }
                });
          }

          @Override
          public void SinkLost(final String speakerName) {
            mActivity.runOnUiThread(
                new Runnable() {
                  public void run() {
                    mSinkSelectDialog.RemoveSink(speakerName);
                  }
                });
          }

          @Override
          public void SinkReady(String speakerName) {
            // Sink that we connected to is now ready lets call play
            boolean isPlaying = mMediaPlayer.isPlaying();
            if (!isPlaying) setupSong();
            playSong();
          }

          @Override
          public void SinkRemoved(final String speakerName, boolean lost) {
            if (lost) {
              mActivity.runOnUiThread(
                  new Runnable() {
                    public void run() {
                      mSinkSelectDialog.RemoveSink(speakerName);
                    }
                  });
            }
          }

          @Override
          public void SinkError(String speakerName) {
            // Publish error message
          }
        });

    mMediaPlayer.setVolume(volume, volume);

    ListView songListView = (ListView) mActivity.findViewById(R.id.songList);
    songListView.setAdapter(
        new ArrayAdapter<String>(mActivity, R.layout.songitem, buildSongList()));
    songListView.setOnItemClickListener(
        new OnItemClickListener() {
          @Override
          public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if (mPrevSelectedSong != null) mPrevSelectedSong.setBackgroundColor(Color.TRANSPARENT);
            view.setSelected(true);
            view.setBackgroundColor(Color.parseColor("#99555555"));
            mPrevSelectedSong = view;
            mSelectedDataSource = mSongFileList.get(position);
            boolean isPlaying = mMediaPlayer.isPlaying();
            setupSong();
            if (isPlaying) playSong();
          }
        });

    mPlayButton = (Button) mActivity.findViewById(R.id.playButton);
    mPlayButton.setOnClickListener(
        new OnClickListener() {
          @Override
          public void onClick(View arg0) {
            if (!mWasPaused) setupSong();
            playSong();
          }
        });

    mPauseButton = (Button) mActivity.findViewById(R.id.pauseButton);
    mPauseButton.setOnClickListener(
        new OnClickListener() {
          @Override
          public void onClick(View arg0) {
            try {
              mMediaPlayer.pause();
              mWasPaused = true;
            } catch (Exception e) {
              e.printStackTrace();
            }
          }
        });

    mStopButton = (Button) mActivity.findViewById(R.id.stopButton);
    mStopButton.setOnClickListener(
        new OnClickListener() {
          @Override
          public void onClick(View arg0) {
            try {
              mMediaPlayer.stop();
              mWasPaused = false;
            } catch (Exception e) {
              e.printStackTrace();
            }
          }
        });

    mSelectSinkButton = (Button) mActivity.findViewById(R.id.pickSink);
    mSelectSinkButton.setOnClickListener(
        new OnClickListener() {
          @Override
          public void onClick(View arg0) {
            showSinkSelectDiaglog();
          }
        });

    mMuteSinksButton = (Button) mActivity.findViewById(R.id.muteAll);
    mMuteSinksButton.setOnClickListener(
        new OnClickListener() {
          @Override
          public void onClick(View arg0) {
            mMediaPlayer.setVolume(0, 0);
            if (mMuteSinksButton.getText().toString().startsWith("Mute"))
              mMuteSinksButton.setText("Unmute All Sinks");
            else mMuteSinksButton.setText("Mute All Sinks");
          }
        });
  }
Пример #5
0
 public void onDialogSinkDisable(String speakerName) {
   mMediaPlayer.removeAllJoynSink(speakerName);
 }
Пример #6
0
 public void onDialogSinkEnable(String speakerName, String speakerPath, short speakerPort) {
   boolean isPlaying = mMediaPlayer.isPlaying();
   mMediaPlayer.addAllJoynSink(speakerName, speakerPath, speakerPort);
   if (isPlaying) mMediaPlayer.start();
 }
Пример #7
0
 public void onDestroy() {
   mMediaPlayer.release();
 }