[Crash] : avoid crash when getFiles is empty

This commit is contained in:
Olivier Bouillet 2020-09-25 21:55:25 +02:00
parent 53fd6ed179
commit ae30ab708e
2 changed files with 34 additions and 22 deletions

View File

@ -243,6 +243,8 @@ public class VideoPlayerFragment extends Fragment implements VideoRendererEventL
Integer videoQuality = sharedPref.getInt(getString(R.string.pref_quality_key), 0); Integer videoQuality = sharedPref.getInt(getString(R.string.pref_quality_key), 0);
//get video qualities //get video qualities
/// #
if (video.getFiles().size() > 0) {
String urlToPlay = video.getFiles().get( 0 ).getFileUrl(); String urlToPlay = video.getFiles().get( 0 ).getFileUrl();
for ( File file : video.getFiles() ) { for ( File file : video.getFiles() ) {
// Set quality if it matches // Set quality if it matches
@ -251,9 +253,12 @@ public class VideoPlayerFragment extends Fragment implements VideoRendererEventL
} }
} }
mService.setCurrentStreamUrl( urlToPlay ); mService.setCurrentStreamUrl( urlToPlay );
torrentStatus.setVisibility(View.GONE); torrentStatus.setVisibility(View.GONE);
startPlayer(); startPlayer();
} else {
stopVideo();
Toast.makeText(context, R.string.api_error, Toast.LENGTH_LONG).show();
}
} }
Log.v(TAG, "end of load Video"); Log.v(TAG, "end of load Video");

View File

@ -27,6 +27,7 @@ import android.os.Build;
import android.os.Environment; import android.os.Environment;
import android.webkit.MimeTypeMap; import android.webkit.MimeTypeMap;
import android.webkit.URLUtil; import android.webkit.URLUtil;
import android.widget.Toast;
import com.github.se_bastiaan.torrentstream.TorrentOptions; import com.github.se_bastiaan.torrentstream.TorrentOptions;
@ -40,6 +41,7 @@ import androidx.core.app.ActivityCompat;
public class Intents { public class Intents {
private static final String TAG = "Intents";
/** /**
* https://troll.tv/videos/watch/6edbd9d1-e3c5-4a6c-8491-646e2020469c * https://troll.tv/videos/watch/6edbd9d1-e3c5-4a6c-8491-646e2020469c
@ -68,6 +70,8 @@ public class Intents {
// TODO, offer which version to download // TODO, offer which version to download
public static void Download(Context context, Video video) { public static void Download(Context context, Video video) {
if (video.getFiles().size() > 0)
{
String url = video.getFiles().get( 0 ).getFileDownloadUrl(); String url = video.getFiles().get( 0 ).getFileDownloadUrl();
// make sure it is a valid filename // make sure it is a valid filename
String destFilename = video.getName().replaceAll( "[^a-zA-Z0-9]", "_" ) + "." + MimeTypeMap.getFileExtensionFromUrl( URLUtil.guessFileName( url, null, null ) ); String destFilename = video.getName().replaceAll( "[^a-zA-Z0-9]", "_" ) + "." + MimeTypeMap.getFileExtensionFromUrl( URLUtil.guessFileName( url, null, null ) );
@ -83,5 +87,8 @@ public class Intents {
// get download service and enqueue file // get download service and enqueue file
DownloadManager manager = (DownloadManager) context.getSystemService( Context.DOWNLOAD_SERVICE ); DownloadManager manager = (DownloadManager) context.getSystemService( Context.DOWNLOAD_SERVICE );
manager.enqueue( request ); manager.enqueue( request );
} else {
Toast.makeText( context, R.string.api_error, Toast.LENGTH_LONG ).show();
}
} }
} }