/** * メディアの再生. * * @return セッションID */ public int playMedia() { if (mSetMediaType == MEDIA_TYPE_MUSIC) { try { if (mMediaStatus == MEDIA_PLAYER_STOP) { mMediaPlayer.prepare(); } if (mMediaStatus == MEDIA_PLAYER_STOP || mMediaStatus == MEDIA_PLAYER_PAUSE || mMediaStatus == MEDIA_PLAYER_PLAY) { mMediaPlayer.seekTo(0); mMyCurrentMediaPosition = 0; if (mMediaStatus == MEDIA_PLAYER_PLAY) { return mMediaPlayer.getAudioSessionId(); } } mMediaPlayer.start(); mMediaStatus = MEDIA_PLAYER_PLAY; } catch (IOException e) { if (BuildConfig.DEBUG) { e.printStackTrace(); } } catch (IllegalStateException e) { if (BuildConfig.DEBUG) { e.printStackTrace(); } } sendOnStatusChangeEvent("play"); return mMediaPlayer.getAudioSessionId(); } else if (mSetMediaType == MEDIA_TYPE_VIDEO) { registerReceiver(mMediaPlayerVideoBR, mIfMediaPlayerVideo); String className = getClassnameOfTopActivity(); if (VideoPlayer.class.getName().equals(className)) { mMediaStatus = MEDIA_PLAYER_PLAY; Intent mIntent = new Intent(VideoConst.SEND_HOSTDP_TO_VIDEOPLAYER); mIntent.putExtra(VideoConst.EXTRA_NAME, VideoConst.EXTRA_VALUE_VIDEO_PLAYER_PLAY); getContext().sendBroadcast(mIntent); sendOnStatusChangeEvent("play"); } else { mMediaStatus = MEDIA_PLAYER_PLAY; Intent mIntent = new Intent(VideoConst.SEND_HOSTDP_TO_VIDEOPLAYER); mIntent.setClass(getContext(), VideoPlayer.class); Uri data = Uri.parse(mMyCurrentFilePath); mIntent.setDataAndType(data, mMyCurrentFileMIMEType); mIntent.putExtra(VideoConst.EXTRA_NAME, VideoConst.EXTRA_VALUE_VIDEO_PLAYER_PLAY); mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(mIntent); sendOnStatusChangeEvent("play"); } return 0; } else { return 0; } }
/** @param media player from apollo service. */ public static void initEqualizer(MediaPlayer player, Context context) { releaseEqualizer(); int id = player.getAudioSessionId(); mEqualizer = new Equalizer(1, id); mBoost = new BassBoost(1, id); updateEqualizerSettings(context); }
/** * メディアの一時停止. * * @return セッションID */ public int pauseMedia() { if (mSetMediaType == MEDIA_TYPE_MUSIC && mMediaStatus != MEDIA_PLAYER_STOP && mMediaStatus != MEDIA_PLAYER_SET) { try { mMediaStatus = MEDIA_PLAYER_PAUSE; mMediaPlayer.pause(); } catch (IllegalStateException e) { if (BuildConfig.DEBUG) { e.printStackTrace(); } } sendOnStatusChangeEvent("pause"); return mMediaPlayer.getAudioSessionId(); } else if (mSetMediaType == MEDIA_TYPE_VIDEO) { mMediaStatus = MEDIA_PLAYER_PAUSE; Intent mIntent = new Intent(VideoConst.SEND_HOSTDP_TO_VIDEOPLAYER); mIntent.putExtra(VideoConst.EXTRA_NAME, VideoConst.EXTRA_VALUE_VIDEO_PLAYER_PAUSE); getContext().sendBroadcast(mIntent); sendOnStatusChangeEvent("pause"); return 0; } else { return 0; } }
public EqualizerController(View rootView, Context context) { mView = rootView; mContext = context; mPlayer = MediaPlayer.create(context, R.raw.testmp3); setupVisualizerUI(); mEqualizer = new Equalizer(0, mPlayer.getAudioSessionId()); // mEqualizer = new Equalizer(0, 0); mNumFrequencyBands = mEqualizer.getNumberOfBands(); mLowerBandLevel = mEqualizer.getBandLevelRange()[0]; mUpperBandLevel = mEqualizer.getBandLevelRange()[1]; mEqualizer.setEnabled(true); setupEqualizerUI(); mVisualizer.setEnabled(true); // When the stream ends, we don't need to collect any more data. We // don't do this in // setupVisualizerFxAndUI because we likely want to have more, // non-Visualizer related code // in this callback. mPlayer.setOnCompletionListener( new MediaPlayer.OnCompletionListener() { public void onCompletion(MediaPlayer mediaPlayer) { mVisualizer.setEnabled(false); } }); mPlayer.start(); }
static { short numberOfBands = 0; short numberOfPresets = 0; PresetInfo[] presets = null; int[] centerFrequency = new int[0]; short[] bandLevelRange = new short[2]; MediaPlayer player = null; Equalizer eq = null; try { player = new MediaPlayer(); eq = new Equalizer(0, player.getAudioSessionId()); numberOfBands = eq.getNumberOfBands(); numberOfPresets = eq.getNumberOfPresets(); presets = new PresetInfo[numberOfPresets]; for (short i = 0; i < numberOfPresets; i++) { PresetInfo preset = new PresetInfo(); eq.usePreset(i); preset.index = i; preset.name = eq.getPresetName(preset.index); preset.settings = AudioEffectSettingsConverter.convert(eq.getProperties()); presets[i] = preset; } centerFrequency = new int[numberOfBands]; for (short i = 0; i < numberOfBands; i++) { centerFrequency[i] = eq.getCenterFreq(i); } bandLevelRange = eq.getBandLevelRange(); } finally { try { if (eq != null) { eq.release(); } } catch (Exception e) { } try { if (player != null) { player.release(); } } catch (Exception e) { } eq = null; player = null; } NUMBER_OF_BANDS = numberOfBands; NUMBER_OF_PRESETS = numberOfPresets; PRESETS = presets; CENTER_FREQUENCY = centerFrequency; BAND_LEVEL_MIN = bandLevelRange[0]; BAND_LEVEL_MAX = bandLevelRange[1]; }
public int getAudioSessionId() { if (mAudioSession == 0) { MediaPlayer foo = new MediaPlayer(); mAudioSession = foo.getAudioSessionId(); foo.release(); } return mAudioSession; }
private void initPlayer() { mPlayer = new MediaPlayer(); mPlayer.setAudioSessionId(mPlayer.getAudioSessionId()); mPlayer.setOnErrorListener(this); mPlayer.setOnInfoListener(this); mPlayer.setOnPreparedListener(this); mPlayer.setOnBufferingUpdateListener(this); mPlayer.setOnSeekCompleteListener(this); mPlayer.setOnCompletionListener(this); }
private void openVideo() { if (mUri == null || mSurfaceHolder == null) { // not ready for playback just yet, will try again later return; } // Tell the music playback service to pause // TODO: these constants need to be published somewhere in the framework. Intent i = new Intent("com.android.music.musicservicecommand"); i.putExtra("command", "pause"); mContext.sendBroadcast(i); // we shouldn't clear the target state, because somebody might have // called start() previously release(false); try { mMediaPlayer = new MediaPlayer(); if (mAudioSession != 0) { mMediaPlayer.setAudioSessionId(mAudioSession); } else { mAudioSession = mMediaPlayer.getAudioSessionId(); } mMediaPlayer.setOnPreparedListener(mPreparedListener); mMediaPlayer.setOnVideoSizeChangedListener(mSizeChangedListener); mMediaPlayer.setOnCompletionListener(mCompletionListener); mMediaPlayer.setOnErrorListener(mErrorListener); mMediaPlayer.setOnInfoListener(mOnInfoListener); mMediaPlayer.setOnBufferingUpdateListener(mBufferingUpdateListener); mCurrentBufferPercentage = 0; mMediaPlayer.setDataSource(mContext, mUri, mHeaders); mMediaPlayer.setDisplay(mSurfaceHolder); mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mMediaPlayer.setScreenOnWhilePlaying(true); mMediaPlayer.prepareAsync(); // we don't set the target state here either, but preserve the // target state that was there before. mCurrentState = STATE_PREPARING; attachMediaController(); } catch (IOException ex) { Log.w(TAG, "Unable to open content: " + mUri, ex); mCurrentState = STATE_ERROR; mTargetState = STATE_ERROR; mErrorListener.onError(mMediaPlayer, MediaPlayer.MEDIA_ERROR_UNKNOWN, 0); return; } catch (IllegalArgumentException ex) { Log.w(TAG, "Unable to open content: " + mUri, ex); mCurrentState = STATE_ERROR; mTargetState = STATE_ERROR; mErrorListener.onError(mMediaPlayer, MediaPlayer.MEDIA_ERROR_UNKNOWN, 0); return; } }
private void setupVisualizerUI() { // Create the Visualizer object and attach it to our media player. mVisualizerView = (VisualizerView) mView.findViewById(R.id.myvisualizerview); mVisualizer = new Visualizer(mPlayer.getAudioSessionId()); mVisualizer.setCaptureSize(Visualizer.getCaptureSizeRange()[1]); mVisualizer.setDataCaptureListener( new Visualizer.OnDataCaptureListener() { public void onWaveFormDataCapture(Visualizer visualizer, byte[] bytes, int samplingRate) { mVisualizerView.updateVisualizer(bytes); } public void onFftDataCapture(Visualizer visualizer, byte[] bytes, int samplingRate) {} }, Visualizer.getMaxCaptureRate() / 2, true, false); }
/** * Mediaの再再生. * * @return SessionID */ public int resumeMedia() { if (mSetMediaType == MEDIA_TYPE_MUSIC) { try { mMediaStatus = MEDIA_PLAYER_PLAY; mMediaPlayer.start(); } catch (IllegalStateException e) { if (BuildConfig.DEBUG) { e.printStackTrace(); } } sendOnStatusChangeEvent("play"); return mMediaPlayer.getAudioSessionId(); } else if (mSetMediaType == MEDIA_TYPE_VIDEO) { mMediaStatus = MEDIA_PLAYER_PLAY; Intent mIntent = new Intent(VideoConst.SEND_HOSTDP_TO_VIDEOPLAYER); mIntent.putExtra(VideoConst.EXTRA_NAME, VideoConst.EXTRA_VALUE_VIDEO_PLAYER_RESUME); this.getContext().sendBroadcast(mIntent); sendOnStatusChangeEvent("play"); return 0; } return 0; }
/** * Links the visualizer to a player * * @param player - MediaPlayer instance to link to */ public void link(MediaPlayer player) { if (player == null) { throw new NullPointerException("Cannot link to null MediaPlayer"); } // Create the Visualizer object and attach it to our media player. mVisualizer = new Visualizer(player.getAudioSessionId()); mVisualizer.setCaptureSize(Visualizer.getCaptureSizeRange()[1]); // Pass through Visualizer data to VisualizerView Visualizer.OnDataCaptureListener captureListener = new Visualizer.OnDataCaptureListener() { @Override public void onWaveFormDataCapture( Visualizer visualizer, byte[] bytes, int samplingRate) { updateVisualizer(bytes); } @Override public void onFftDataCapture(Visualizer visualizer, byte[] bytes, int samplingRate) { updateVisualizerFFT(bytes); } }; mVisualizer.setDataCaptureListener( captureListener, Visualizer.getMaxCaptureRate() / 2, true, true); // Enabled Visualizer and disable when we're done with the stream mVisualizer.setEnabled(true); player.setOnCompletionListener( new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mediaPlayer) { mVisualizer.setEnabled(false); } }); }
private void openVideo() { if ((mFd == null && mUri == null) || mSurfaceHolder == null) { // not ready for playback just yet, will try again later return; } // Tell the music playback service to pause // TODO: these constants need to be published somewhere in the framework. Intent i = new Intent("com.android.music.musicservicecommand"); i.putExtra("command", "pause"); mContext.sendBroadcast(i); // we shouldn't clear the target state, because somebody might have // called start() previously release(false); try { mMediaPlayer = new MediaPlayer(); // TODO: create SubtitleController in MediaPlayer, but we need // a context for the subtitle renderers final Context context = getContext(); // final SubtitleController controller = new SubtitleController(context, // mMediaPlayer.getMediaTimeProvider(), mMediaPlayer); // controller.registerRenderer(new WebVttRenderer(context)); // mMediaPlayer.setSubtitleAnchor(controller, this); if (mAudioSession != 0) { mMediaPlayer.setAudioSessionId(mAudioSession); } else { mAudioSession = mMediaPlayer.getAudioSessionId(); } mMediaPlayer.setOnPreparedListener(mPreparedListener); mMediaPlayer.setOnVideoSizeChangedListener(mSizeChangedListener); mMediaPlayer.setOnCompletionListener(mCompletionListener); mMediaPlayer.setOnErrorListener(mErrorListener); mMediaPlayer.setOnInfoListener(mInfoListener); mMediaPlayer.setOnBufferingUpdateListener(mBufferingUpdateListener); mCurrentBufferPercentage = 0; if (mUri != null) { mMediaPlayer.setDataSource(mContext, mUri, mHeaders); } else { mMediaPlayer.setDataSource(mFd); } mMediaPlayer.setDisplay(mSurfaceHolder); mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mMediaPlayer.setScreenOnWhilePlaying(true); mMediaPlayer.prepareAsync(); /* for (Pair<InputStream, MediaFormat> pending: mPendingSubtitleTracks) { try { mMediaPlayer.addSubtitleSource(pending.first, pending.second); } catch (IllegalStateException e) { mInfoListener.onInfo( mMediaPlayer, MediaPlayer.MEDIA_INFO_UNSUPPORTED_SUBTITLE, 0); } }*/ // we don't set the target state here either, but preserve the // target state that was there before. mCurrentState = STATE_PREPARING; attachMediaController(); } catch (IOException ex) { Log.w(TAG, "Unable to open content: ", ex); mCurrentState = STATE_ERROR; mTargetState = STATE_ERROR; mErrorListener.onError(mMediaPlayer, MediaPlayer.MEDIA_ERROR_UNKNOWN, 0); return; } catch (IllegalArgumentException ex) { Log.w(TAG, "Unable to open content: ", ex); mCurrentState = STATE_ERROR; mTargetState = STATE_ERROR; mErrorListener.onError(mMediaPlayer, MediaPlayer.MEDIA_ERROR_UNKNOWN, 0); return; } finally { mPendingSubtitleTracks.clear(); } }