Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dependencies updated and support for install-time media playback added #3

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 16 additions & 18 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,21 @@ plugins {
}

android {
compileSdkVersion 30
buildToolsVersion "30.0.3"

defaultConfig {
applicationId "com.example.playassetdeliverydemo"
compileSdk 34
minSdkVersion 21
targetSdkVersion 30
versionCode 1
versionName "1.0"
targetSdkVersion 34
versionCode 8
versionName "1.8.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

}

buildTypes {
release {
debuggable false
minifyEnabled false
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
Expand All @@ -30,22 +27,23 @@ android {
}

assetPacks = [":install_time_asset_pack",":fast_follow_asset_pack",":on_demand_asset_pack"]
namespace 'com.example.playassetdeliverydemo'
}

dependencies {

implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.11.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'

implementation 'com.google.android.play:core:1.10.0'
implementation 'com.google.android.play:core:1.10.3'

implementation 'com.google.android.exoplayer:exoplayer-core:2.14.0'
implementation 'com.google.android.exoplayer:exoplayer-dash:2.14.0'
implementation 'com.google.android.exoplayer:exoplayer-ui:2.14.0'
implementation 'androidx.media3:media3-exoplayer:1.2.0'
implementation 'androidx.media3:media3-exoplayer-dash:1.2.0'
implementation 'androidx.media3:media3-ui:1.2.0'

implementation 'org.apache.commons:commons-io:1.3.2'
}
6 changes: 3 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.playassetdeliverydemo">
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Expand All @@ -13,7 +12,8 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.PlayAssetDeliveryDemo">
<activity android:name=".ui.MainActivity">
<activity android:name=".ui.MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
Expand All @@ -40,6 +41,7 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand All @@ -48,7 +50,8 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe

private AssetManager assetManager;
private InputStream inputStream = null;
private final String videoFileName = "BigBuckBunny_640x360.m4v";
private final String videoFileName = "sample_video.m4v";
private final String audioFileName = "audio.mp3";
private final int REQUEST_WRITE_PERMISSION = 111;
private MediaPlayer mPlayer;
private boolean isPaused = false;
Expand All @@ -63,10 +66,12 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
private AssetPackState assetPackState;
private boolean isFastFollow = false;
private boolean isOnDemand = false;
private Button btn_it;
private Button btn_ff;
private Button btn_od;
private Button btn_watch_video;
private Button btnPlayAudio;
private TextView txt_download_status;
private Context context;

@Override
Expand All @@ -82,11 +87,14 @@ protected void onCreate(Bundle savedInstanceState) {
*/
private void initViews() {
context = MainActivity.this;
btn_it = findViewById(R.id.btn_it);
btn_ff = findViewById(R.id.btn_ff);
btn_od = findViewById(R.id.btn_od);
txt_download_status = findViewById(R.id.txt_download_status);
btn_watch_video = findViewById(R.id.btn_watch_video);
btnPlayAudio = findViewById(R.id.btn_play_audio);

btn_it.setOnClickListener(this);
btn_ff.setOnClickListener(this);
btn_od.setOnClickListener(this);
btn_watch_video.setOnClickListener(this);
Expand All @@ -100,6 +108,11 @@ private void initViews() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_it: {
playInstallTimeVideo();
break;
}

case R.id.btn_ff: {
dialog.showProgresDialog();

Expand Down Expand Up @@ -146,6 +159,35 @@ private void initInstallTime() {
}
}

private void playInstallTimeVideo() {
try {
inputStream = assetManager.open(videoFileName);
File file = getFileFromAssets(this, videoFileName, inputStream);
if (file.exists()) {
playVideoInExoplayer(file);
}
} catch (IOException e) {
Log.e(TAG, "File doesn't exists!");
}
}

private File getFileFromAssets(Context context, String fileName, InputStream is) {
try {
File file = new File(context.getCacheDir(), fileName);
if (!file.exists()) {
OutputStream cache = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = is.read(buffer)) != -1) {
cache.write(buffer, 0, bytesRead);
}
}
return file;
} catch (IOException e) {
return null;
}
}

/**
* Checks for the Permission is granted or not
*/
Expand Down Expand Up @@ -238,7 +280,6 @@ void playVideoInExoplayer(File file) {
* This method will show Alert Dialog to play audio file
*/
private void showAlertDialog() {
String audioFileName = "aud_avatar_narration.m4a";
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.layout_audio_player, null);
Expand Down Expand Up @@ -313,9 +354,9 @@ private void initAssetPackManager() {
}
}

// private void checkDownloadSize() {
// getPackStates(fastFollowAssetPack);
// }
private void checkDownloadSize() {
getPackStates(fastFollowAssetPack);
}

/**
* This method will be triggered when the pack download size is more than 150MB
Expand Down Expand Up @@ -372,6 +413,7 @@ private void initFastFollow() {
if (assetsPath == null) {
getPackStates(fastFollowAssetPack);
}

if (assetsPath != null) {
File file = new File(assetsPath + File.separator + videoFileName);
if (file.exists()) {
Expand Down Expand Up @@ -418,13 +460,14 @@ public void onStateUpdate(AssetPackState state) {
switch (state.status()) {
case AssetPackStatus.PENDING:
Log.i(TAG, "Pending");
txt_download_status.setText("PENDING");
break;

case AssetPackStatus.DOWNLOADING:
long downloaded = state.bytesDownloaded();
long totalSize = state.totalBytesToDownload();
double percent = 100.0 * downloaded / totalSize;

txt_download_status.setText("DOWNLOADING: " + percent);
Log.i(TAG, "PercentDone=" + String.format("%.2f", percent));
break;

Expand All @@ -435,6 +478,7 @@ public void onStateUpdate(AssetPackState state) {

case AssetPackStatus.COMPLETED:
// Asset pack is ready to use. Start the Game/App.
txt_download_status.setText("COMPLETED");
initClickedAssetPack();
break;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@

import android.annotation.SuppressLint;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.media3.common.MediaItem;
import androidx.media3.exoplayer.ExoPlayer;
import androidx.media3.ui.PlayerView;

import com.example.playassetdeliverydemo.R;
import com.google.android.exoplayer2.MediaItem;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.ui.PlayerView;
import com.google.android.exoplayer2.util.Util;

public class VideoPlayerActivity extends AppCompatActivity {

private PlayerView playerView;
private SimpleExoPlayer player;
private ExoPlayer player;
private boolean playWhenReady = true;
private int currentWindow = 0;
private long playbackPosition = 0;
Expand All @@ -43,7 +43,7 @@ private void initViews() {
@Override
public void onStart() {
super.onStart();
if (Util.SDK_INT >= 24) {
if (Build.VERSION.SDK_INT >= 24) {
initializePlayer();
}
}
Expand All @@ -55,7 +55,7 @@ public void onStart() {
public void onResume() {
super.onResume();
hideSystemUi();
if ((Util.SDK_INT < 24 || player == null)) {
if ((Build.VERSION.SDK_INT < 24 || player == null)) {
initializePlayer();
}
}
Expand All @@ -66,7 +66,7 @@ public void onResume() {
@Override
public void onPause() {
super.onPause();
if (Util.SDK_INT < 24) {
if (Build.VERSION.SDK_INT < 24) {
releasePlayer();
}
}
Expand All @@ -77,7 +77,7 @@ public void onPause() {
@Override
public void onStop() {
super.onStop();
if (Util.SDK_INT >= 24) {
if (Build.VERSION.SDK_INT >= 24) {
releasePlayer();
}
}
Expand All @@ -87,7 +87,7 @@ public void onStop() {
*/
private void initializePlayer() {
if (player == null) {
player = new SimpleExoPlayer.Builder(this).build();
player = new ExoPlayer.Builder(this).build();
playerView.setPlayer(player);
MediaItem mediaItem = MediaItem.fromUri(uri);
player.setMediaItem(mediaItem);
Expand All @@ -101,7 +101,7 @@ private void releasePlayer() {
if (player != null) {
playWhenReady = player.getPlayWhenReady();
playbackPosition = player.getCurrentPosition();
currentWindow = player.getCurrentWindowIndex();
currentWindow = player.getCurrentMediaItemIndex();
player.release();
player = null;
}
Expand Down
13 changes: 13 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
android:layout_margin="10dp"
tools:context=".ui.MainActivity">

<Button
android:id="@+id/btn_it"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:text="Install-Time" />

<Button
android:id="@+id/btn_ff"
android:layout_width="wrap_content"
Expand Down Expand Up @@ -37,4 +44,10 @@
android:textAllCaps="false"
android:text="Play Audio" />

<TextView
android:id="@+id/txt_download_status"
android:layout_width="wrap_content"
android:text="ASSET STATUS"
android:layout_height="wrap_content" />

</LinearLayout>
5 changes: 2 additions & 3 deletions app/src/main/res/layout/activity_video_player.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.google.android.exoplayer2.ui.PlayerView
<androidx.media3.ui.PlayerView
android:id="@+id/playerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
android:layout_height="match_parent" />

</RelativeLayout>
3 changes: 1 addition & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:4.2.1"
classpath 'com.android.tools.build:gradle:8.2.1'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand All @@ -16,7 +16,6 @@ allprojects {
repositories {
google()
mavenCentral()
jcenter() // Warning: this repository is going to shut down soon
}
}

Expand Down
19 changes: 19 additions & 0 deletions bundletool_build_install_apks.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
DEBUG_BUNDLE_PATH="app/build/outputs/bundle/debug/app-debug.aab"
OUTPUT_FILE="output.apks"

export JAVA_HOME="/Applications/Android Studio.app/Contents/jbr/Contents/Home"

# Delete output file if exists
rm $OUTPUT_FILE

# Generate bundle
./gradlew :app:bundleDebug

# Build APKs from android bundle (.aab)
bundletool build-apks --bundle=$DEBUG_BUNDLE_PATH \--output=$OUTPUT_FILE --local-testing

# Get minimum and maximum size of an APK
bundletool get-size total --apks=$OUTPUT_FILE

# Install APKs to the connected device
bundletool install-apks --apks=$OUTPUT_FILE
Binary file not shown.
Binary file not shown.
Loading