Ejemplo n.º 1
0
  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);
    }
  }
Ejemplo n.º 2
0
  private void playVideo() {
    doCleanUp();
    try {
      /*
       * 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.
       */
      path = "http://www.androidbook.com/akc/filestorage/android/documentfiles/3389/movie.mp4";
      //            path = "http://forum.ea3w.com/coll_ea3w/attach/2008_10/12237832415.3gp";

      // Create a new media player and set the listeners
      mMediaPlayer = new MediaPlayer();
      mMediaPlayer.setDataSource(path);
      mMediaPlayer.setDisplay(holder);
      mMediaPlayer.prepare();
      mMediaPlayer.setOnBufferingUpdateListener(this);
      mMediaPlayer.setOnCompletionListener(this);
      mMediaPlayer.setOnPreparedListener(this);
      mMediaPlayer.setOnVideoSizeChangedListener(this);
      mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

    } catch (Exception e) {
      Log.e(TAG, "error: " + e.getMessage(), e);
    }
  }
  @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() );
    }

    */
  }
  public void openVideo(Uri uri) {
    if (uri == null || mSurfaceHolder == null || getContext() == null) {
      // not ready for playback just yet, will try again later
      if (mSurfaceHolder == null && uri != null) {
        mUri = uri;
      }
      return;
    }

    mUri = uri;
    mDuration = 0;

    // Idle 状态:当使用new()方法创建一个MediaPlayer对象或者调用了其reset()方法时,该MediaPlayer对象处于idle状态。
    // End 状态:通过release()方法可以进入End状态,只要MediaPlayer对象不再被使用,就应当尽快将其通过release()方法释放掉
    // Initialized 状态:这个状态比较简单,MediaPlayer调用setDataSource()方法就进入Initialized状态,表示此时要播放的文件已经设置好了。
    // Prepared
    // 状态:初始化完成之后还需要通过调用prepare()或prepareAsync()方法,这两个方法一个是同步的一个是异步的,只有进入Prepared状态,才表明MediaPlayer到目前为止都没有错误,可以进行文件播放。

    Exception exception = null;
    try {
      if (mMediaPlayer == null) {
        mMediaPlayer = new MediaPlayer();
        mMediaPlayer.setOnPreparedListener(mPreparedListener);
        mMediaPlayer.setOnCompletionListener(mCompletionListener);
        mMediaPlayer.setOnErrorListener(mErrorListener);
        mMediaPlayer.setOnVideoSizeChangedListener(mVideoSizeChangedListener);
        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mMediaPlayer.setOnSeekCompleteListener(mSeekCompleteListener);
        mMediaPlayer.setOnInfoListener(mInfoListener);
        //			mMediaPlayer.setScreenOnWhilePlaying(true);
        //				mMediaPlayer.setVolume(mSystemVolumn, mSystemVolumn);
        mMediaPlayer.setDisplay(mSurfaceHolder);
      } else {
        mMediaPlayer.reset();
      }
      mMediaPlayer.setDataSource(getContext(), uri);

      //			if (mLooping)
      //				mMediaPlayer.setLooping(true);//循环播放
      mMediaPlayer.prepareAsync();
      // we don't set the target state here either, but preserve the
      // target state that was there before.
      mCurrentState = STATE_PREPARING;
    } catch (IOException ex) {
      exception = ex;
    } catch (IllegalArgumentException ex) {
      exception = ex;
    } catch (Exception ex) {
      exception = ex;
    }
    if (exception != null) {
      Logger.e(exception);
      mCurrentState = STATE_ERROR;
      if (mErrorListener != null)
        mErrorListener.onError(mMediaPlayer, MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
    }
  }
Ejemplo n.º 5
0
  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");
    getContext().sendBroadcast(i);

    if (mMediaPlayer != null) {
      mMediaPlayer.reset();
      mMediaPlayer.release();
      mMediaPlayer = null;
    }
    try {
      mMediaPlayer = new MediaPlayer();
      mMediaPlayer.setOnPreparedListener(mPreparedListener);
      mMediaPlayer.setOnVideoSizeChangedListener(mSizeChangedListener);
      mIsPrepared = false;
      Log.v(TAG, "reset duration to -1 in openVideo");
      mDuration = -1;
      mMediaPlayer.setOnCompletionListener(mCompletionListener);
      mMediaPlayer.setOnErrorListener(mErrorListener);
      mMediaPlayer.setOnBufferingUpdateListener(mBufferingUpdateListener);
      mCurrentBufferPercentage = 0;
      if (URLUtil.isAssetUrl(mUri.toString())) { // DST: 20090606 detect asset url
        AssetFileDescriptor afd = null;
        try {
          String path = mUri.toString().substring("file:///android_asset/".length());
          afd = getContext().getAssets().openFd(path);
          mMediaPlayer.setDataSource(
              afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
        } finally {
          if (afd != null) {
            afd.close();
          }
        }
      } else {
        setDataSource();
      }
      mMediaPlayer.setDisplay(mSurfaceHolder);
      mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
      mMediaPlayer.setScreenOnWhilePlaying(true);
      mMediaPlayer.prepareAsync();
      attachMediaController();
    } catch (IOException ex) {
      Log.w(TAG, "Unable to open content: " + mUri, ex);
      return;
    } catch (IllegalArgumentException ex) {
      Log.w(TAG, "Unable to open content: " + mUri, ex);
      return;
    }
  }
Ejemplo n.º 6
0
 private void initplayer() {
   if (player == null) {
     player = new MediaPlayer();
     player.setOnPreparedListener(MediaPlayerListener);
     player.setOnVideoSizeChangedListener(MediaPlayerListener);
     player.setOnSeekCompleteListener(MediaPlayerListener);
     player.setOnInfoListener(MediaPlayerListener);
     player.setOnCompletionListener(MediaPlayerListener);
     player.setOnErrorListener(MediaPlayerListener);
     player.setOnBufferingUpdateListener(MediaPlayerListener);
   }
 }
Ejemplo n.º 7
0
  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 playVideo(Integer Media) {
    doCleanUp();
    try {

      /*
               * 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:    }

      private void playVideo(Integer Media) {
          doCleanUp();
          try {

              /*
               * 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(
                MediaPlayerVideoActivity.this,
                "Please edit MediaPlayerDemo_Video Activity,"
                    + " and set the path variable to your media file URL.",
                Toast.LENGTH_LONG)
            .show();
      }

      // Create a new media player and set the listeners
      mMediaPlayer = new MediaPlayer();
      mMediaPlayer.setDataSource(path);
      mMediaPlayer.setDisplay(holder);
      mMediaPlayer.prepare();
      mMediaPlayer.setOnBufferingUpdateListener(this);
      mMediaPlayer.setOnCompletionListener(this);
      mMediaPlayer.setOnPreparedListener(this);
      mMediaPlayer.setOnVideoSizeChangedListener(this);
      mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

    } catch (Exception e) {
      Log.e(TAG, "error: " + e.getMessage(), e);
    }
  }
Ejemplo n.º 9
0
  private void openVideo() {
    if (mUri == null || mSurfaceHolder == null) {
      // not ready for playback just yet, will try again later
      return;
    }

    // we shouldn't clear the target state, because somebody might have
    // called start() previously
    release(false);
    try {
      mMediaPlayer = new MediaPlayer();
      mMediaPlayer.setOnPreparedListener(mPreparedListener);
      mMediaPlayer.setOnVideoSizeChangedListener(mSizeChangedListener);
      mDuration = -1;
      mMediaPlayer.setOnCompletionListener(mCompletionListener);
      mMediaPlayer.setOnErrorListener(mErrorListener);
      mMediaPlayer.setOnBufferingUpdateListener(mBufferingUpdateListener);
      mCurrentBufferPercentage = 0;
      mMediaPlayer.setDataSource(getContext(), 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;
    } catch (IllegalStateException 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;
    }
  }
Ejemplo n.º 10
0
  private void openVideo() {
    /*
     * if (mUri == null || mSurfaceHolder == null) { // not ready for
     * playback just yet, will try again later return; }
     */
    if (mInputStream == null || mSurfaceHolder == null) {
      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);

    if (mMediaPlayer != null) {
      mMediaPlayer.reset();
      mMediaPlayer.release();
      mMediaPlayer = null;
    }
    try {
      mMediaPlayer = new MediaPlayer();
      mMediaPlayer.setOnPreparedListener(mPreparedListener);
      mMediaPlayer.setOnVideoSizeChangedListener(mSizeChangedListener);
      mIsPrepared = false;
      Log.v(TAG, "reset duration to -1 in openVideo");
      mDuration = -1;
      mMediaPlayer.setOnCompletionListener(mCompletionListener);
      mMediaPlayer.setOnErrorListener(mErrorListener);
      mMediaPlayer.setOnBufferingUpdateListener(mBufferingUpdateListener);
      mCurrentBufferPercentage = 0;
      // mMediaPlayer.setDataSource(mContext, mUri);
      mMediaPlayer.setDataSource(mInputStream.getFD());
      mMediaPlayer.setDisplay(mSurfaceHolder);
      mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
      mMediaPlayer.setScreenOnWhilePlaying(true);
      mMediaPlayer.prepareAsync();
      attachMediaController();
    } catch (IOException ex) {
      Log.w(TAG, "Unable to open content: " + mInputStream, ex);
      return;
    } catch (IllegalArgumentException ex) {
      Log.w(TAG, "Unable to open content: " + mInputStream, ex);
      return;
    }
  }
Ejemplo n.º 11
0
  public AndroidMusic(AssetFileDescriptor assetDescriptor) {
    mediaPlayer = new MediaPlayer();
    try {
      mediaPlayer.setDataSource(
          assetDescriptor.getFileDescriptor(),
          assetDescriptor.getStartOffset(),
          assetDescriptor.getLength());
      mediaPlayer.prepare();
      isPrepared = true;
      mediaPlayer.setOnCompletionListener(this);
      mediaPlayer.setOnSeekCompleteListener(this);
      mediaPlayer.setOnPreparedListener(this);
      mediaPlayer.setOnVideoSizeChangedListener(this);

    } catch (Exception e) {
      throw new RuntimeException("Couldn't load music");
    }
  }
Ejemplo n.º 12
0
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    Log.e(TAG, "onCreate");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_video_player);

    mVideoSurface = (ResizeSurfaceView) findViewById(R.id.videoSurface);
    mContentView = findViewById(R.id.video_container);
    mLoadingView = findViewById(R.id.loading);
    SurfaceHolder videoHolder = mVideoSurface.getHolder();
    videoHolder.addCallback(this);

    mMediaPlayer = new MediaPlayer();
    mMediaPlayer.setOnVideoSizeChangedListener(this);
    controller = new VideoControllerView(this);
    mLoadingView.setVisibility(View.VISIBLE);

    try {
      mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
      mMediaPlayer.setDataSource(
          this, Uri.parse("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"));
      mMediaPlayer.setOnPreparedListener(this);
    } catch (IllegalArgumentException e) {
      e.printStackTrace();
    } catch (SecurityException e) {
      e.printStackTrace();
    } catch (IllegalStateException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }

    mVideoSurface.setOnTouchListener(
        new View.OnTouchListener() {
          @Override
          public boolean onTouch(View v, MotionEvent event) {
            controller.toggleContollerView();
            return false;
          }
        });
  }
  private void createPlayer(SurfaceHolder holder) {
    if (mMediaPlayer != null) {
      destroyPlayer(holder);
    }

    mMediaPlayer = new MediaPlayer();
    mHolder = holder;

    mVideoWidth = 0;
    mVideoHeight = 0;
    mVideoDurationSeconds = 0;

    try {
      mMediaPlayer.setDisplay(mHolder);
      mMediaPlayer.setDataSource(mUrl);

      mMediaPlayer.setScreenOnWhilePlaying(true);

      mMediaPlayer.setOnPreparedListener(this);
      mMediaPlayer.setOnVideoSizeChangedListener(this);
      mMediaPlayer.setOnCompletionListener(this);
      mMediaPlayer.setOnErrorListener(this);
      mMediaPlayer.setOnInfoListener(this);
      mMediaPlayer.setOnBufferingUpdateListener(this);
      mMediaPlayer.setOnSeekCompleteListener(this);

      mMediaPlayer.prepareAsync();
    } catch (IllegalArgumentException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (SecurityException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IllegalStateException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
Ejemplo n.º 14
0
    public void surfaceCreated(SurfaceHolder holder) {
      Log.d(TAG, "surfaceCreated called");

      // TODO: Holy hack batman. Fix this asap.
      ArrayList<String> videos = ((PlaybackActivity) getActivity()).getVideos();
      String url = getId() == R.id.video_1_fragment ? videos.get(0) : videos.get(1);

      try {
        mMediaPlayer = new MediaPlayer();
        mMediaPlayer.setDataSource(this.getActivity(), Uri.fromFile(new File(url)));
        mMediaPlayer.setDisplay(mSurfaceHolder);
        mMediaPlayer.prepare();
        mMediaPlayer.setOnBufferingUpdateListener(this);
        mMediaPlayer.setOnCompletionListener(this);
        mMediaPlayer.setOnPreparedListener(this);
        mMediaPlayer.setOnVideoSizeChangedListener(this);
        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mMediaPlayer.setLooping(true);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
Ejemplo n.º 15
0
  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();
    }
  }
Ejemplo n.º 16
0
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Remove title bar
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    // Remove notification bar
    this.getWindow()
        .setFlags(
            WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    // set content view AFTER ABOVE sequence (to avoid crash)
    setContentView(R.layout.player);

    decorView = getWindow().getDecorView();
    uiOption = getWindow().getDecorView().getSystemUiVisibility();

    surfaceView = (SurfaceView) this.findViewById(R.id.SurfaceView);
    surfaceHolder = surfaceView.getHolder();
    surfaceHolder.addCallback(this);

    mediaPlayer = new MediaPlayer();
    mediaPlayer.setOnCompletionListener(this);
    mediaPlayer.setOnErrorListener(this);
    mediaPlayer.setOnInfoListener(this);
    mediaPlayer.setOnPreparedListener(this);
    mediaPlayer.setOnSeekCompleteListener(this);
    mediaPlayer.setOnVideoSizeChangedListener(this);

    mMenuBar = (RelativeLayout) findViewById(R.id.menuBar);
    mMenuButton = (ImageView) findViewById(R.id.menuButton);
    mVideoList = (LinearLayout) findViewById(R.id.videoList);
    mTitle = (TextView) findViewById(R.id.MovieTitle);
    mMenuButton.setOnClickListener(this);
    mVideoList.setOnClickListener(this);

    /*
     * String filePath = Environment.getExternalStorageDirectory().getPath()
     * + "/Movies/Spanglish_H264_AAC_800x432.mp4";
     */

    // POINT: sdcard 의 동영상 파일 목록을 구하기
    String sdPath = Environment.getExternalStorageDirectory().getAbsolutePath();
    File sdRoot = new File(sdPath);
    FileList(sdRoot);

    for (int i = 0; i < mFileList.size(); i++) {
      if ((mFileList.get(i).endsWith(".mp4")) || (mFileList.get(i).endsWith(".mkv"))) {
        Log.e("TAT", mFileList.get(i));
        mVideoFileList.add(mFileList.get(i));
      }
    }

    if (mVideoFileList.isEmpty()) {
      Toast.makeText(this, "재생할 파일이 없습니다.", 1).show();
    }

    for (int i = 0; i < mVideoFileList.size(); i++) {
      final String path = mVideoFileList.get(i);
      final String filename = path.substring(path.lastIndexOf("/") + 1);
      final TextView text = new TextView(this);
      text.setContentDescription(i + "");
      text.setSingleLine();
      text.setPadding(
          text.getPaddingLeft() + 10,
          text.getPaddingTop() + 10,
          text.getPaddingRight(),
          text.getPaddingBottom());
      text.setHeight(80);
      text.setText(filename);
      text.setOnClickListener(
          new OnClickListener() {
            @Override
            public void onClick(View v) {
              try {
                if (mediaPlayer.isPlaying()) mediaPlayer.stop();
                mediaPlayer.reset();
                mediaPlayer.setDataSource(path);
                mediaPlayer.prepare();
                idx = Integer.parseInt(text.getContentDescription().toString());
                mTitle.setText("[ " + filename + " ]");
              } catch (IllegalArgumentException e) {
                Log.v(LOGTAG, e.getMessage());
                finish();
              } catch (IllegalStateException e) {
                Log.v(LOGTAG, e.getMessage());
                finish();
              } catch (SecurityException e) {
                e.printStackTrace();
              } catch (IOException e) {
                e.printStackTrace();
              }
            }
          });
      mVideoList.addView(text);
    }

    /* TODO : intend를 통해 클릭한 동영상 path 넘겨주기 */
    // Intent intent = getIntent();
    // mmList = intent.getStringArrayListExtra("vList");

    String filePath = mVideoFileList.get(0);

    try {
      mediaPlayer.setDataSource(filePath);
      String filename = filePath.substring(filePath.lastIndexOf("/") + 1);
      mTitle.setText("[ " + filename + " ]");
    } catch (IllegalArgumentException e) {
      Log.v(LOGTAG, e.getMessage());
      finish();
    } catch (IllegalStateException e) {
      Log.v(LOGTAG, e.getMessage());
      finish();
    } catch (IOException e) {
      Log.v(LOGTAG, e.getMessage());
      finish();
    }

    controller =
        new MediaController(this) {
          @Override
          public void show() {
            super.show(0);
            mMenuBar.setVisibility(View.VISIBLE);
          }

          @Override
          public void hide() {
            super.hide();
            mMenuBar.setVisibility(View.INVISIBLE);
            ((ScrollView) mVideoList.getParent()).setVisibility(View.INVISIBLE);
          }
        };
    currentDisplay = getWindowManager().getDefaultDisplay();

    //
    // if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    // uiOption |= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
  }
Ejemplo n.º 17
0
  private void prepare() {
    mp = new MediaPlayer();
    try {
      //        	mp.setDataSource("http://forum.ea3w.com/coll_ea3w/attach/2008_10/12237832415.3gp");
      //
      //	mp.setDataSource("http://res.happyge.com:8081/client/test2/101/10101/101010100/img/400000.mp4");
      mp.setDataSource(url);
      mp.prepareAsync();
    } catch (IllegalArgumentException | SecurityException | IllegalStateException | IOException e) {
      e.printStackTrace();
    }
    mp.setOnBufferingUpdateListener(
        new OnBufferingUpdateListener() {

          @Override
          public void onBufferingUpdate(MediaPlayer mp, int percent) {}
        });
    mp.setOnCompletionListener(
        new MediaPlayer.OnCompletionListener() {

          @Override
          public void onCompletion(MediaPlayer mp) {
            state = VideoState.stop;
            ivStart.setVisibility(View.VISIBLE);
            ivStop.setVisibility(View.INVISIBLE);
            ivPause.setVisibility(View.INVISIBLE);
          }
        });
    mp.setOnPreparedListener(
        new OnPreparedListener() {

          @Override
          public void onPrepared(MediaPlayer mp) {
            mIsVideoReadyToBePlayed = true;
            mp.seekTo(0);
            startVideoPlayback();
          }
        });
    mp.setOnVideoSizeChangedListener(
        new OnVideoSizeChangedListener() {

          @Override
          public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {

            if (width == 0 || height == 0) {
              return;
            }
            //				if (width > AppInfo.getScreenWidth()) {
            double d = (double) AppInfo.getScreenWidth() / width;
            width = AppInfo.getScreenWidth();
            height = (int) (height * d);
            //				}
            mIsVideoSizeKnown = true;
            mVideoWidth = width;
            mVideoHeight = height;

            startVideoPlayback();
          }
        });
    mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
  }