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

The following examples show how to use android.media.MediaPlayer#setOnPreparedListener() . 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: LiveStuffH4.java    From goprohero with MIT License 6 votes vote down vote up
@Override
 public void surfaceCreated(SurfaceHolder sh) {
     _mediaPlayer = new MediaPlayer();
     _mediaPlayer.setDisplay(_surfaceHolder);

     Context context = getApplicationContext();
//     Map<String, String> headers = getRtspHeaders();
     Uri source = Uri.parse(RTSP_URL);

     try {
         // Specify the IP camera's URL and auth headers.
      //   _mediaPlayer.setDataSource(context, source, headers);

         // Begin the process of setting up a video stream.
         _mediaPlayer.setOnPreparedListener(this);
         _mediaPlayer.prepareAsync();
     }
     catch (Exception e) {}
 }
 
Example 2
Source File: PreviewFragment.java    From kolabnotes-android with GNU Lesser General Public License v3.0 6 votes vote down vote up
void displayAudio(ActiveAccount account, String noteUID,Attachment attachment){
    musicView.setVisibility(View.VISIBLE);

    try {
        nowPlayingView.setText(attachment.getFileName());

        mediaPlayer = new MediaPlayer();
        mediaPlayer.setOnPreparedListener(this);

        mediaController = new MediaController(getActivity());

        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mediaPlayer.setDataSource(getActivity(), attachmentRepository.getUriFromAttachment(account.getAccount(), account.getRootFolder(), noteUID, attachment));
        mediaPlayer.prepare();

        mediaController.show(0);
    }catch (IOException e){
        Toast.makeText(getActivity(), R.string.attachment_not_previewable, Toast.LENGTH_LONG).show();
    }

}
 
Example 3
Source File: VideoPlayerFragment.java    From Aria with Apache License 2.0 6 votes vote down vote up
private NextMediaPlayer(String videoPath) {
  player = new MediaPlayer();
  player.setAudioStreamType(AudioManager.STREAM_MUSIC);
  player.setOnPreparedListener(this);
  player.setOnErrorListener(this);
  player.setOnCompletionListener(this);
  player.setScreenOnWhilePlaying(true);
  this.videoPath = videoPath;
  player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
    @Override public void onPrepared(MediaPlayer mp) {
      if (listener != null) {
        listener.onPrepared(mp);
      }
    }
  });
}
 
Example 4
Source File: PlaybackFragment.java    From Android-AudioRecorder-App with Apache License 2.0 6 votes vote down vote up
private void startPlaying() {
  mPlayButton.setImageResource(R.drawable.ic_media_pause);
  mMediaPlayer = new MediaPlayer();

  try {
    mMediaPlayer.setDataSource(item.getFilePath());
    mMediaPlayer.prepare();
    mSeekBar.setMax(mMediaPlayer.getDuration());

    mMediaPlayer.setOnPreparedListener(mp -> mMediaPlayer.start());
  } catch (IOException e) {
    Log.e(LOG_TAG, "prepare() failed");
  }

  mMediaPlayer.setOnCompletionListener(mp -> stopPlaying());

  updateSeekBar();

  //keep screen on while playing audio
  getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
 
Example 5
Source File: UserProfileActivity.java    From styT with Apache License 2.0 6 votes vote down vote up
public void Bi(String object) {
    final MediaPlayer mediaPlayer = new MediaPlayer();
    try {
        mediaPlayer.setDataSource(object);
        mediaPlayer.prepareAsync();
        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                // 准备完成 开始播放
                mediaPlayer.start();
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
    }
    //MediaPlayer mPlayer = MediaPlayer.create(this, object);
    // mPlayer.start();
}
 
Example 6
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 7
Source File: EditVideoActivity.java    From WeiXinRecordedDemo with MIT License 6 votes vote down vote up
private void initMediaPlay(SurfaceTexture surface){

        try {
            mMediaPlayer = new MediaPlayer();
            mMediaPlayer.setDataSource(path);
            mMediaPlayer.setSurface(new Surface(surface));
            mMediaPlayer.setLooping(true);
            mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mMediaPlayer.start();
                }
            });
            mMediaPlayer.prepareAsync();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
 
Example 8
Source File: SoundRecordingFragment.java    From AlbumCameraRecorder with MIT License 6 votes vote down vote up
/**
 * 播放MediaPlayer
 */
private void startPlaying() {
    // 变成等待的图标
    ((SoundrecordingLayout.ViewHolder) mViewHolder.pvLayout.mViewHolder).iv_record.setImageResource(R.drawable.ic_pause_white_24dp);
    mMediaPlayer = new MediaPlayer();

    try {
        // 文件地址
        mMediaPlayer.setDataSource(recordingItem.getFilePath());
        mMediaPlayer.prepare();

        mMediaPlayer.setOnPreparedListener(mp -> mMediaPlayer.start());
    } catch (IOException e) {
        Log.e(LOG_TAG, "prepare() failed");
    }

    mMediaPlayer.setOnCompletionListener(mp -> stopPlaying());

    //keep screen on while playing audio
    mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
 
Example 9
Source File: MediaPlayerActivity.java    From KSYMediaPlayer_Android with Apache License 2.0 6 votes vote down vote up
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mContext = getApplicationContext();

    setContentView(R.layout.activity_media_player);

    mDataSource = getIntent().getStringExtra("path");

    mTextureView = (TextureView) findViewById(R.id.media_player_view);
    mTextureView.setSurfaceTextureListener(mSurfaceTextureListener);

    mMediaPlayer = new MediaPlayer();
    mMediaPlayer.setOnErrorListener(mOnErrorListener);
    mMediaPlayer.setOnPreparedListener(mOnPreparedListener);
    mMediaPlayer.setOnCompletionListener(mOnCompletionListener);

    try {
        mMediaPlayer.setDataSource(mDataSource);
        mMediaPlayer.prepareAsync();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example 10
Source File: ChatInputView.java    From aurora-imui with MIT License 6 votes vote down vote up
private void playVideo() {
    try {
        mCameraSupport.release();
        mMediaPlayer = new MediaPlayer();
        mMediaPlayer.setDataSource(mVideoFilePath);
        Surface surface = new Surface(mTextureView.getSurfaceTexture());
        mMediaPlayer.setSurface(surface);
        surface.release();
        mMediaPlayer.setLooping(true);
        mMediaPlayer.prepareAsync();
        mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {
                mediaPlayer.start();
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 11
Source File: VideoPlayerActivity.java    From ClockView with Apache License 2.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_video);
    mSurfaceView = (SurfaceView) findViewById(R.id.surface_view);
    play = (ImageView) findViewById(R.id.play);
    time = (TextView) findViewById(R.id.time);
    mMediaPlayer = new MediaPlayer();
    mSurfaceView.getHolder().addCallback(this);
    try {
        mMediaPlayer.setDataSource("http://live.hkstv.hk.lxdns.com/live/hks/playlist.m3u8");
        mMediaPlayer.prepareAsync();
    } catch (IOException e) {
        e.printStackTrace();
    }
    mSurfaceView.getHolder().setKeepScreenOn(true);
    play.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (!mMediaPlayer.isPlaying()){
                mMediaPlayer.start();
            }
        }
    });
    mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            time.setText(mp.getDuration()+"");
            mMediaPlayer.start();
        }
    });

}
 
Example 12
Source File: VideoPlayTextureView.java    From VideoRecorder with Apache License 2.0 5 votes vote down vote up
/**
 * 初始化数据
 * @param context
 */
private void init(Context context) {
	mediaPlayer = new MediaPlayer();
	setSurfaceTextureListener(this);
	mediaPlayer.setOnCompletionListener(this);
	mediaPlayer.setOnPreparedListener(this);
}
 
Example 13
Source File: MyAdapter.java    From Study_Android_Demo with Apache License 2.0 5 votes vote down vote up
public MyAdapter(List<VideoBean> list, Context context) {
    this.list = list;
    this.context = context;
    inflater = LayoutInflater.from(context);
    player = new MediaPlayer();
    player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            mp.start();
        }
    });
}
 
Example 14
Source File: MultiPlayer.java    From Music-Player with GNU General Public License v3.0 5 votes vote down vote up
/**
 * @param player The {@link MediaPlayer} to use
 * @param path   The path of the file, or the http/rtsp URL of the stream
 *               you want to play
 * @return True if the <code>player</code> has been prepared and is
 * ready to play, false otherwise
 */
private boolean setDataSourceImpl(@NonNull final MediaPlayer player, @NonNull final String path) {
    if (context == null) {
        return false;
    }
    try {
        player.reset();
        player.setOnPreparedListener(null);
        if (path.startsWith("content://")) {
            player.setDataSource(context, Uri.parse(path));
        } else {
            player.setDataSource(path);
        }
        player.setAudioStreamType(AudioManager.STREAM_MUSIC);
        player.prepare();
    } catch (Exception e) {
        return false;
    }
    player.setOnCompletionListener(this);
    player.setOnErrorListener(this);
    final Intent intent = new Intent(AudioEffect.ACTION_OPEN_AUDIO_EFFECT_CONTROL_SESSION);
    intent.putExtra(AudioEffect.EXTRA_AUDIO_SESSION, getAudioSessionId());
    intent.putExtra(AudioEffect.EXTRA_PACKAGE_NAME, context.getPackageName());
    intent.putExtra(AudioEffect.EXTRA_CONTENT_TYPE, AudioEffect.CONTENT_TYPE_MUSIC);
    context.sendBroadcast(intent);
    return true;
}
 
Example 15
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 16
Source File: VideoPlayTextureView.java    From FFmpegRecorder with GNU General Public License v3.0 5 votes vote down vote up
/**
 * ��ʼ������
 * @param context
 */
private void init(Context context) {
	mediaPlayer = new MediaPlayer();
	setSurfaceTextureListener(this);
	mediaPlayer.setOnCompletionListener(this);
	mediaPlayer.setOnPreparedListener(this);
}
 
Example 17
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 18
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 19
Source File: VoIPBaseService.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
protected void startRingtoneAndVibration(int chatID){
	SharedPreferences prefs = MessagesController.getNotificationsSettings(currentAccount);
	AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
	boolean needRing=am.getRingerMode()!=AudioManager.RINGER_MODE_SILENT;
	/*if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP){
		try{
			int mode=Settings.Global.getInt(getContentResolver(), "zen_mode");
			if(needRing)
				needRing=mode==0;
		}catch(Exception ignore){}
	}*/
	if(needRing){
		if(!USE_CONNECTION_SERVICE){
			am.requestAudioFocus(this, AudioManager.STREAM_RING, AudioManager.AUDIOFOCUS_GAIN);
		}
		ringtonePlayer=new MediaPlayer();
		ringtonePlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener(){
			@Override
			public void onPrepared(MediaPlayer mediaPlayer){
				ringtonePlayer.start();
			}
		});
		ringtonePlayer.setLooping(true);
		ringtonePlayer.setAudioStreamType(AudioManager.STREAM_RING);
		try{
			String notificationUri;
			if(prefs.getBoolean("custom_"+chatID, false))
				notificationUri=prefs.getString("ringtone_path_"+chatID, RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE).toString());
			else
				notificationUri=prefs.getString("CallsRingtonePath", RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE).toString());
			ringtonePlayer.setDataSource(this, Uri.parse(notificationUri));
			ringtonePlayer.prepareAsync();
		}catch(Exception e){
			FileLog.e(e);
			if(ringtonePlayer!=null){
				ringtonePlayer.release();
				ringtonePlayer=null;
			}
		}
		int vibrate;
		if(prefs.getBoolean("custom_"+chatID, false))
			vibrate=prefs.getInt("calls_vibrate_"+chatID, 0);
		else
			vibrate=prefs.getInt("vibrate_calls", 0);
		if((vibrate!=2 && vibrate!=4 && (am.getRingerMode()==AudioManager.RINGER_MODE_VIBRATE || am.getRingerMode()==AudioManager.RINGER_MODE_NORMAL)) ||
				(vibrate==4 && am.getRingerMode()==AudioManager.RINGER_MODE_VIBRATE)){
			vibrator=(Vibrator) getSystemService(VIBRATOR_SERVICE);
			long duration=700;
			if(vibrate==1)
				duration/=2;
			else if(vibrate==3)
				duration*=2;
			vibrator.vibrate(new long[]{0, duration, 500}, 0);
		}
	}
}
 
Example 20
Source File: VoIPBaseService.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
protected void startRingtoneAndVibration(int chatID){
	SharedPreferences prefs = MessagesController.getNotificationsSettings(currentAccount);
	AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
	boolean needRing=am.getRingerMode()!=AudioManager.RINGER_MODE_SILENT;
	if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP){
		try{
			int mode=Settings.Global.getInt(getContentResolver(), "zen_mode");
			if(needRing)
				needRing=mode==0;
		}catch(Exception ignore){}
	}
	if(needRing){
		if(!USE_CONNECTION_SERVICE){
			am.requestAudioFocus(this, AudioManager.STREAM_RING, AudioManager.AUDIOFOCUS_GAIN);
		}
		ringtonePlayer=new MediaPlayer();
		ringtonePlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener(){
			@Override
			public void onPrepared(MediaPlayer mediaPlayer){
				ringtonePlayer.start();
			}
		});
		ringtonePlayer.setLooping(true);
		ringtonePlayer.setAudioStreamType(AudioManager.STREAM_RING);
		try{
			String notificationUri;
			if(prefs.getBoolean("custom_"+chatID, false))
				notificationUri=prefs.getString("ringtone_path_"+chatID, RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE).toString());
			else
				notificationUri=prefs.getString("CallsRingtonePath", RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE).toString());
			ringtonePlayer.setDataSource(this, Uri.parse(notificationUri));
			ringtonePlayer.prepareAsync();
		}catch(Exception e){
			FileLog.e(e);
			if(ringtonePlayer!=null){
				ringtonePlayer.release();
				ringtonePlayer=null;
			}
		}
		int vibrate;
		if(prefs.getBoolean("custom_"+chatID, false))
			vibrate=prefs.getInt("calls_vibrate_"+chatID, 0);
		else
			vibrate=prefs.getInt("vibrate_calls", 0);
		if((vibrate!=2 && vibrate!=4 && (am.getRingerMode()==AudioManager.RINGER_MODE_VIBRATE || am.getRingerMode()==AudioManager.RINGER_MODE_NORMAL)) ||
				(vibrate==4 && am.getRingerMode()==AudioManager.RINGER_MODE_VIBRATE)){
			vibrator=(Vibrator) getSystemService(VIBRATOR_SERVICE);
			long duration=700;
			if(vibrate==1)
				duration/=2;
			else if(vibrate==3)
				duration*=2;
			vibrator.vibrate(new long[]{0, duration, 500}, 0);
		}
	}
}