private void initMediaPlayer() {

    mediaPlayer = new MediaPlayer();

    mediaPlayer.setAudioStreamType(STREAM_TYPE);
    mediaPlayer.setVolume(1f, 1f);

    mediaPlayer.setOnErrorListener(
        new OnErrorListener() {
          @Override
          public boolean onError(MediaPlayer mp, int what, int extra) {
            Log.e(TAG, "MediaPlayer.onErrorListener (What: " + what + ", Extra: " + extra + ")");
            // mp.reset();
            if (what == MediaPlayer.MEDIA_ERROR_SERVER_DIED) {
              mp.release();
              initMediaPlayer();
            }
            return true;
          }
        });

    mediaPlayer.setOnInfoListener(
        new OnInfoListener() {
          @Override
          public boolean onInfo(MediaPlayer mp, int what, int extra) {
            Log.e(TAG, "MediaPlayer.onInfoListener (What: " + what + ", Extra: " + extra + ")");
            return false;
          }
        });
  }
Пример #2
0
  private void openVideo() {
    if (mUri == null || mSurfaceHolder == null) {
      // not ready for playback just yet, will try again later
      return;
    }

    if (mMediaPlayer != null) {
      mMediaPlayer.reset();
      mMediaPlayer.release();
      mMediaPlayer = null;
    }
    try {
      mMediaPlayer = new MediaPlayer();
      mMediaPlayer.setOnPreparedListener(mPreparedListener);
      mIsPrepared = false;
      mMediaPlayer.setOnCompletionListener(mCompletionListener);
      mMediaPlayer.setOnErrorListener(mErrorListener);
      mMediaPlayer.setOnBufferingUpdateListener(mBufferingUpdateListener);
      mCurrentBufferPercentage = 0;
      mMediaPlayer.setDataSource(mContext, mUri);
      mMediaPlayer.setDisplay(mSurfaceHolder);
      mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
      mMediaPlayer.setScreenOnWhilePlaying(true);
      mMediaPlayer.prepareAsync();
      attachMediaController();
    } catch (IOException ex) {
      Log.w("VideoView", "Unable to open content: " + mUri, ex);
      return;
    } catch (IllegalArgumentException ex) {
      Log.w("VideoView", "Unable to open content: " + mUri, ex);
      return;
    }
  }
Пример #3
0
  /**
   * 构造方法
   *
   * @param context
   */
  public MusicPlayer(Context context) {
    this.context = context;

    //            File file = new File(filePath);
    //            if(!file.exists()){
    //                Timber.e("文件不存在!!!");
    //            }

    // 初始化
    mediaPlayer = MediaPlayer.create(context, R.raw.test);
    //            mediaPlayer.setLooping(true);
    //            mediaPlayer.prepare();

    // 设置监听器
    mediaPlayer.setOnCompletionListener(
        new MediaPlayer.OnCompletionListener() {
          @Override
          public void onCompletion(MediaPlayer mp) {
            Timber.e("complete");
          }
        });

    mediaPlayer.setOnErrorListener(
        new MediaPlayer.OnErrorListener() {
          @Override
          public boolean onError(MediaPlayer mp, int what, int extra) {
            Timber.e("error");
            return false;
          }
        });
  }
Пример #4
0
  public void startPlayback(float percentage) {
    if (state() == PLAYING_PAUSED_STATE) {
      mSampleStart = System.currentTimeMillis() - mPlayer.getCurrentPosition();
      mPlayer.seekTo((int) (percentage * mPlayer.getDuration()));
      mPlayer.start();
      setState(PLAYING_STATE);
    } else {
      stop();

      mPlayer = new MediaPlayer();
      try {
        mPlayer.setDataSource(mSampleFile.getAbsolutePath());
        mPlayer.setOnCompletionListener(this);
        mPlayer.setOnErrorListener(this);
        mPlayer.prepare();
        mPlayer.seekTo((int) (percentage * mPlayer.getDuration()));
        mPlayer.start();
      } catch (IllegalArgumentException e) {
        setError(INTERNAL_ERROR);
        mPlayer = null;
        return;
      } catch (IOException e) {
        setError(STORAGE_ACCESS_ERROR);
        mPlayer = null;
        return;
      }

      mSampleStart = System.currentTimeMillis();
      setState(PLAYING_STATE);
    }
  }
  private void initData(Context context) {
    this.mContext = context;
    mActivity = (Activity) context;
    DisplayMetrics dm = new DisplayMetrics();
    mActivity.getWindowManager().getDefaultDisplay().getMetrics(dm);
    mDensity = dm.density;
    mWidth = dm.widthPixels;
    mInflater = LayoutInflater.from(mContext);
    AudioManager audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
    audioManager.setMode(AudioManager.MODE_NORMAL);
    if (audioManager.isSpeakerphoneOn()) {
      audioManager.setSpeakerphoneOn(true);
    } else {
      audioManager.setSpeakerphoneOn(false);
    }
    mp.setAudioStreamType(AudioManager.STREAM_RING);
    mp.setOnErrorListener(
        new OnErrorListener() {

          @Override
          public boolean onError(MediaPlayer mp, int what, int extra) {
            return false;
          }
        });
  }
 /** Prepare the given url */
 @Override
 public MediaPlayerInterface prepare(
     Context context,
     Query query,
     MediaPlayer.OnPreparedListener onPreparedListener,
     MediaPlayer.OnCompletionListener onCompletionListener,
     MediaPlayer.OnErrorListener onErrorListener) {
   Log.d(TAG, "prepare()");
   mOnPreparedListener = onPreparedListener;
   mOnCompletionListener = onCompletionListener;
   mOnErrorListener = onErrorListener;
   mPreparedQuery = null;
   mPreparingQuery = query;
   release();
   mMediaPlayer =
       MediaPlayer.create(context, Uri.parse(query.getPreferredTrackResult().getPath()));
   if (mMediaPlayer == null) {
     return null;
   }
   mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
   mMediaPlayer.setOnPreparedListener(this);
   mMediaPlayer.setOnErrorListener(this);
   mMediaPlayer.setOnCompletionListener(this);
   return this;
 }
Пример #7
0
 public void initAudioPlayer() {
   mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
   mediaPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
   mediaPlayer.setOnPreparedListener(this);
   // mediaPlayer.setOnCompletionListener(this);
   mediaPlayer.setOnErrorListener(this);
 }
Пример #8
0
  private void createPlayer() {
    try {
      if (mSongSelector == null) mSongSelector = new SongSelector(getContentResolver(), this);

      if (mPlayer == null) {
        mPlayer = new MediaPlayer();

        mPlayer.setOnPreparedListener(this);
        mPlayer.setOnSeekCompleteListener(this);
        mPlayer.setOnCompletionListener(this);
        mPlayer.setOnErrorListener(this);

        mPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
      }
    } 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();
    }
  }
  public boolean playFileForMute(String path, MediaEventsHandler parent) {
    if (null != mMediaPlayer && mMediaPlayer.isPlaying()) {
      return false;
    }
    this.mParent = parent;
    try {
      mMediaPlayer = new MediaPlayer();
      FileInputStream fin = new FileInputStream(path);
      mMediaPlayer.setDataSource(fin.getFD());
      mMediaPlayer.prepare();
      mMediaPlayer.setOnCompletionListener(this);
      mMediaPlayer.setOnErrorListener(this);
      mMediaPlayer.setOnInfoListener(this);
      mMediaPlayer.start();

    } catch (IOException e) {

      return false;
    } catch (Exception e) {
      System.out.println("########################EXCEPTION HANDLED#############################");
      e.printStackTrace();

      return false;
    }
    return true;
  }
Пример #10
0
  // playback
  public Playback(SurfaceView surfaceview, int playerMode) {
    mmediaplayer = new MediaPlayer();

    msurfaceholder = surfaceview.getHolder();
    msurfaceholder.addCallback(this);
    // msurfaceholder.setFixedSize(surfaceview.getWidth(),
    // surfaceview.getHeight());
    msurfaceholder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    mmediaplayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    mmediaplayer.setDisplay(msurfaceholder);

    mmediaplayer.setOnErrorListener(errListener);
    mmediaplayer.setOnPreparedListener(preListener);
    mmediaplayer.setOnCompletionListener(completeListener);
    mmediaplayer.setOnSeekCompleteListener(seekListener);

    mPlayStatus = VideoConst.PLAY_STATUS_UNKNOW;

    if (playerMode == VideoConst.PLAYER_MODE_MMP) {
      videopl = PlayList.getPlayList();
      mcurPath = videopl.getCurrentPath(Const.FILTER_VIDEO);
    }
    mplayerMode = playerMode;

    pb = this;
  }
Пример #11
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);
    }
  }
Пример #12
0
  @Override
  public void onCreate() {

    feedBackIntent = new Intent(BROADCAST_FEEDBACK);
    seekIntent = new Intent(BROADCAST_SEEK);

    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    mediaPlayer.setOnCompletionListener(this);
    mediaPlayer.setOnErrorListener(this);
    mediaPlayer.setOnBufferingUpdateListener(this);
    mediaPlayer.setOnInfoListener(this);
    mediaPlayer.setOnSeekCompleteListener(this);
    mediaPlayer.setOnPreparedListener(this);
    mediaPlayer.reset();

    DropboxConnect dropConnect = new DropboxConnect(getApplicationContext());
    dropboxApi = dropConnect.getDropboxAPI();

    nowplayingData = SongSQLite.getmInstance(getApplicationContext());
    playingSongList = nowplayingData.getAllSong(SongSQLite.TABLE_NOWPLAYING, null);
    intCurrentSong = 0;

    if (playingSongList == null) playingSongList = new ArrayList<>(); // If Database is Emply

    intTotalSong = playingSongList.size();
    updateCurrentSong();

    initNotification();
    Log.d(">>>> THAI <<<<", "onCreat() Service");
  }
Пример #13
0
  public AudioApi(MediaPlayer audioPlayer) {
    this.audioPlayer = audioPlayer;

    audioPlayer.setOnErrorListener(this);
    audioPlayer.setOnPreparedListener(this);
    audioPlayer.setOnCompletionListener(this);
    audioPlayer.setOnSeekCompleteListener(this);
    audioPlayer.setOnBufferingUpdateListener(this);
  }
  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);
    }
  }
 public void initMusicPlayer() {
   // set player properties
   player.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
   player.setAudioStreamType(AudioManager.STREAM_MUSIC);
   // set listeners
   player.setOnPreparedListener(this);
   player.setOnCompletionListener(this);
   player.setOnErrorListener(this);
 }
Пример #16
0
 @Override
 public void onCreate() {
   count = 0;
   mediaPlayer = new MediaPlayer();
   mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
   // set listeners
   mediaPlayer.setOnPreparedListener(this);
   mediaPlayer.setOnCompletionListener(this);
   mediaPlayer.setOnErrorListener(this);
 }
Пример #17
0
 public void onCreate() {
   super.onCreate();
   position = 0;
   play = new MediaPlayer();
   play.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
   play.setAudioStreamType(AudioManager.STREAM_MUSIC);
   play.setOnPreparedListener(this);
   play.setOnErrorListener(this);
   play.setOnCompletionListener(this);
 }
Пример #18
0
 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);
 }
Пример #19
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;
    }
  }
Пример #20
0
 @Override
 public void onCreate() {
   super.onCreate();
   // Initializing the media player object
   mPlayer = new MediaPlayer();
   initPlayer();
   mPlayer.setOnPreparedListener(this);
   mPlayer.setOnCompletionListener(this);
   mPlayer.setOnErrorListener(this);
   notificationBuilder = new Notification.Builder(getApplicationContext());
 }
Пример #21
0
 @Override
 public void onCreate() {
   super.onCreate();
   JustMusicApplication app = (JustMusicApplication) getApplication();
   currentPosition = app.sp.getInt("currentPosition", 0);
   play_Mode = app.sp.getInt("play_Mode", ORDER_PLAY);
   mediaPlayer = new MediaPlayer();
   mediaPlayer.setOnCompletionListener(this);
   mediaPlayer.setOnErrorListener(this);
   mp3Infos = MediaUtil.getMp3Infos(this);
   es.execute(updateStatusRunnable);
 }
Пример #22
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);
   }
 }
Пример #23
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;
    }
  }
Пример #24
0
  private void setupMediaPlayerIfNeeded() {
    Logger.debug(TAG, ">>>" + "setupMediaPlayerIfNeeded");
    if (mediaPlayer == null) {
      mediaPlayer = new MediaPlayer();
      mediaPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);

      mediaPlayer.setOnPreparedListener(onPreparedListener);
      mediaPlayer.setOnCompletionListener(onCompletionListener);
      mediaPlayer.setOnErrorListener(onErrorListener);

    } else {
      mediaPlayer.reset();
    }
  }
  @Override
  public void onCreate() {
    super.onCreate();

    RocApplication app = (RocApplication) getApplication();
    app.getApplicationGraph().inject(this);

    bus.register(this);

    mSongs = Song.getAll();

    Log.d("service", "onCreate");

    // Uri uri =
    // Uri.parse("https://gargoyle.s3.amazonaws.com/rocnation/audio/01-What%20We%20Talkin%20About.mp3");
    // Uri uri = Uri.parse("https://gargoyle.s3.amazonaws.com/rocnation/audio/03-DOA.mp3");
    // mMediaPlayer = MediaPlayer.create(app, uri);
    mMediaPlayer = new MediaPlayer();

    mMediaPlayer.setOnErrorListener(this);
    mMediaPlayer.setOnCompletionListener(mOnCompletionListener);

    if (mMediaPlayer != null) {
      mMediaPlayer.setLooping(true);
      mMediaPlayer.setVolume(100, 100);
    }

    mMediaPlayer.setOnErrorListener(
        new OnErrorListener() {
          public boolean onError(MediaPlayer mp, int what, int extra) {
            MusicService.this.onError(mMediaPlayer, what, extra);
            return true;
          }
        });

    updateProgressBar();
  }
Пример #26
0
  protected void initialize() throws IOException {
    try {
      mp = new MediaPlayer();
      String url = TiConvert.toString(proxy.getProperty(TiC.PROPERTY_URL));
      if (URLUtil.isAssetUrl(url)) {
        Context context = proxy.getTiContext().getTiApp();
        String path = url.substring(TiConvert.ASSET_URL.length());
        AssetFileDescriptor afd = null;
        try {
          afd = context.getAssets().openFd(path);
          // Why mp.setDataSource(afd) doesn't work is a problem for another day.
          // http://groups.google.com/group/android-developers/browse_thread/thread/225c4c150be92416
          mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
        } catch (IOException e) {
          Log.e(LCAT, "Error setting file descriptor: ", e);
        } finally {
          if (afd != null) {
            afd.close();
          }
        }
      } else {
        Uri uri = Uri.parse(url);
        if (uri.getScheme().equals(TiC.PROPERTY_FILE)) {
          mp.setDataSource(uri.getPath());
        } else {
          remote = true;
          mp.setDataSource(url);
        }
      }

      mp.setLooping(looping);
      mp.setOnCompletionListener(this);
      mp.setOnErrorListener(this);
      mp.setOnInfoListener(this);
      mp.setOnBufferingUpdateListener(this);

      mp.prepare(); // Probably need to allow for Async
      setState(STATE_INITIALIZED);

      setVolume(volume);
      if (proxy.hasProperty(TiC.PROPERTY_TIME)) {
        setTime(TiConvert.toInt(proxy.getProperty(TiC.PROPERTY_TIME)));
      }
    } catch (Throwable t) {
      Log.w(LCAT, "Issue while initializing : ", t);
      release();
      setState(STATE_STOPPED);
    }
  }
  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);
  }
Пример #28
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;
    }
  }
Пример #29
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;
    }
  }
Пример #30
0
        public boolean onError(MediaPlayer mp, int framework_err, int impl_err) {
          Log.d(TAG, "Error: " + framework_err + "," + impl_err);
          mCurrentState = STATE_ERROR;
          mTargetState = STATE_ERROR;
          if (mMediaController != null) {
            mMediaController.hide();
          }

          /* If an error handler has been supplied, use it and finish. */
          if (mOnErrorListener != null) {
            if (mOnErrorListener.onError(mMediaPlayer, framework_err, impl_err)) {
              return true;
            }

            mOnErrorListener = null;
          }

          /* Otherwise, pop up an error dialog so the user knows that
           * something bad has happened. Only try and pop up the dialog
           * if we're attached to a window. When we're going away and no
           * longer have a window, don't bother showing the user an error.
           */
          if (getWindowToken() != null) {
            mMediaPlayer.setOnErrorListener(null);

            new AlertDialog.Builder(getContext())
                .setTitle(R.string.error_generic)
                .setMessage(R.string.loading_image_error)
                .setPositiveButton(
                    R.string.cancel,
                    new DialogInterface.OnClickListener() {
                      public void onClick(DialogInterface dialog, int whichButton) {
                        /* If we get here, there is no onError listener, so
                         * at least inform them that the video is over.
                         */
                        if (mOnCompletionListener != null) {
                          mOnCompletionListener.onCompletion(mMediaPlayer);
                        }
                      }
                    })
                .setCancelable(false)
                .show();
          }
          return true;
        }