private void vplayerInit(boolean isHWCodec) { mPlayer = new MediaPlayer(PlayerService.this.getApplicationContext(), isHWCodec); mPlayer.setOnHWRenderFailedListener( new OnHWRenderFailedListener() { @Override public void onFailed() { if (mListener != null) mListener.onHWRenderFailed(); } }); mPlayer.setOnBufferingUpdateListener(PlayerService.this); mPlayer.setOnCompletionListener(PlayerService.this); mPlayer.setOnPreparedListener(PlayerService.this); mPlayer.setOnVideoSizeChangedListener(PlayerService.this); mPlayer.setOnErrorListener(PlayerService.this); mPlayer.setOnInfoListener(PlayerService.this); }
private void openVideo() { if (mUri == null || mSurfaceHolder == null || !Vitamio.isInitialized(mContext)) return; Intent i = new Intent("com.android.music.musicservicecommand"); i.putExtra("command", "pause"); mContext.sendBroadcast(i); release(false); try { mDuration = -1; mCurrentBufferPercentage = 0; mMediaPlayer = new MediaPlayer(mContext); mMediaPlayer.setOnPreparedListener(mPreparedListener); mMediaPlayer.setOnVideoSizeChangedListener(mSizeChangedListener); mMediaPlayer.setOnCompletionListener(mCompletionListener); mMediaPlayer.setOnErrorListener(mErrorListener); mMediaPlayer.setOnBufferingUpdateListener(mBufferingUpdateListener); mMediaPlayer.setOnInfoListener(mInfoListener); mMediaPlayer.setOnSeekCompleteListener(mSeekCompleteListener); mMediaPlayer.setOnTimedTextListener(mTimedTextListener); mMediaPlayer.setDataSource(mContext, mUri); mMediaPlayer.setDisplay(mSurfaceHolder); mMediaPlayer.setScreenOnWhilePlaying(true); mMediaPlayer.prepareAsync(); mCurrentState = STATE_PREPARING; attachMediaController(); } catch (IOException ex) { Log.e("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.e("Unable to open content: " + mUri, ex); mCurrentState = STATE_ERROR; mTargetState = STATE_ERROR; mErrorListener.onError(mMediaPlayer, MediaPlayer.MEDIA_ERROR_UNKNOWN, 0); return; } }
@Override public void onCreate(Bundle icicle) { super.onCreate(icicle); if (!io.vov.vitamio.LibsChecker.checkVitamioLibs(this)) return; requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow() .setFlags( WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); extras = getIntent().getExtras(); // handle extras if (extras == null) { wrapItUp(RESULT_CANCELED, "Error: No options provided"); } else { if (extras.containsKey("isStreaming")) { isStreaming = extras.getBoolean("isStreaming"); } if (extras.containsKey("shouldAutoClose")) { mShouldAutoClose = extras.getBoolean("shouldAutoClose"); } mMediaType = extras.getString("type"); if (mMediaType == null) mMediaType = MEDIA_TYPE_VIDEO; mMediaPlayer = new MediaPlayer(this); mMediaController = new MediaController(this, !isStreaming); mMediaController.setMediaPlayer(this); mMediaPlayer.setOnBufferingUpdateListener(this); mMediaPlayer.setOnCompletionListener(this); mMediaPlayer.setOnErrorListener(this); mMediaPlayer.setOnPreparedListener(this); mMediaPlayer.setOnVideoSizeChangedListener(this); setVolumeControlStream(AudioManager.STREAM_MUSIC); RelativeLayout relLayout = new RelativeLayout(this); if (extras.containsKey("bgColor")) { try { bgColor = Color.parseColor(extras.getString("bgColor")); } catch (Exception e) { Log.v(TAG, "Error parsing color"); Log.e(TAG, e.toString()); bgColor = DEFAULT_BG_COLOR; } } relLayout.setBackgroundColor(bgColor); RelativeLayout.LayoutParams relLayoutParam = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT); relLayoutParam.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE); mMediaView = new SurfaceView(this); mMediaView.setLayoutParams(relLayoutParam); relLayout.addView(mMediaView); mProgressBar = new ProgressBar(this); mProgressBar.setIndeterminate(true); mProgressBar.setVisibility(View.VISIBLE); RelativeLayout.LayoutParams pblp = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); pblp.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE); mProgressBar.setLayoutParams(pblp); relLayout.addView(mProgressBar); mProgressBar.bringToFront(); mMediaController.setAnchorView(relLayout); mMediaController.setEnabled(true); if (mMediaType.equalsIgnoreCase(MEDIA_TYPE_AUDIO)) { mMediaView.setBackgroundColor(bgColor); if (extras.containsKey("bgImage")) { if (extras.containsKey("bgImageScaleType")) { String scaleType = extras.getString("bgImageScaleType"); if (scaleType.equalsIgnoreCase("fit")) { bgImageScaleType = ImageView.ScaleType.FIT_CENTER; } else if (scaleType.equalsIgnoreCase("stretch")) { bgImageScaleType = ImageView.ScaleType.FIT_XY; } else { bgImageScaleType = ImageView.ScaleType.CENTER; } } bgImage = new ImageView(this); new ImageLoadTask(extras.getString("bgImage"), this).execute(null, null); RelativeLayout.LayoutParams bgImageLayoutParam = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT); bgImageLayoutParam.addRule(RelativeLayout.CENTER_IN_PARENT); bgImage.setLayoutParams(bgImageLayoutParam); bgImage.setScaleType(bgImageScaleType); relLayout.addView(bgImage); } } setContentView(relLayout, relLayoutParam); holder = mMediaView.getHolder(); holder.addCallback(this); holder.setFormat(PixelFormat.RGBA_8888); } }
private void playVideo(Integer Media) { doCleanUp(); try { switch (Media) { case LOCAL_VIDEO: /* * TODO: Set the path variable to a local media file path. */ path = ""; if (path == "") { // Tell the user to provide a media file URL. Toast.makeText( MediaPlayerDemo_Video.this, "Please edit MediaPlayerDemo_Video Activity, " + "and set the path variable to your media file path." + " Your media file must be stored on sdcard.", Toast.LENGTH_LONG) .show(); return; } break; case STREAM_VIDEO: /* * TODO: Set path variable to progressive streamable mp4 or * 3gpp format URL. Http protocol should be used. * Mediaplayer can only play "progressive streamable * contents" which basically means: 1. the movie atom has to * precede all the media data atoms. 2. The clip has to be * reasonably interleaved. * */ if (path == "") { // Tell the user to provide a media file URL. Toast.makeText( MediaPlayerDemo_Video.this, "Please edit MediaPlayerDemo_Video Activity," + " and set the path variable to your media file URL.", Toast.LENGTH_LONG) .show(); return; } break; } // Create a new media player and set the listeners mMediaPlayer = new MediaPlayer(this); mMediaPlayer.setDataSource(path); mMediaPlayer.setDisplay(holder); mMediaPlayer.prepareAsync(); mMediaPlayer.setOnBufferingUpdateListener(this); mMediaPlayer.setOnCompletionListener(this); mMediaPlayer.setOnPreparedListener(this); mMediaPlayer.setOnVideoSizeChangedListener(this); setVolumeControlStream(AudioManager.STREAM_MUSIC); } catch (Exception e) { Log.e(TAG, "error: " + e.getMessage(), e); } }