Skip to content

Commit

Permalink
Current Status: Video Fragment Player functional with video streaming…
Browse files Browse the repository at this point in the history
… success

VideoFragment code updated with latest Exoplayer library classes
Internet Permissions added to manifest
Updated Exoplayer Libraries in gradle
  • Loading branch information
thatsabhi22 committed Apr 29, 2020
1 parent e98de5a commit 5d6a4a5
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 28 deletions.
4 changes: 3 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ dependencies {
implementation 'com.facebook.stetho:stetho-okhttp3:1.5.0'
implementation 'com.google.android.material:material:1.1.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.14.0'
implementation 'com.google.android.exoplayer:exoplayer:2.7.1'
implementation 'com.google.android.exoplayer:exoplayer-core:2.10.5'
implementation 'com.google.android.exoplayer:exoplayer-dash:2.10.5'
implementation 'com.google.android.exoplayer:exoplayer-ui:2.10.5'
implementation 'com.jakewharton:butterknife:10.2.1'

annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.1'
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.udacity.bakingapp">

<uses-permission android:name="android.permission.INTERNET"/>

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@
import androidx.fragment.app.Fragment;

import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.DefaultLoadControl;
import com.google.android.exoplayer2.ExoPlayerFactory;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.extractor.DefaultExtractorsFactory;
import com.google.android.exoplayer2.source.ExtractorMediaSource;
import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.source.ProgressiveMediaSource;
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector;
import com.google.android.exoplayer2.ui.SimpleExoPlayerView;
import com.google.android.exoplayer2.ui.PlayerView;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
import com.google.android.exoplayer2.util.Util;
import com.udacity.bakingapp.R;
Expand All @@ -45,7 +46,7 @@ public class VideoPlayerFragment extends Fragment {
TextView mStepTitle;

@BindView(R.id.player_view_tablet)
SimpleExoPlayerView mPlayerView;
PlayerView mPlayerView;

@BindView(R.id.step_description_tv)
TextView mStepDescription;
Expand All @@ -71,20 +72,19 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View root = inflater.inflate(R.layout.fragment_video_player, container, false);

ButterKnife.bind(this, root);

// Check if there is any state saved
if(savedInstanceState != null){
if (savedInstanceState != null) {
mStep = savedInstanceState.getParcelable(STEP_SINGLE);
mShouldPlayWhenReady = savedInstanceState.getBoolean(STEP_PLAY_WHEN_READY);
mPlayerPosition = savedInstanceState.getLong(STEP_VIDEO_POSITION);
mWindowIndex = savedInstanceState.getInt(STEP_PLAY_WINDOW_INDEX);
mVideoUri = Uri.parse(savedInstanceState.getString(STEP_URI));
}
// If there is no saved state getArguments from CookingActivity
else{
if(getArguments() != null){
else {
if (getArguments() != null) {

mImageViewPlaceholder.setVisibility(View.GONE);
mPlayerView.setVisibility(View.VISIBLE);
Expand All @@ -93,31 +93,27 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
mStep = getArguments().getParcelable(STEP_SINGLE);

// If has no video
if(mStep.getVideoURL().equals("")){
if (mStep.getVideoURL().equals("")) {
// Check thumbnail
if(mStep.getThumbnailURL().equals("")){
if (mStep.getThumbnailURL().equals("")) {
// If no video or thumbnail, use placeholder image
mPlayerView.setUseArtwork(true);
mImageViewPlaceholder.setVisibility(View.VISIBLE);
mPlayerView.setUseController(false);
}
else{
} else {
mImageViewPlaceholder.setVisibility(View.GONE);
mPlayerView.setVisibility(View.VISIBLE);
mVideoThumbnail = mStep.getThumbnailURL();
mVideoThumbnailImage = ThumbnailUtils.createVideoThumbnail(mVideoThumbnail, MediaStore.Video.Thumbnails.MICRO_KIND);
mPlayerView.setUseArtwork(true);
mPlayerView.setDefaultArtwork(mVideoThumbnailImage);
}
}
else{
} else {
mVideoUri = Uri.parse(mStep.getVideoURL());
}
}
}
return root;


}

public void initializeVideoPlayer(Uri videoUri) {
Expand All @@ -127,26 +123,20 @@ public void initializeVideoPlayer(Uri videoUri) {

if (mSimpleExoPlayer == null) {

mSimpleExoPlayer = ExoPlayerFactory.newSimpleInstance(getActivity(),
new DefaultTrackSelector(),
new DefaultLoadControl());
mSimpleExoPlayer = ExoPlayerFactory.newSimpleInstance(getActivity());

// Bind the player to the view.
mPlayerView.setPlayer(mSimpleExoPlayer);

// Prepare the MediaSource.
String userAgent = Util.getUserAgent(getActivity(), getString(R.string.app_name));
MediaSource mediaSource = new ExtractorMediaSource(videoUri,
new DefaultDataSourceFactory(getActivity(), userAgent),
new DefaultExtractorsFactory(),
null,
null);
MediaSource mediaSource = buildMediaSource(videoUri);

if (mPlayerPosition != C.TIME_UNSET) {
mSimpleExoPlayer.seekTo(mPlayerPosition);
}
// Prepare the player with the source.
mSimpleExoPlayer.prepare(mediaSource);
mSimpleExoPlayer.prepare(mediaSource,false,false);
mSimpleExoPlayer.setPlayWhenReady(mShouldPlayWhenReady);
}
}
Expand Down Expand Up @@ -210,4 +200,11 @@ private void updateStartPosition() {
mPlayerPosition = mSimpleExoPlayer.getCurrentPosition();
}
}

private MediaSource buildMediaSource(Uri uri) {
DataSource.Factory dataSourceFactory =
new DefaultDataSourceFactory(getActivity(), getString(R.string.app_name));
return new ProgressiveMediaSource.Factory(dataSourceFactory)
.createMediaSource(uri);
}
}
3 changes: 1 addition & 2 deletions app/src/main/res/layout-sw600dp/activity_recipe.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:layout_weight="15"
android:background="#FFCCAA" />
android:layout_weight="15"/>

</LinearLayout>
2 changes: 1 addition & 1 deletion app/src/main/res/layout-sw600dp/fragment_video_player.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
android:layout_height="match_parent"
tools:context=".fragments.VideoPlayerFragment">

<com.google.android.exoplayer2.ui.SimpleExoPlayerView
<com.google.android.exoplayer2.ui.PlayerView
android:id="@+id/player_view_tablet"
android:layout_width="0dp"
android:layout_height="0dp"
Expand Down

0 comments on commit 5d6a4a5

Please sign in to comment.