Java Code Examples for android.media.MediaPlayer#setOnBufferingUpdateListener()

The following examples show how to use android.media.MediaPlayer#setOnBufferingUpdateListener() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MusicPlayer.java    From mobile-manager-tool with MIT License 6 votes vote down vote up
public MusicPlayer(Context context,SeekBar seekBar) {
    super();
    this.context = context;
    this.seekBar = seekBar;
    try {
        mediaPlayer = new MediaPlayer();
        mediaPlayer.setOnCompletionListener(this);
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);// 设置媒体流类型
        mediaPlayer.setOnBufferingUpdateListener(this);
        mediaPlayer.setOnPreparedListener(this);
    } catch (Exception e) {
        e.printStackTrace();
    }
    // 每一秒触发一次
    mTimer.schedule(timerTask, 0, 1000);
}
 
Example 2
Source File: VideoViewerActivity.java    From CameraV with GNU General Public License v3.0 6 votes vote down vote up
private void playVideo(Uri uri) {
doCleanUp();
try {


  // Create a new media player and set the listeners
  mMediaPlayer = new MediaPlayer();
  mMediaPlayer.setDataSource(this, uri);
  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);
}
}
 
Example 3
Source File: Player.java    From MediaPlayerProxy with Apache License 2.0 6 votes vote down vote up
public Player(SeekBar skbProgress) {
	this.skbProgress = skbProgress;

	try {
		mediaPlayer = new MediaPlayer();
		mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
		mediaPlayer.setOnBufferingUpdateListener(this);
		mediaPlayer.setOnPreparedListener(this);
	} catch (Exception e) {
		Log.e("mediaPlayer", "error", e);
	}

	mTimer.schedule(mTimerTask, 0, 1000);

	proxy = new MediaPlayerProxy();
	proxy.init();
	proxy.start();
}
 
Example 4
Source File: MusicPlayer.java    From SimplifyReader with Apache License 2.0 6 votes vote down vote up
private void initParameter(Context context) {
    mContext = context;

    mMediaPlayer = new MediaPlayer();

    mMediaPlayer.setOnErrorListener(this);
    mMediaPlayer.setOnPreparedListener(this);
    mMediaPlayer.setOnCompletionListener(this);
    mMediaPlayer.setOnBufferingUpdateListener(this);

    mMusicList = new ArrayList<MusicsListEntity>();

    mCurPlayIndex = -1;
    mPlayState = MusicPlayState.MPS_LIST_EMPTY;
    mPLayMode = MusicPlayMode.MPM_LIST_LOOP_PLAY;

    mRandom = new Random();
    mRandom.setSeed(System.currentTimeMillis());
}
 
Example 5
Source File: MediaPlayerImpl.java    From dcs-sdk-java with Apache License 2.0 6 votes vote down vote up
public MediaPlayerImpl() {
    mMediaPlayer = new MediaPlayer();
    // set audio stream type
    mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    mMediaPlayer.setOnBufferingUpdateListener(bufferingUpdateListener);
    mMediaPlayer.setOnErrorListener(errorListener);
    mMediaPlayer.setOnPreparedListener(preparedListener);
    mMediaPlayer.setOnCompletionListener(completionListener);
    mMediaPlayer.setOnSeekCompleteListener(seekCompleteListener);
    // 不同的音频源,此回调有的不回调!!!
    // mMediaPlayer.setOnInfoListener(infoListener);

    // 读取音量和静音的数据
    currentVolume = (float) MediaPlayerPreferenceUtil.get(context,
            KEY_SP_VOLUME, 0.8f);
    isMute = (boolean) MediaPlayerPreferenceUtil.get(context,
            KEY_SP_MUTE, false);
    // LinkedList
    mediaPlayerListeners = Collections.synchronizedList(new LinkedList<IMediaPlayer.IMediaPlayerListener>());
    posHandler = new PosHandler(Looper.getMainLooper());

    // 来电监听
    telephonyManager = (TelephonyManager)
            context.getSystemService(Service.TELEPHONY_SERVICE);
    telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}
 
Example 6
Source File: MediaPlayerActivity.java    From SendBird-Android with MIT License 6 votes vote down vote up
private void playVideo() {
    mProgressBar.setVisibility(View.VISIBLE);
    doCleanUp();
    try {
        mMediaPlayer = new MediaPlayer();
        mMediaPlayer.setDataSource(mUrl);
        mMediaPlayer.setDisplay(mSurfaceHolder);
        mMediaPlayer.prepareAsync();
        mMediaPlayer.setOnBufferingUpdateListener(this);
        mMediaPlayer.setOnCompletionListener(this);
        mMediaPlayer.setOnPreparedListener(this);
        mMediaPlayer.setOnVideoSizeChangedListener(this);
        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 7
Source File: PAudioPlayer.java    From PHONK with GNU General Public License v3.0 6 votes vote down vote up
public PAudioPlayer(AppRunner appRunner) {
    super(appRunner);
    getAppRunner().whatIsRunning.add(this);

    mMediaPlayer = new MediaPlayer();
    mMediaPlayer.setLooping(false);
    mMediaPlayer.setOnPreparedListener(mp -> {
        MLog.d(TAG, "prepared");

        if (callbackfn != null) {
            ReturnObject r = new ReturnObject();
            callbackfn.event(r);
        }
        //mMediaPlayer.start();

    });

    mMediaPlayer.setOnBufferingUpdateListener((mp, percent) -> MLog.d(TAG, "buffering: " + percent));

    mMediaPlayer.setOnCompletionListener(mp -> MLog.d(TAG, "completed"));

}
 
Example 8
Source File: MNViderPlayer.java    From MNVideoPlayer with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void surfaceCreated(SurfaceHolder holder) {
    mediaPlayer = new MediaPlayer();
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    mediaPlayer.setDisplay(holder); // 添加到容器中
    //播放完成的监听
    mediaPlayer.setOnCompletionListener(this);
    // 异步准备的一个监听函数,准备好了就调用里面的方法
    mediaPlayer.setOnPreparedListener(this);
    //播放错误的监听
    mediaPlayer.setOnErrorListener(this);
    mediaPlayer.setOnBufferingUpdateListener(this);
    //第一次初始化需不需要主动播放
    if (isFirstPlay) {
        playVideo(videoPath, videoTitle);
    }
    isFirstPlay = true;
}
 
Example 9
Source File: AndroidMediaPlayerFacade.java    From no-player with Apache License 2.0 6 votes vote down vote up
private MediaPlayer createAndBindMediaPlayer(Either<Surface, SurfaceHolder> surface,
                                             Uri videoUri) throws IOException, IllegalStateException, IllegalArgumentException {
    MediaPlayer mediaPlayer = mediaPlayerCreator.createMediaPlayer();
    mediaPlayer.setOnPreparedListener(internalPreparedListener);
    mediaPlayer.setOnVideoSizeChangedListener(internalSizeChangedListener);
    mediaPlayer.setOnCompletionListener(internalCompletionListener);
    mediaPlayer.setOnErrorListener(internalErrorListener);
    mediaPlayer.setOnBufferingUpdateListener(internalBufferingUpdateListener);
    mediaPlayer.setDataSource(context, videoUri, NO_HEADERS);
    attachSurface(mediaPlayer, surface);
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    mediaPlayer.setScreenOnWhilePlaying(true);

    currentBufferPercentage = 0;
    volume = 1.0f;

    return mediaPlayer;
}
 
Example 10
Source File: VideoView.java    From Spectaculum with Apache License 2.0 5 votes vote down vote up
private void openVideo() {
    if (mUri == null || mInputSurfaceHolder == null) {
        // not ready for playback yet, will be called again later
        return;
    }

    release();

    try {
        mPlayer = new MediaPlayer();
        mPlayer.setSurface(getInputHolder().getSurface());
        mPlayer.setOnPreparedListener(mPreparedListener);
        mPlayer.setOnVideoSizeChangedListener(mSizeChangedListener);
        mPlayer.setOnSeekCompleteListener(mSeekCompleteListener);
        mPlayer.setOnCompletionListener(mCompletionListener);
        mPlayer.setOnErrorListener(mErrorListener);
        mPlayer.setOnInfoListener(mInfoListener);
        mPlayer.setOnBufferingUpdateListener(mBufferingUpdateListener);
        mCurrentBufferPercentage = 0;
        mPlayer.setDataSource(getContext(), mUri, mHeaders);
        mCurrentState = STATE_PREPARING;
        mPlayer.prepareAsync();
    } catch (Exception e) {
        Log.e(TAG, "video open failed", e);
        mCurrentState = STATE_ERROR;
        mTargetState = STATE_ERROR;
        mErrorListener.onError(mPlayer, MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
    }
}
 
Example 11
Source File: VideoView.java    From twitter-kit-android with Apache License 2.0 5 votes vote down vote up
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();
        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;
        mMediaPlayer.setLooping(mLooping);
        mMediaPlayer.setDataSource(getContext(), mUri);
        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 (Exception ex) {
        Log.w(TAG, "Unable to open content: " + mUri, ex);
        mCurrentState = STATE_ERROR;
        mTargetState = STATE_ERROR;
        mErrorListener.onError(mMediaPlayer, MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
    }
}
 
Example 12
Source File: PanoMediaPlayerWrapper.java    From Pano360 with MIT License 5 votes vote down vote up
public PanoMediaPlayerWrapper() {
    mMediaPlayer=new MediaPlayer();
    mMediaPlayer.setOnPreparedListener(this);
    mMediaPlayer.setOnCompletionListener(this);
    mMediaPlayer.setOnErrorListener(this);
    mMediaPlayer.setOnVideoSizeChangedListener(this);
    mMediaPlayer.setOnInfoListener(this);
    mMediaPlayer.setOnBufferingUpdateListener(this);
}
 
Example 13
Source File: SystemImplMediaPlayer.java    From android-jungle-mediaplayer with Apache License 2.0 5 votes vote down vote up
private void init() {
    mMediaPlayer = new MediaPlayer();
    mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    mMediaPlayer.setOnErrorListener(mOnErrorListener);
    mMediaPlayer.setOnPreparedListener(mOnPreparedListener);
    mMediaPlayer.setOnCompletionListener(mOnCompletionListener);
    mMediaPlayer.setOnSeekCompleteListener(mOnSeekCompletionListener);
    mMediaPlayer.setOnBufferingUpdateListener(mOnBufferingUpdateListener);
    mMediaPlayer.setOnVideoSizeChangedListener(mOnVideoSizeChangedListener);
}
 
Example 14
Source File: MusicService.java    From mobile-manager-tool with MIT License 5 votes vote down vote up
@Override
public void onCreate() {
    super.onCreate();
    try {
        mediaPlayer = new MediaPlayer();
        mediaPlayer.setOnCompletionListener(this);
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);// 设置媒体流类型
        mediaPlayer.setOnBufferingUpdateListener(this);
        mediaPlayer.setOnPreparedListener(this);
    } catch (Exception e) {
        e.printStackTrace();
    }
    IntentFilter commandFilter = new IntentFilter();
    commandFilter.addAction(ActionManager.ACTION_MUSIC_PLAY);
    commandFilter.addAction(ActionManager.ACTION_MUSIC_PAUSE);
    commandFilter.addAction(ActionManager.ACTION_MUSIC_PREVIOUS_PLAY);
    commandFilter.addAction(ActionManager.ACTION_MUSIC_NEXT_PLAY);
    commandFilter.addAction(ActionManager.ACTION_MUSIC_STOP_TRACKING);
    commandFilter.addAction(ActionManager.ACTION_MUSIC_CLOSE_NOTIFICATION);

    registerReceiver(musicPlayReceiver, commandFilter);

    // 每一秒触发一次
    mTimer.schedule(timerTask, 0, 1000);

    mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    musicService = this;
}
 
Example 15
Source File: VideoViewTV.java    From letv with Apache License 2.0 5 votes vote down vote up
private void deadListener(MediaPlayer mMediaPlayer) {
    if (mMediaPlayer != null) {
        mMediaPlayer.setOnSeekCompleteListener(null);
        mMediaPlayer.setOnInfoListener(null);
        mMediaPlayer.setOnPreparedListener(null);
        mMediaPlayer.setOnVideoSizeChangedListener(null);
        mMediaPlayer.setOnCompletionListener(null);
        mMediaPlayer.setOnErrorListener(null);
        mMediaPlayer.setOnBufferingUpdateListener(null);
    }
}
 
Example 16
Source File: AndroidMediaPlayer.java    From DKVideoPlayer with Apache License 2.0 5 votes vote down vote up
@Override
public void initPlayer() {
    mMediaPlayer = new MediaPlayer();
    setOptions();
    mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    mMediaPlayer.setOnErrorListener(onErrorListener);
    mMediaPlayer.setOnCompletionListener(onCompletionListener);
    mMediaPlayer.setOnInfoListener(onInfoListener);
    mMediaPlayer.setOnBufferingUpdateListener(onBufferingUpdateListener);
    mMediaPlayer.setOnPreparedListener(onPreparedListener);
    mMediaPlayer.setOnVideoSizeChangedListener(onVideoSizeChangedListener);
}
 
Example 17
Source File: MusicPlayerManager.java    From NewFastFrame with Apache License 2.0 5 votes vote down vote up
private void initData() {
    mMediaPlayer = new MediaPlayer();
    mPlayData = new PlayData();
    mMediaPlayer.setOnPreparedListener(this);
    mMediaPlayer.setOnErrorListener(this);
    mMediaPlayer.setOnCompletionListener(this);
    mMediaPlayer.setOnBufferingUpdateListener(this);
}
 
Example 18
Source File: NativeVideoDelegate.java    From ExoMedia with Apache License 2.0 5 votes vote down vote up
protected void initMediaPlayer() {
    mediaPlayer = new MediaPlayer();

    mediaPlayer.setOnInfoListener(internalListeners);
    mediaPlayer.setOnErrorListener(internalListeners);
    mediaPlayer.setOnPreparedListener(internalListeners);
    mediaPlayer.setOnCompletionListener(internalListeners);
    mediaPlayer.setOnSeekCompleteListener(internalListeners);
    mediaPlayer.setOnBufferingUpdateListener(internalListeners);
    mediaPlayer.setOnVideoSizeChangedListener(internalListeners);

    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    mediaPlayer.setScreenOnWhilePlaying(true);
}
 
Example 19
Source File: UniversalVideoView.java    From LLApp with Apache License 2.0 4 votes vote down vote up
private void openVideo() {
    if (mUri == null || mSurfaceHolder == null) {
        // not ready for playback just yet, will try again later
        return;
    }
    AudioManager am = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
    am.requestAudioFocus(null, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);

    // 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(mInfoListener);
        mMediaPlayer.setOnBufferingUpdateListener(mBufferingUpdateListener);
        mCurrentBufferPercentage = 0;
        mMediaPlayer.setDataSource(mContext, mUri);
        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);
    }
}
 
Example 20
Source File: AndroidMediaPlayer.java    From K-Sonic with MIT License 4 votes vote down vote up
public AndroidMediaPlayer(com.aocate.media.MediaPlayer owningMediaPlayer, Context context) {
        super(owningMediaPlayer, context);

        mp = new MediaPlayer();

//		final ReentrantLock lock = new ReentrantLock();
//		Handler handler = new Handler(Looper.getMainLooper()) {
//            @Override
//            public void handleMessage(Message msg) {
//            	Log.d(AMP_TAG, "Instantiating new AndroidMediaPlayer from Handler");
//            	lock.lock();
//            	if (mp == null) {
//            		mp = new MediaPlayer();
//            	}
//            	lock.unlock();
//            }
//        };
//		
//        long endTime = System.currentTimeMillis() + TIMEOUT_DURATION_MS;
//        
//        while (true) {
//        	// Retry messages until mp isn't null or it's time to give up
//        	handler.sendMessage(handler.obtainMessage());
//        	if ((mp != null)
//        		|| (endTime < System.currentTimeMillis())) {
//        		break;
//        	}
//        	try {
//				Thread.sleep(50);
//			} catch (InterruptedException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//        }

        if (mp == null) {
            throw new IllegalStateException("Did not instantiate android.media.MediaPlayer successfully");
        }

        mp.setOnBufferingUpdateListener(this.onBufferingUpdateListener);
        mp.setOnCompletionListener(this.onCompletionListener);
        mp.setOnErrorListener(this.onErrorListener);
        mp.setOnInfoListener(this.onInfoListener);
        Log.d(AMP_TAG, " ++++++++++++++++++++++++++++++++ Setting prepared listener to this.onPreparedListener");
        mp.setOnPreparedListener(this.onPreparedListener);
        mp.setOnSeekCompleteListener(this.onSeekCompleteListener);
    }