private void prepareToStart(Track dataSource) { try { if (dataSource.isVideo()) { mediaController.setMediaPlayer(mVideoView); mVideoView.setVisibility(VideoView.VISIBLE); mVideoView.setVideoPath(dataSource.getPath()); mVideoView.start(); } else { mediaPlayer.reset(); mVideoView.setVisibility(VideoView.INVISIBLE); mediaPlayer.setDataSource(dataSource.getPath()); mediaPlayer.prepare(); mediaPlayer.start(); } ((TextView) findViewById(R.id.now_playing_text)) .setText(currentPlaylist.getCurrent().getTitle()); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_avplayer); mVideoView = (VideoView) findViewById(R.id.videoView1); Intent i = getIntent(); trackList = i.getParcelableArrayListExtra(Track.klass); currentPlaylist = new Playlist(trackList, i.getIntExtra("pos", 0)); mediaPlayer = new MediaPlayer(); mediaPlayer.setOnPreparedListener(this); mediaController = new MediaController(this); mVideoView.setMediaController(mediaController); mVideoView.setVisibility(VideoView.INVISIBLE); // set controls for next and previous mediaController.setPrevNextListeners( new View.OnClickListener() { @Override public void onClick(View next) { prepareToStart(currentPlaylist.getNext()); } }, new View.OnClickListener() { @Override public void onClick(View prev) { prepareToStart(currentPlaylist.getPrevious()); } }); prepareToStart(currentPlaylist.getCurrent()); }