@Override public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) { Surface s = new Surface(surfaceTexture); try { mMediaPlayer = new MediaPlayer(); mMediaPlayer.setDataSource(mContext, mVideoUri); mMediaPlayer.setSurface(s); mMediaPlayer.prepare(); mMediaPlayer.setOnBufferingUpdateListener(this); mMediaPlayer.setOnCompletionListener(this); mMediaPlayer.setOnPreparedListener(this); mMediaPlayer.setOnVideoSizeChangedListener(this); mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mMediaPlayer.seekTo(0); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } /* if ( mMediaPlayer != null ) { mSeekBar.setProgress( 0 ); mSeekBar.setMax( mMediaPlayer.getDuration() ); mVideoTextureView.SetVideoSize( mMediaPlayer.getVideoWidth(), mMediaPlayer.getVideoHeight() ); } */ }
private void openVideo() { if (mUri == null || mSurface == null) { return; } if (mEnableMusicPause) { sendMusicPauseRequest(); } release(false); try { mPlayer = new MediaPlayer(); mPlayer.setOnPreparedListener(mPreparedListener); mPlayer.setOnVideoSizeChangedListener(mSizeChangedListener); mPlayer.setOnCompletionListener(mCompletionListener); mPlayer.setOnErrorListener(mErrorListener); mPlayer.setDataSource(mCtx, mUri); mPlayer.setSurface(mSurface); mSurface.release(); mSurface = null; mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mPlayer.setParameter(CLEAR_MOTION_KEY, CLEAR_MOTION_DISABLE); mPlayer.prepareAsync(); setCurrentState(STATE_PREPARING); } catch (IOException ex) { Log.e(TAG, "IOException : " + ex); setAllState(STATE_ERROR); mErrorListener.onError(mPlayer, MediaPlayer.MEDIA_ERROR_UNKNOWN, 0); } catch (IllegalArgumentException ex) { Log.e(TAG, "IllegalArgumentException : " + ex); setAllState(STATE_ERROR); mErrorListener.onError(mPlayer, MediaPlayer.MEDIA_ERROR_UNKNOWN, 0); } }
public void setupVideoTexture() { vmaterial = new VideoMaterial(); vt = mTextureManager.addVideoTexture(); int textureid = vt.getTextureId(); mTexture = new SurfaceTexture(textureid); mMediaPlayer = new MediaPlayer(); mMediaPlayer.setOnPreparedListener(this); mMediaPlayer.setOnBufferingUpdateListener(this); mMediaPlayer.setOnCompletionListener(this); mMediaPlayer.setOnErrorListener(this); mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mMediaPlayer.setSurface(new Surface(mTexture)); mMediaPlayer.setLooping(true); }
private void prepare(Surface surface) { try { if (!StringUtil.isNullOrEmpty(path) && mediaPlayer != null) { mediaPlayer.reset(); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); // 设置需要播放的视频 mediaPlayer.setDataSource(path); // 把视频画面输出到Surface mediaPlayer.setSurface(surface); mediaPlayer.setLooping(true); mediaPlayer.prepare(); mediaPlayer.seekTo(0); } } catch (Exception e) { } }
public void prepareVideo(SurfaceTexture t) { this.surface = new Surface(t); mp = new MediaPlayer(); mp.setSurface(this.surface); try { mp.setDataSource(url); mp.prepareAsync(); mp.setOnPreparedListener( new MediaPlayer.OnPreparedListener() { public void onPrepared(MediaPlayer mp) { isMpPrepared = true; mp.setLooping(true); iVideoPreparedListener.onVideoPrepared(video); } }); } catch (IllegalArgumentException e1) { e1.printStackTrace(); } catch (SecurityException e1) { e1.printStackTrace(); } catch (IllegalStateException e1) { e1.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } try { } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (IllegalStateException e) { e.printStackTrace(); } try { } catch (IllegalStateException e) { e.printStackTrace(); } }
/** Loads a movie from a file in the assets folder */ @SuppressLint("NewApi") public boolean load( String filename, MEDIA_TYPE requestedType, boolean playOnTextureImmediately, int seekPosition) { // If the client requests that we should be able to play ON_TEXTURE, // then we need to create a MediaPlayer: boolean canBeOnTexture = false; boolean canBeFullscreen = false; boolean result = false; mMediaPlayerLock.lock(); mSurfaceTextureLock.lock(); // If the media has already been loaded then exit. // The client must first call unload() before calling load again: if ((mCurrentState == MEDIA_STATE.READY) || (mMediaPlayer != null)) { DebugLog.LOGD("Already loaded"); } else { if (((requestedType == MEDIA_TYPE.ON_TEXTURE) || // If the client requests on texture only (requestedType == MEDIA_TYPE.ON_TEXTURE_FULLSCREEN)) && // or on texture with full screen (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH)) // and this is an ICS device { if (mSurfaceTexture == null) { DebugLog.LOGD("Can't load file to ON_TEXTURE because the Surface Texture is not ready"); } else { try { mMediaPlayer = new MediaPlayer(); try { // This example shows how to load the movie from the assets folder of the app // However, if you would like to load the movie from the sdcard or from a network // location // simply comment the three lines below // AssetFileDescriptor afd = // mParentActivity.getAssets().openFd(filename); // // mMediaPlayer.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength()); // afd.close(); File file = new File(VideoPlayback.sdCardDir, filename); fileCheck = file; if (file.exists()) { mMediaPlayer.setDataSource(VideoPlayback.sdCardDir + "/" + filename); // Variavel global que sera usada para deletar o video // caso caia no ErrorListener, pois significa que // ela esta corrompida. fileCheck = file; mMediaPlayer.prepareAsync(); mMediaPlayer.setOnPreparedListener(this); mMediaPlayer.setOnBufferingUpdateListener(this); mMediaPlayer.setOnCompletionListener(this); mMediaPlayer.setOnErrorListener(this); mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mMediaPlayer.setSurface(new Surface(mSurfaceTexture)); canBeOnTexture = true; mShouldPlayImmediately = playOnTextureImmediately; } mCurrentState = MEDIA_STATE.READY; } catch (Exception e) { // Se cair aqui, provavelmente o arquivo esta corrompido // entao deletamos o arquivo, e ele � baixado novamente fileCheck.delete(); mCurrentState = MEDIA_STATE.READY; } } catch (Exception e) { DebugLog.LOGE("Error while creating the MediaPlayer: " + e.toString()); mCurrentState = MEDIA_STATE.ERROR; mMediaPlayerLock.unlock(); mSurfaceTextureLock.unlock(); return false; } } } // If it's not an ICE CREAM SANDWICH device at least else { try { // We need to verify that the file exists // AssetFileDescriptor afd = // mParentActivity.getAssets().openFd(filename); // afd.close(); File file = new File(VideoPlayback.sdCardDir, filename); fileCheck = file; // if (!file.exists()) { mCurrentState = MEDIA_STATE.READY; mVideoType = MEDIA_TYPE.FULLSCREEN; // } } catch (Exception e) { DebugLog.LOGD("File does not exist"); mCurrentState = MEDIA_STATE.ERROR; mMediaPlayerLock.unlock(); mSurfaceTextureLock.unlock(); return false; } } // If the client requests that we should be able to play FULLSCREEN // then we need to create a FullscreenPlaybackActivity if ((requestedType == MEDIA_TYPE.FULLSCREEN) || (requestedType == MEDIA_TYPE.ON_TEXTURE_FULLSCREEN)) { mPlayerHelperActivityIntent = new Intent(mParentActivity, FullscreenPlayback.class); mPlayerHelperActivityIntent.setAction(android.content.Intent.ACTION_VIEW); canBeFullscreen = true; } // We store the parameters for further use mMovieName = filename; mSeekPosition = seekPosition; if (canBeFullscreen && canBeOnTexture) mVideoType = MEDIA_TYPE.ON_TEXTURE_FULLSCREEN; else if (canBeFullscreen) { mVideoType = MEDIA_TYPE.FULLSCREEN; mCurrentState = MEDIA_STATE.READY; } // If it is pure fullscreen then we're ready otherwise we let the MediaPlayer load first else if (canBeOnTexture) mVideoType = MEDIA_TYPE.ON_TEXTURE; else mVideoType = MEDIA_TYPE.UNKNOWN; result = true; } mSurfaceTextureLock.unlock(); mMediaPlayerLock.unlock(); return result; }