List of usage examples for android.media MediaPlayer setOnPreparedListener
public void setOnPreparedListener(OnPreparedListener listener)
From source file:se.eliga.aves.songs.BirdSpeciesXenoCantoPlayerFragment.java
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { view = inflater.inflate(R.layout.audio_player_layout, container, false); setHasOptionsMenu(true);/* w ww .j a va 2 s .c om*/ mediaPlayer = new MediaPlayer(); mediaController = new MediaController(getActivity(), false); mediaController.setMediaPlayer(this); mediaController.setAnchorView(view); mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() { @Override public boolean onError(MediaPlayer mp, int what, int extra) { if (what == -38) { return true; //not pretty.. http://stackoverflow.com/questions/15205855/error-19-0-mediaplayer/15206308#15206308 } else { return false; } } }); mediaPlayer.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() { @Override public void onBufferingUpdate(MediaPlayer mp, int percent) { progressBar.setProgress(percent); } }); mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { handler.post(new Runnable() { public void run() { progressBar.setVisibility(ProgressBar.GONE); if (mediaController != null) { mediaController.show(0); } if (mediaPlayer != null) { try { mediaPlayer.start(); } catch (Exception e) { Log.d(TAG, "Could not start mediaplayer", e); } } } }); } }); mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mediaPlayer) { if (audioAutoNext.isChecked()) { int count = getListAdapter().getCount(); XenoCantoAudio nextAudio; if (currentPosition < (count - 1)) { nextAudio = (XenoCantoAudio) getListAdapter().getItem(++currentPosition); } else { nextAudio = (XenoCantoAudio) getListAdapter().getItem(0); } initiatePlay(nextAudio); } } }); loadBird(getCurrentBird()); return view; }
From source file:github.daneren2005.dsub.service.DownloadServiceImpl.java
private synchronized void doPlay(final DownloadFile downloadFile, final int position, final boolean start) { try {//from www . ja v a 2 s. c o m downloadFile.setPlaying(true); final File file = downloadFile.isCompleteFileAvailable() ? downloadFile.getCompleteFile() : downloadFile.getPartialFile(); isPartial = file.equals(downloadFile.getPartialFile()); downloadFile.updateModificationDate(); mediaPlayer.setOnCompletionListener(null); mediaPlayer.reset(); setPlayerState(IDLE); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); String dataSource = file.getPath(); if (isPartial) { if (proxy == null) { proxy = new StreamProxy(this); proxy.start(); } dataSource = String.format("http://127.0.0.1:%d/%s", proxy.getPort(), URLEncoder.encode(dataSource, Constants.UTF_8)); Log.i(TAG, "Data Source: " + dataSource); } else if (proxy != null) { proxy.stop(); proxy = null; } mediaPlayer.setDataSource(dataSource); setPlayerState(PREPARING); mediaPlayer.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() { public void onBufferingUpdate(MediaPlayer mp, int percent) { Log.i(TAG, "Buffered " + percent + "%"); if (percent == 100) { mediaPlayer.setOnBufferingUpdateListener(null); } } }); mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { public void onPrepared(MediaPlayer mediaPlayer) { try { setPlayerState(PREPARED); synchronized (DownloadServiceImpl.this) { if (position != 0) { Log.i(TAG, "Restarting player from position " + position); mediaPlayer.seekTo(position); } cachedPosition = position; if (start) { mediaPlayer.start(); setPlayerState(STARTED); } else { setPlayerState(PAUSED); } } lifecycleSupport.serializeDownloadQueue(); } catch (Exception x) { handleError(x); } } }); setupHandlers(downloadFile, isPartial); mediaPlayer.prepareAsync(); } catch (Exception x) { handleError(x); } }
From source file:com.shinymayhem.radiopresets.ServiceRadioPlayer.java
protected void initializePlayer(MediaPlayer player) { if (LOCAL_LOGV) log("initializePlayer()", "v"); player.setOnPreparedListener(this); player.setOnInfoListener(this); player.setOnCompletionListener(this); player.setOnErrorListener(this); //player.setOnBufferingUpdateListener(this); return;/*from w w w . j a v a 2 s . c o m*/ }
From source file:mp.teardrop.PlaybackService.java
/** * Returns a new MediaPlayer object// www. j a v a2 s . co m */ private MediaPlayer getNewMediaPlayer() { MediaPlayer mp = new MediaPlayer(); mp.setAudioStreamType(AudioManager.STREAM_MUSIC); mp.setOnCompletionListener(this); mp.setOnErrorListener(this); mp.setOnPreparedListener(this); return mp; }
From source file:github.daneren2005.dsub.service.DownloadService.java
private synchronized void doPlay(final DownloadFile downloadFile, final int position, final boolean start) { try {/*from ww w . j ava 2 s . co m*/ downloadFile.setPlaying(true); final File file = downloadFile.isCompleteFileAvailable() ? downloadFile.getCompleteFile() : downloadFile.getPartialFile(); boolean isPartial = file.equals(downloadFile.getPartialFile()); downloadFile.updateModificationDate(); subtractPosition = 0; mediaPlayer.setOnCompletionListener(null); mediaPlayer.setOnPreparedListener(null); mediaPlayer.setOnErrorListener(null); mediaPlayer.reset(); setPlayerState(IDLE); try { mediaPlayer.setAudioSessionId(audioSessionId); } catch (Throwable e) { mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); } String dataSource = file.getAbsolutePath(); if (isPartial && !Util.isOffline(this)) { if (proxy == null) { proxy = new BufferProxy(this); proxy.start(); } proxy.setBufferFile(downloadFile); dataSource = proxy.getPrivateAddress(dataSource); Log.i(TAG, "Data Source: " + dataSource); } else if (proxy != null) { proxy.stop(); proxy = null; } mediaPlayer.setDataSource(dataSource); setPlayerState(PREPARING); mediaPlayer.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() { public void onBufferingUpdate(MediaPlayer mp, int percent) { Log.i(TAG, "Buffered " + percent + "%"); if (percent == 100) { mediaPlayer.setOnBufferingUpdateListener(null); } } }); mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { public void onPrepared(MediaPlayer mediaPlayer) { try { setPlayerState(PREPARED); synchronized (DownloadService.this) { if (position != 0) { Log.i(TAG, "Restarting player from position " + position); mediaPlayer.seekTo(position); } cachedPosition = position; applyReplayGain(mediaPlayer, downloadFile); if (start || autoPlayStart) { mediaPlayer.start(); setPlayerState(STARTED); // Disable autoPlayStart after done autoPlayStart = false; } else { setPlayerState(PAUSED); onSongProgress(); } } // Only call when starting, setPlayerState(PAUSED) already calls this if (start) { lifecycleSupport.serializeDownloadQueue(); } } catch (Exception x) { handleError(x); } } }); setupHandlers(downloadFile, isPartial, start); mediaPlayer.prepareAsync(); } catch (Exception x) { handleError(x); } }
From source file:github.popeen.dsub.service.DownloadService.java
private synchronized void doPlay(final DownloadFile downloadFile, final int position, final boolean start) { try {/*from w w w . j a va 2 s . c o m*/ subtractPosition = 0; mediaPlayer.setOnCompletionListener(null); mediaPlayer.setOnPreparedListener(null); mediaPlayer.setOnErrorListener(null); mediaPlayer.reset(); setPlayerState(IDLE); try { mediaPlayer.setAudioSessionId(audioSessionId); } catch (Throwable e) { mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); } String dataSource; boolean isPartial = false; if (downloadFile.isStream()) { dataSource = downloadFile.getStream(); Log.i(TAG, "Data Source: " + dataSource); } else { downloadFile.setPlaying(true); final File file = downloadFile.isCompleteFileAvailable() ? downloadFile.getCompleteFile() : downloadFile.getPartialFile(); isPartial = file.equals(downloadFile.getPartialFile()); downloadFile.updateModificationDate(); dataSource = file.getAbsolutePath(); if (isPartial && !Util.isOffline(this)) { if (proxy == null) { proxy = new BufferProxy(this); proxy.start(); } proxy.setBufferFile(downloadFile); dataSource = proxy.getPrivateAddress(dataSource); Log.i(TAG, "Data Source: " + dataSource); } else if (proxy != null) { proxy.stop(); proxy = null; } } mediaPlayer.setDataSource(dataSource); setPlayerState(PREPARING); mediaPlayer.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() { public void onBufferingUpdate(MediaPlayer mp, int percent) { Log.i(TAG, "Buffered " + percent + "%"); if (percent == 100) { mediaPlayer.setOnBufferingUpdateListener(null); } } }); mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { public void onPrepared(MediaPlayer mediaPlayer) { try { setPlayerState(PREPARED); synchronized (DownloadService.this) { if (position != 0) { Log.i(TAG, "Restarting player from position " + position); mediaPlayer.seekTo(position); } cachedPosition = position; applyReplayGain(mediaPlayer, downloadFile); if (start || autoPlayStart) { mediaPlayer.start(); applyPlaybackParamsMain(); setPlayerState(STARTED); // Disable autoPlayStart after done autoPlayStart = false; } else { setPlayerState(PAUSED); onSongProgress(); } updateRemotePlaylist(); } // Only call when starting, setPlayerState(PAUSED) already calls this if (start) { lifecycleSupport.serializeDownloadQueue(); } } catch (Exception x) { handleError(x); } } }); setupHandlers(downloadFile, isPartial, start); mediaPlayer.prepareAsync(); } catch (Exception x) { handleError(x); } }