JavaFX mp3 audio, mp4 audio, text Data superimposing - java

I am developing an application that finds files in different folder locations and displays all the files in a table view. If a user clicks on a row then that file gets displayed on the right side of the program. Currently, it is kind of working for txt, mp3 files and mp4 files.
DESCRIPTION:
I have created two services, FileSearchService and FilePreviewService. The FileSearchService searches for files and add those in a static observableList. The FilePreviewService gets the selected row, checks if the file is of type txt or mp3 or other and uses a FileProcessor abstract class to process the selected item. If the item selected is of type txt, the child class of FileProcessor, TextProcessor comes into play and returns an AnchorPane with TextArea as a child attached to that AnchorPane. The textarea sets the text obtained from the selected item(row). Finally the anchorPane is returned to the main controller. The Main Controller then displays the item.
Problem:
If i click on a row that holds a mp4 (or mp3) file then the mp4 (or mp3) file plays fine and gets displayed on my anchorPane. While the mp4 (or mp3) is playing, if i click on a txt file then the data of that file gets displayed on anchorPane but the audio of mp4(or mp3) is still playing.
Below are two images which describes my problem.
I clicked on a video file, video displays at the right side of my application.
Now i click on a txt file, then the anchorPane shows textData but the video(rather audio) is still playing.
Now, if i click a mp3 file then both mp3 and mp4 audios superimpose.
What I want
I want only one item to get executed. If a mp3 file is being played and if i click on a mp4 video then the mp3 should stop and video should play.My application can handle multiple consecutive mp3 or mp4 or txt clicks. Clicking a mp4 file followed by a txt file click does not get handled.
FilePreviewService.Java
public class FilePreviewService extends Service<Void> {
FileModel model;
private FileProcesser fileProcesser;
String fileExtension = "";
public FileProcesser getFileProcesser() {
return fileProcesser;
}
public FilePreviewService(FileModel model) {
this.model = model;
this.fileExtension = reverseFileName(getFileExtension(model));
}
#Override
protected Task<Void> createTask() {
return new Task<Void>() {
#Override
protected Void call() throws Exception {
System.out.println("FIlePreviewService, createTask() Thread = " + Thread.currentThread().getName());
fileProcesser = FileUtility.getFileProcesserFromUtil(fileExtension);
getFileProcesser().processFile(model.getFileLocation());
return null;
}
};
}
}
AudioProcesser.Java
public final class AudioProcesser extends FileProcesser{
static AudioProcesser audioProcesser;
Media media;
MediaPlayer mediaPlayer;
public static AudioProcesser getAudioProcesser() {
if (audioProcesser == null)
audioProcesser = new AudioProcesser();
return audioProcesser;
}
#Override
public void processFile(String fileLocation) throws Exception {
switch (getAudioMediaStatus()) {
case NOT_PLAYED:
playMedia(fileLocation);
break;
case PLAYING:
/* TIP:
If mediaPlayer.stop is placed after the line
* media = new Media(new File(fileLocation).toURI().toString());
mediaPlayer = new MediaPlayer(media);
then multiple music play together when multiple different row gets selected,
one after another
*/
mediaPlayer.stop();
playMedia(fileLocation);
break;
default:
System.out.println("Audio in default case");
}
}
private void playMedia(String fileLocation) {
media = new Media(new File(fileLocation).toURI().toString());
mediaPlayer = new MediaPlayer(media);
mediaPlayer.play();
setAudioMediaStatus(PLAYING);
}
}
VideoProcesser.Java
public class VideoProcesser extends FileProcesser {
static VideoProcesser videoProcesser = null;
MediaPlayer mediaPlayer;
Media media;
MediaView mediaView;
#Override
public void processFile(String fileLocation) throws Exception {
switch (getVideoMediaStatus()) {
case NOT_PLAYED:
playVideo(fileLocation);
break;
case PLAYING:
mediaPlayer.stop();
playVideo(fileLocation);
break;
default:
System.out.println("Audio in default case");
}
// pane.getChildren().add();
}
#Override
public AnchorPane getPane(){
return pane;
}
private void playVideo(String fileLocation) {
System.out.println("VideoProcesser Thread = " + Thread.currentThread().getName());
media = new Media(new File(fileLocation).toURI().toString());
mediaPlayer = new MediaPlayer(media);
// mediaPlayer.setAutoPlay(true);
if(mediaView == null) {
mediaView = new MediaView(mediaPlayer);
}
mediaView.setMediaPlayer(mediaPlayer);
mediaView.setPreserveRatio(true);
mediaPlayer.play();
mediaPlayer.setOnError(() -> System.out.println("Current error: "+mediaPlayer.getError()));
setVideoMediaStatus(PLAYING);
pane.getChildren().add(mediaView);
}
public static FileProcesser getVideoProcesser() {
if(videoProcesser == null)
videoProcesser = new VideoProcesser();
return videoProcesser;
}
}
TextProcesser.Java
public class TextProcesser extends FileProcesser {
static TextProcesser textProcesser = null;
public static FileProcesser getTextProcesser() {
if(textProcesser == null)
textProcesser = new TextProcesser();
return textProcesser;
}
#Override
public void processFile(String fileLocation) throws IOException {
InputStream inputStream = new FileInputStream(fileLocation);
Scanner sc = null;
//text file: 25.1_-_Marvel_Graph.txt, size 1.5MB
System.out.println("Data reading started = " + new Date());
if (inputStream != null) {
StringBuilder txtData = new StringBuilder("");
try {
sc = new Scanner(inputStream, "UTF-8");
while (sc.hasNextLine()) {
txtData.append(sc.nextLine());
}
// note that Scanner suppresses exceptions
if (sc.ioException() != null) {
throw sc.ioException();
}
} finally {
if (inputStream != null) {
inputStream.close();
}
if (sc != null) {
sc.close();
}
}
dataToDisplay = txtData.toString();
}
System.out.println("Data reading finished = " + new Date());
}
#Override
public AnchorPane getPane(){
TextArea txtArea = new TextArea();
txtArea.setEditable(false);
txtArea.setText((String) dataToDisplay);
txtArea.setPrefHeight(778);
txtArea.setWrapText(true);
pane.getChildren().add(txtArea);
return pane;
}
}
MainController.Java
public void initialize(URL location, ResourceBundle resources) {
init();
initFilePreviewExecutors();
for (int i = 0; i < locationsToSearch.length; i++) {
fileModel = new FileModel(locationsToSearch[i]);
FileSearchService fileSearchService = new FileSearchService(fileModel);
fileSearchService.setExecutor(fileSearchExecutor);
fileSearchService.setOnSucceeded(e -> {
fileSearchService.setFileSearchCompleted(true);
searchFinished = true;
});
fileSearchService.start();
CacheFileService cfs = new CacheFileService(locationsToSearch[i]);
}
try {
stop();
} catch (InterruptedException e) {
e.printStackTrace();
}
nameCol.setCellValueFactory(new PropertyValueFactory<>("fileName"));
sizeCol.setCellValueFactory(new PropertyValueFactory<>("fileSize"));
locationCol.setCellValueFactory(new PropertyValueFactory<>("fileLocation"));
recordsTableView.setItems(fileModel.getData());
recordsTableView.setContextMenu(new ContextMenu(showRecordInfo));
recordsTableView.setRowFactory(tv -> {
TableRow<FileModel> row = new TableRow<>();
row.setOnMouseClicked(event -> {
if(!row.isEmpty() && event.getButton() == MouseButton.PRIMARY && event.getClickCount() == 1) {
FileModel fileModel = row.getItem();
filePreviewService = new FilePreviewService(fileModel);
filePreviewService.setExecutor(filePreviewExecutor);
filePreviewService.setOnSucceeded(event1 -> {
recordPreviewPane = filePreviewService.getFileProcesser().getPane();
if(recordPreviewPane == null) {
System.out.println("RECORDPREVIEWPANE IS NULL");
}
previewPane.setContent(recordPreviewPane);
});
filePreviewService.restart();
} else if(!row.isEmpty() && event.getButton() == MouseButton.SECONDARY) {
FileModel fileModel = row.getItem();
showRecordInfo.setOnAction( e -> {
Scene scene = defaultViewFactory.getRecordInfoScene(fileModel);
Stage stage = new Stage();
stage.setScene(scene);
stage.show();
});
}
});
return row;});
}
I hope i made my question clear. In case if you want to see other java file then below is a link to the project.
Github :
Edit: The way i am solving my problem is very inefficient. I added some more code in FilePreviewService. Ofcourse the below code needs to satisfy some more condition. It's just a very inefficient approach.
getFileProcesser().processFile(model.getFileLocation());
if(i > 0) {
oldFileProcesser = fileProcesserStack.pop();
}
if(fileProcesser != null) {
fileProcesserStack.push(fileProcesser);
}
//audio and video consecutive play fixing
if(i > 0 && (oldFileProcesser instanceof AudioProcesser || oldFileProcesser instanceof VideoProcesser)
&& !(fileProcesser instanceof AudioProcesser || fileProcesser instanceof VideoProcesser)) {
if(oldFileProcesser instanceof AudioProcesser) {
AudioMediaStatus.setAudioMediaStatus(AudioMediaStatus.JUST_STOP); }
if(oldFileProcesser instanceof VideoProcesser) {
VideoMediaStatus.setVideoMediaStatus(VideoMediaStatus.JUST_STOP);}
oldFileProcesser.processFile("");
}
i++;

If I understood correctly, you are trying to stop a .mp3 when a .mp4 is played, or vice-versa.
First the why of your problem. You are calling AudioProcesser when you start an .mp3, cool, great. So you start a new MediaPlayer through that class.
BUT, you do the same thing using the VideoProcesser class. So now you have two MediaPlayers running at the same time and thats why the audio overlays.
The solution, have an instance variable and expose some new methods for other classes to call, namely a stopMediaPlayer() method.
Example below, with as little changes to your code as possible:
public final class AudioProcesser extends FileProcesser{
//Always have one instance of the variable.
static AudioProcesser audioProcesser = new AudioProcesser();
private Media media;
private MediaPlayer mediaPlayer;
public static AudioProcesser getAudioProcesser() {
return audioProcesser;
}
//Added an exposure to the underlying audioMediaStatus
public void setAudioMediaStatus(AudioMediaStatus status){
AudioMediaStatus.setAudioMediaStatus(status);
}
//Another exposure to the underlying audioMediaStatus
public AudioMediaStatus getAudioMediaStatus(){
return AudioMediaStatus.getAudioMediaStatus();
}
//Used only for this class
private void setMediaPlayer(String fileLocation){
Media media = new Media(new File(fileLocation).toURI().toString());
mediaPlayer = new MediaPlayer(media);
}
//Exposed a stop method.
public void stopMedia(){
if(mediaPlayer != null) {
//Change this accordingly.
setAudioMediaStatus(AudioMediaStatus.NOT_PLAYED);
mediaPlayer.stop();
}
}
#Override
public void processFile(String fileLocation) throws Exception {
switch (getAudioMediaStatus()) {
case NOT_PLAYED:
playMedia(fileLocation);
break;
case PLAYING:
/* TIP:
If mediaPlayer.stop is placed after the line
* media = new Media(new File(fileLocation).toURI().toString());
mediaPlayer = new MediaPlayer(media);
then multiple music play together when multiple different row gets selected,
one after another
*/
mediaPlayer.stop();
playMedia(fileLocation);
break;
default:
System.out.println("Audio in default case");
}
}
private void playMedia(String fileLocation) {
VideoProcesser.getVideoProcesser().stopMedia();
//Moved the previous statements to its own method.
setMediaPlayer(fileLocation);
mediaPlayer.play();
setAudioMediaStatus(AudioMediaStatus.PLAYING);
}
}
As you can see I've added a few items, namely some methods and I got rid of your static imports. The important things to note are the stopMedia() method and the first line in the playMedia() method.
The stopMedia method does exactly what it's name says. In the playMedia method, you can see that I've added VideoProcesser.getVideoProcesser().stopMedia() as the first line.
The Audio/VideoProcesser classes are almost identical so the added methods and minor tweaks will transfer over, I have tested this and it does work.
Since you also want to stop audio/video when you select a txt file, you will need to add the same call to video and audios stopMedia method in that class as well.

Related

How to keep the MediaRouter running in background activity?

please I need your help after searching a lot without issues.
I have an demostration app to use an second screen attached to my device.
I have the source code of the app, they use the Mediarouter class and an class named LauncherSecondScreen extended from the Presentation class
I have tried to make the app as an service to keep runnig the app in background, but the mediarouter callback seems running only on the princpal thread ( I'm not sure I am just a beginner in android dev).
I have the full code of the app : there is two layout activity one showed on the princpal screen and the other on the second screen:
public class MainActivity extends Activity {
private final String TAG = "PresentationWithMediaRouterActivity";
private MediaRouter mMediaRouter;
private LauncherSecondScreen mPresentation;
private boolean mPaused;
/**
* Initialization of the Activity after it is first created. Must at least
* call {#link android.app.Activity#setContentView setContentView()} to
* describe what is to be displayed in the screen.
*/
#Override
protected void onCreate(Bundle savedInstanceState) {
// Be sure to call the super class.
super.onCreate(savedInstanceState);
// Get the media router service.
mMediaRouter = (MediaRouter)getSystemService(Context.MEDIA_ROUTER_SERVICE);
// See assets/res/any/layout/presentation_with_media_router_activity.xml for this
// view layout definition, which is being set here as
// the content of our screen.
setContentView(R.layout.activity_main);
}
#Override
protected void onResume() {
// Be sure to call the super class.
super.onResume();
// Listen for changes to media routes.
mMediaRouter.addCallback(MediaRouter.ROUTE_TYPE_LIVE_VIDEO, mMediaRouterCallback);
// Update the presentation based on the currently selected route.
mPaused = false;
updatePresentation();
}
private void updatePresentation() {
// Get the current route and its presentation display.
MediaRouter.RouteInfo route = mMediaRouter.getSelectedRoute(
MediaRouter.ROUTE_TYPE_LIVE_VIDEO);
Display presentationDisplay = route != null ? route.getPresentationDisplay() : null;
// Dismiss the current presentation if the display has changed.
if (mPresentation != null && mPresentation.getDisplay() != presentationDisplay) {
Log.i(TAG, "Dismissing presentation because the current route no longer "
+ "has a presentation display.");
mPresentation.dismiss();
mPresentation = null;
}
// Show a new presentation if needed.
if (mPresentation == null && presentationDisplay != null) {
Log.i(TAG, "Showing presentation on display: " + presentationDisplay);
mPresentation = new LauncherSecondScreen(this, presentationDisplay);
mPresentation.setOnDismissListener(mOnDismissListener);
try {
mPresentation.show();
} catch (WindowManager.InvalidDisplayException ex) {
Log.w(TAG, "Couldn't show presentation! Display was removed in "
+ "the meantime.", ex);
mPresentation = null;
}
}
// Update the contents playing in this activity.
updateContents();
}
private void updateContents() {
// Show either the content in the main activity or the content in the presentation
// along with some descriptive text about what is happening.
if (mPresentation != null) {
if (mPaused) {
mPresentation.dismiss();//getSurfaceView().onPause();
} else {
mPresentation.show();//getSurfaceView().onResume();
}
} else {
/* mInfoTextView.setText("presentation_with_media_router_now_playing_locally");
mSurfaceView.setVisibility(View.VISIBLE);
if (mPaused) {
mSurfaceView.onPause();
} else {
mSurfaceView.onResume();
}*/
}
}
private final MediaRouter.SimpleCallback mMediaRouterCallback =
new MediaRouter.SimpleCallback() {
#Override
public void onRouteSelected(MediaRouter router, int type, RouteInfo info) {
Log.d(TAG, "onRouteSelected: type=" + type + ", info=" + info);
updatePresentation();
}
#Override
public void onRouteUnselected(MediaRouter router, int type, RouteInfo info) {
Log.d(TAG, "onRouteUnselected: type=" + type + ", info=" + info);
updatePresentation();
}
#Override
public void onRoutePresentationDisplayChanged(MediaRouter router, RouteInfo info) {
Log.d(TAG, "onRoutePresentationDisplayChanged: info=" + info);
updatePresentation();
}
};
/**
* Listens for when presentations are dismissed.
*/
private final DialogInterface.OnDismissListener mOnDismissListener =
new DialogInterface.OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialog) {
if (dialog == mPresentation) {
Log.i(TAG, "Presentation was dismissed.");
mPresentation = null;
updateContents();
}
}
};
#SuppressLint({"NewApi"})
public class LauncherSecondScreen extends Presentation
{
public LauncherSecondScreen(Context paramContext, Display paramDisplay)
{
super(paramContext, paramDisplay/*,android.R.style.Theme_Holo_Light_Dialog_NoActionBar*/);
}
protected void onCreate(Bundle paramBundle)
{
super.onCreate(paramBundle);
setContentView(R.layout.dialog_second_screen_content);
//// this.iv_secondScreen_banner = ((ImageView)findViewById(R.id.titleImage));
}
}
}
the app is well, it make one view in the princpale screen and a second view in the second screen , but when i resume the app to background the second screen take the same view of the first screen.
I want to keep the second view showing in the second screen even i resume the app to use another app

I'm lost with the Android Apk Exapnsion Procedure

My app recently reached the "maximum" size of 100 MBs so I had to learn about the apk expansion procedure. I read google's documentation but i found it kind of bad so i looked around and followed this guide: https://kitefaster.com/2017/02/15/expansion-apk-files-android-studio/ . Now, after adding the SampleDownloaderActivity,SampleDownloaderService and the SampleAlarmReceiver classes i got lost. I created a directory in my phone's internal storage in this location Android/obb/com.mypackage.example like the Google documentation mentions so i can put my expansion files there. But here are my questions:
1) I've added the required permissions in the app's manifest.xml file but do i need to ask for them first through code in order for the receiver,downloader and the downloaderActivity to work?
2) The files that i want to include in my main expansion file are some images from my xhdpi drawable folder but i am not sure on what i need to do in order to create my .obb file containing those images. Do i need to create a .zip file with them? Do i just drop them inside the directory i created that i mentioned above? What do i need to do?
3) Supposing the previous questions have been answered, if the files already exist in the directory or have been downloaded, i must get that dir through code and read the files in order to use them in my app, correct?
4) If the files are not there,how do i initiate the download from Google-Play? From what i understand by the doc, i need to make my app's main activity the "SampleDownloaderActivity" one, right?
5) After the files are done downloading, do i have to create an intent in the SampleDownloaderActivity's onCreate method in order for the app to go to my desired activity which uses those files?
Below, i'm posting the apk expansion related code files in which i've changed what i understood was needed. Do i need to change something else? Thank you in advance for your help!
ApkExpDownloaderService.java
public class ApkExpDownloaderService extends DownloaderService {
// stuff for LVL -- MODIFY FOR YOUR APPLICATION!
private static final String BASE64_PUBLIC_KEY = "MY_KEY";
// used by the preference obfuscater
private static final byte[] SALT = new byte[] {
// my array of bytes
};
/**
* This public key comes from your Android Market publisher account, and it
* used by the LVL to validate responses from Market on your behalf.
*/
#Override
public String getPublicKey() {
return BASE64_PUBLIC_KEY;
}
/**
* This is used by the preference obfuscater to make sure that your
* obfuscated preferences are different than the ones used by other
* applications.
*/
#Override
public byte[] getSALT() {
return SALT;
}
/**
* Fill this in with the class name for your alarm receiver. We do this
* because receivers must be unique across all of Android (it's a good idea
* to make sure that your receiver is in your unique package)
*/
#Override
public String getAlarmReceiverClassName() {
return ApkExpAlarmReceiver.class.getName();
}
}
ApkExpDownloaderActivity.java
public class ApkExpDownloaderActivity extends Activity implements IDownloaderClient {
private static final String LOG_TAG = "LVLDownloader";
private ProgressBar mPB;
private TextView mStatusText;
private TextView mProgressFraction;
private TextView mProgressPercent;
private TextView mAverageSpeed;
private TextView mTimeRemaining;
private View mDashboard;
private View mCellMessage;
private Button mPauseButton;
private Button mWiFiSettingsButton;
private boolean mStatePaused;
private int mState;
private IDownloaderService mRemoteService;
private IStub mDownloaderClientStub;
private void setState(int newState) {
if (mState != newState) {
mState = newState;
mStatusText.setText(Helpers.getDownloaderStringResourceIDFromState(newState));
}
}
private void setButtonPausedState(boolean paused) {
mStatePaused = paused;
int stringResourceID = paused ? R.string.text_button_resume :
R.string.text_button_pause;
mPauseButton.setText(stringResourceID);
}
/**
* This is a little helper class that demonstrates simple testing of an
* Expansion APK file delivered by Market. You may not wish to hard-code
* things such as file lengths into your executable... and you may wish to
* turn this code off during application development.
*/
private static class XAPKFile {
public final boolean mIsMain;
public final int mFileVersion;
public final long mFileSize;
XAPKFile(boolean isMain, int fileVersion, long fileSize) {
mIsMain = isMain;
mFileVersion = fileVersion;
mFileSize = fileSize;
}
}
/**
* Here is where you place the data that the validator will use to determine
* if the file was delivered correctly. This is encoded in the source code
* so the application can easily determine whether the file has been
* properly delivered without having to talk to the server. If the
* application is using LVL for licensing, it may make sense to eliminate
* these checks and to just rely on the server.
*/
private static final XAPKFile[] xAPKS = {
new XAPKFile(
true, // true signifies a main file
21, // the version of the APK that the file was uploaded
// against
687801613L // the length of the file in bytes
)
};
/**
* Go through each of the APK Expansion files defined in the structure above
* and determine if the files are present and match the required size. Free
* applications should definitely consider doing this, as this allows the
* application to be launched for the first time without having a network
* connection present. Paid applications that use LVL should probably do at
* least one LVL check that requires the network to be present, so this is
* not as necessary.
*
* #return true if they are present.
*/
boolean expansionFilesDelivered() {
for (XAPKFile xf : xAPKS) {
String fileName = Helpers.getExpansionAPKFileName(this, xf.mIsMain, xf.mFileVersion);
if (!Helpers.doesFileExist(this, fileName, xf.mFileSize, false))
return false;
}
return true;
}
/**
* Calculating a moving average for the validation speed so we don't get
* jumpy calculations for time etc.
*/
static private final float SMOOTHING_FACTOR = 0.005f;
/**
* Used by the async task
*/
private boolean mCancelValidation;
/**
* Go through each of the Expansion APK files and open each as a zip file.
* Calculate the CRC for each file and return false if any fail to match.
*
* #return true if XAPKZipFile is successful
*/
void validateXAPKZipFiles() {
AsyncTask<Object, DownloadProgressInfo, Boolean> validationTask = new AsyncTask<Object, DownloadProgressInfo, Boolean>() {
#Override
protected void onPreExecute() {
mDashboard.setVisibility(View.VISIBLE);
mCellMessage.setVisibility(View.GONE);
mStatusText.setText(R.string.text_verifying_download);
mPauseButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mCancelValidation = true;
}
});
mPauseButton.setText(R.string.text_button_cancel_verify);
super.onPreExecute();
}
#Override
protected Boolean doInBackground(Object... params) {
for (XAPKFile xf : xAPKS) {
String fileName = Helpers.getExpansionAPKFileName(
ApkExpDownloaderActivity.this,
xf.mIsMain, xf.mFileVersion);
if (!Helpers.doesFileExist(ApkExpDownloaderActivity.this, fileName,
xf.mFileSize, false))
return false;
fileName = Helpers
.generateSaveFileName(ApkExpDownloaderActivity.this, fileName);
ZipResourceFile zrf;
byte[] buf = new byte[1024 * 256];
try {
zrf = new ZipResourceFile(fileName);
ZipEntryRO[] entries = zrf.getAllEntries();
/**
* First calculate the total compressed length
*/
long totalCompressedLength = 0;
for (ZipEntryRO entry : entries) {
totalCompressedLength += entry.mCompressedLength;
}
float averageVerifySpeed = 0;
long totalBytesRemaining = totalCompressedLength;
long timeRemaining;
/**
* Then calculate a CRC for every file in the Zip file,
* comparing it to what is stored in the Zip directory.
* Note that for compressed Zip files we must extract
* the contents to do this comparison.
*/
for (ZipEntryRO entry : entries) {
if (-1 != entry.mCRC32) {
long length = entry.mUncompressedLength;
CRC32 crc = new CRC32();
DataInputStream dis = null;
try {
dis = new DataInputStream(
zrf.getInputStream(entry.mFileName));
long startTime = SystemClock.uptimeMillis();
while (length > 0) {
int seek = (int) (length > buf.length ? buf.length
: length);
dis.readFully(buf, 0, seek);
crc.update(buf, 0, seek);
length -= seek;
long currentTime = SystemClock.uptimeMillis();
long timePassed = currentTime - startTime;
if (timePassed > 0) {
float currentSpeedSample = (float) seek
/ (float) timePassed;
if (0 != averageVerifySpeed) {
averageVerifySpeed = SMOOTHING_FACTOR
* currentSpeedSample
+ (1 - SMOOTHING_FACTOR)
* averageVerifySpeed;
} else {
averageVerifySpeed = currentSpeedSample;
}
totalBytesRemaining -= seek;
timeRemaining = (long) (totalBytesRemaining / averageVerifySpeed);
this.publishProgress(
new DownloadProgressInfo(
totalCompressedLength,
totalCompressedLength
- totalBytesRemaining,
timeRemaining,
averageVerifySpeed)
);
}
startTime = currentTime;
if (mCancelValidation)
return true;
}
if (crc.getValue() != entry.mCRC32) {
Log.e(Constants.TAG,
"CRC does not match for entry: "
+ entry.mFileName);
Log.e(Constants.TAG,
"In file: " + entry.getZipFileName());
return false;
}
} finally {
if (null != dis) {
dis.close();
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
return true;
}
#Override
protected void onProgressUpdate(DownloadProgressInfo... values) {
onDownloadProgress(values[0]);
super.onProgressUpdate(values);
}
#Override
protected void onPostExecute(Boolean result) {
if (result) {
mDashboard.setVisibility(View.VISIBLE);
mCellMessage.setVisibility(View.GONE);
mStatusText.setText(R.string.text_validation_complete);
mPauseButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finish();
}
});
mPauseButton.setText(android.R.string.ok);
} else {
mDashboard.setVisibility(View.VISIBLE);
mCellMessage.setVisibility(View.GONE);
mStatusText.setText(R.string.text_validation_failed);
mPauseButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finish();
}
});
mPauseButton.setText(android.R.string.cancel);
}
super.onPostExecute(result);
}
};
validationTask.execute(new Object());
}
/**
* If the download isn't present, we initialize the download UI. This ties
* all of the controls into the remote service calls.
*/
private void initializeDownloadUI() {
mDownloaderClientStub = DownloaderClientMarshaller.CreateStub
(this, ApkExpDownloaderService.class);
setContentView(R.layout.main);
mPB = (ProgressBar) findViewById(R.id.progressBar);
mStatusText = (TextView) findViewById(R.id.statusText);
mProgressFraction = (TextView) findViewById(R.id.progressAsFraction);
mProgressPercent = (TextView) findViewById(R.id.progressAsPercentage);
mAverageSpeed = (TextView) findViewById(R.id.progressAverageSpeed);
mTimeRemaining = (TextView) findViewById(R.id.progressTimeRemaining);
mDashboard = findViewById(R.id.downloaderDashboard);
mCellMessage = findViewById(R.id.approveCellular);
mPauseButton = (Button) findViewById(R.id.pauseButton);
mWiFiSettingsButton = (Button) findViewById(R.id.wifiSettingsButton);
mPauseButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (mStatePaused) {
mRemoteService.requestContinueDownload();
} else {
mRemoteService.requestPauseDownload();
}
setButtonPausedState(!mStatePaused);
}
});
mWiFiSettingsButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
}
});
Button resumeOnCell = (Button) findViewById(R.id.resumeOverCellular);
resumeOnCell.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mRemoteService.setDownloadFlags(IDownloaderService.FLAGS_DOWNLOAD_OVER_CELLULAR);
mRemoteService.requestContinueDownload();
mCellMessage.setVisibility(View.GONE);
}
});
}
/**
* Called when the activity is first create; we wouldn't create a layout in
* the case where we have the file and are moving to another activity
* without downloading.
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/**
* Both downloading and validation make use of the "download" UI
*/
initializeDownloadUI();
/**
* Before we do anything, are the files we expect already here and
* delivered (presumably by Market) For free titles, this is probably
* worth doing. (so no Market request is necessary)
*/
if (!expansionFilesDelivered()) {
try {
Intent launchIntent = ApkExpDownloaderActivity.this
.getIntent();
Intent intentToLaunchThisActivityFromNotification = new Intent(
ApkExpDownloaderActivity
.this, ApkExpDownloaderActivity.this.getClass());
intentToLaunchThisActivityFromNotification.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_CLEAR_TOP);
intentToLaunchThisActivityFromNotification.setAction(launchIntent.getAction());
if (launchIntent.getCategories() != null) {
for (String category : launchIntent.getCategories()) {
intentToLaunchThisActivityFromNotification.addCategory(category);
}
}
// Build PendingIntent used to open this activity from
// Notification
PendingIntent pendingIntent = PendingIntent.getActivity(
ApkExpDownloaderActivity.this,
0, intentToLaunchThisActivityFromNotification,
PendingIntent.FLAG_UPDATE_CURRENT);
// Request to start the download
int startResult = DownloaderClientMarshaller.startDownloadServiceIfRequired(this,
pendingIntent, ApkExpDownloaderService.class);
if (startResult != DownloaderClientMarshaller.NO_DOWNLOAD_REQUIRED) {
// The DownloaderService has started downloading the files,
// show progress
initializeDownloadUI();
return;
} // otherwise, download not needed so we fall through to
// starting the movie
} catch (NameNotFoundException e) {
Log.e(LOG_TAG, "Cannot find own package! MAYDAY!");
e.printStackTrace();
}
} else {
validateXAPKZipFiles();
}
}
/**
* Connect the stub to our service on start.
*/
#Override
protected void onStart() {
if (null != mDownloaderClientStub) {
mDownloaderClientStub.connect(this);
}
super.onStart();
}
/**
* Disconnect the stub from our service on stop
*/
#Override
protected void onStop() {
if (null != mDownloaderClientStub) {
mDownloaderClientStub.disconnect(this);
}
super.onStop();
}
//TODO:sp need more info on this
/**
* Critical implementation detail. In onServiceConnected we create the
* remote service and marshaler. This is how we pass the client information
* back to the service so the client can be properly notified of changes. We
* must do this every time we reconnect to the service.
*/
#Override
public void onServiceConnected(Messenger m) {
mRemoteService = DownloaderServiceMarshaller.CreateProxy(m);
mRemoteService.onClientUpdated(mDownloaderClientStub.getMessenger());
}
/**
* The download state should trigger changes in the UI --- it may be useful
* to show the state as being indeterminate at times. This sample can be
* considered a guideline.
*/
#Override
public void onDownloadStateChanged(int newState) {
setState(newState);
boolean showDashboard = true;
boolean showCellMessage = false;
boolean paused;
boolean indeterminate;
switch (newState) {
case IDownloaderClient.STATE_IDLE:
// STATE_IDLE means the service is listening, so it's
// safe to start making calls via mRemoteService.
paused = false;
indeterminate = true;
break;
case IDownloaderClient.STATE_CONNECTING:
case IDownloaderClient.STATE_FETCHING_URL:
showDashboard = true;
paused = false;
indeterminate = true;
break;
case IDownloaderClient.STATE_DOWNLOADING:
paused = false;
showDashboard = true;
indeterminate = false;
break;
case IDownloaderClient.STATE_FAILED_CANCELED:
case IDownloaderClient.STATE_FAILED:
case IDownloaderClient.STATE_FAILED_FETCHING_URL:
case IDownloaderClient.STATE_FAILED_UNLICENSED:
paused = true;
showDashboard = false;
indeterminate = false;
break;
case IDownloaderClient.STATE_PAUSED_NEED_CELLULAR_PERMISSION:
case IDownloaderClient.STATE_PAUSED_WIFI_DISABLED_NEED_CELLULAR_PERMISSION:
showDashboard = false;
paused = true;
indeterminate = false;
showCellMessage = true;
break;
case IDownloaderClient.STATE_PAUSED_BY_REQUEST:
paused = true;
indeterminate = false;
break;
case IDownloaderClient.STATE_PAUSED_ROAMING:
case IDownloaderClient.STATE_PAUSED_SDCARD_UNAVAILABLE:
paused = true;
indeterminate = false;
break;
case IDownloaderClient.STATE_COMPLETED:
showDashboard = false;
paused = false;
indeterminate = false;
validateXAPKZipFiles();
return;
default:
paused = true;
indeterminate = true;
showDashboard = true;
}
int newDashboardVisibility = showDashboard ? View.VISIBLE : View.GONE;
if (mDashboard.getVisibility() != newDashboardVisibility) {
mDashboard.setVisibility(newDashboardVisibility);
}
int cellMessageVisibility = showCellMessage ? View.VISIBLE : View.GONE;
if (mCellMessage.getVisibility() != cellMessageVisibility) {
mCellMessage.setVisibility(cellMessageVisibility);
}
mPB.setIndeterminate(indeterminate);
setButtonPausedState(paused);
}
/**
* Sets the state of the various controls based on the progressinfo object
* sent from the downloader service.
*/
#Override
public void onDownloadProgress(DownloadProgressInfo progress) {
mAverageSpeed.setText(getString(R.string.kilobytes_per_second,
Helpers.getSpeedString(progress.mCurrentSpeed)));
mTimeRemaining.setText(getString(R.string.time_remaining,
Helpers.getTimeRemaining(progress.mTimeRemaining)));
progress.mOverallTotal = progress.mOverallTotal;
mPB.setMax((int) (progress.mOverallTotal >> 8));
mPB.setProgress((int) (progress.mOverallProgress >> 8));
mProgressPercent.setText(Long.toString(progress.mOverallProgress
* 100 /
progress.mOverallTotal) + "%");
mProgressFraction.setText(Helpers.getDownloadProgressString
(progress.mOverallProgress,
progress.mOverallTotal));
}
#Override
protected void onDestroy() {
this.mCancelValidation = true;
super.onDestroy();
}
}
ApkExpReceiver.java
public class ApkExpAlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
try {
DownloaderClientMarshaller.startDownloadServiceIfRequired(context, intent, ApkExpDownloaderService.class);
} catch (NameNotFoundException e) {
e.printStackTrace();
}
}
}
Permissions is a bigger topic and independent of OBB files. Rather than explain it here, I recommend the documentation. In short, for Android devices before 6.0 they only need to be in the manifest. For Android devices after 6.0 you should ask for them at an appropriate moment at runtime.
OBB is as the name suggests an "Opaque" binary blob. You can choose whatever file format you want, google just treats it as a blob of bits. Lots of apps use zip files, but you don't have to, you can use whatever file format you like.
Correct.
The sample download activity is what the name suggests - a sample. You can use that activity, or you can re-use the parts of the code to do the downloading in your own app. Whatever you do, downloading may take a little while, needs permissions, and won't work while offline, so you need to display an appropriate user interface to the user for all these cases. What an appropriate UI is will vary for different apps.

unable to create logic to play all files sequentially from listView(playlist). at end of a each song(listview item) song should be played

Here is my code in which i m choosing the directory/Folder to get all (mp3) files in my listview. after adding files in listview i want to play them automatically one by one from first list item to end. I am trying to use indexes of the listview items in for loop. but logically wrong somewhare. May i missing something. Help will be appriciated. Thanks buddies.
DirectoryChooser directoryChooser = new DirectoryChooser();
directoryChooser.setTitle("Select a Folder for Playlist");
directoryChooser.setInitialDirectory(new File("D:\\Songs"));
File dir = directoryChooser.showDialog(new Stage());
playlist.getItems().addAll(dir.listFiles());
try {
playlist.getSelectionModel().selectLast();
lastItemIndex = playlist.getSelectionModel().getSelectedIndex();
playlist.getSelectionModel().selectFirst();
fileToPlayIndex = playlist.getSelectionModel().getSelectedIndex();
// JOptionPane.showMessageDialog(null,fileToPlayIndex );
for(int i=fileToPlayIndex; i<=lastItemIndex; i++ ){
fileToPlay = playlist.getSelectionModel().getSelectedItem();
Media media = new Media(fileToPlay.toURI().toString());
mediaPlayer = new MediaPlayer(media);
mdv.setMediaPlayer(mediaPlayer);
mdv.setPreserveRatio(true);
mediaPlayer.setAutoPlay(true);
DoubleProperty width = mdv.fitWidthProperty();
DoubleProperty height = mdv.fitHeightProperty();
width.bind(Bindings.selectDouble(mdv.sceneProperty(), "width"));
height.bind(Bindings.selectDouble(mdv.sceneProperty(),"Height"));
// voulume slider
volslider();
// seekslider implementation
seekslider();
mediaPlayer.play();
JOptionPane.showMessageDialog(null, "Media is playing ");
mediaPlayer.setOnEndOfMedia(new Runnable() {
#Override
public void run() {
mediaPlayer.stop();
JOptionPane.showMessageDialog(null, "Media Has been ended.");
playlist.getSelectionModel().selectNext();
//getting the changed(next) index of the list view item.
fileToPlayIndex = playlist.getSelectionModel().getSelectedIndex();
flag = 1;
mediaPlayer.dispose();
}
});
}
}
catch (Exception eex) {
JOptionPane.showMessageDialog(null,eex);
}
}

libgdx stopping running music with class

I'm working on a little Android Game with libGDX.
The structure is as follows:
Main > Screen:Splashscreen > Screen:MainMenu > Screen: Settings or GameScreen
I have a Preference for the state of music(if running "music" is true).
If the music is true the music starts in the MainMenu.
Now the user should be abled to turn the music on and off in the Settings Screen.
To manage the music I made a class called MusicPlayer.
The Code:
MainMenu:
public class StartScreen implements Screen {
MusicPlayer musicPl;
Preferences scorepref;
boolean music;
(...)
#Override
public void show() {
(...)
scorepref = Gdx.app.getPreferences("Highscore");
musicPl = new MusicPlayer();
music = scorepref.getBoolean("music");
if(music){
musicPl.play();
}
}
#Override
public void dispose() {
musicPl.dispose();
}
}
Settings:
public class SettingScreen implements Screen {
Preferences scorepref;
//Setting Values
boolean tut = false;
boolean music = false;
boolean sounds = false;
int theme = 0;
//
MusicPlayer musicPl;
int touchX = 0;
int touchY = 0;
boolean touchD = false;
boolean touchU = false;
#Override
public void show() {
(handling Input and setting touchX,touchY,touchD and touchU)
(...)
musicPl = new MusicPlayer();
scorepref = Gdx.app.getPreferences("Highscore");
}
#Override
public void render(float delta) {
tut = scorepref.getBoolean("tut");
music = scorepref.getBoolean("music");
sounds = scorepref.getBoolean("sounds");
(...)
//if Touched
if(touchD == true && touchU == true){
touchD = false;
touchU = false;
//wo?
Vector3 posn = new Vector3(Gdx.input.getX(),Gdx.input.getY(), 0);
camera.unproject(posn);
if(){
(...)
} else if (posn.x >= -192 && posn.x <= 192 && posn.y <= 53 && posn.y >= -75) {
if(music){
music = false;
scorepref.putBoolean("music", music);
scorepref.flush();
musicPl.stop(); //Error line 206 is here
} else {
music = true;
scorepref.putBoolean("music", music);
scorepref.flush();
musicPl.play();
}
} (...)
} else if (posn.x >= -344 && posn.x <= -248 && posn.y <= 543 && posn.y >= 463) {
((Game) Gdx.app.getApplicationListener()).setScreen(new StartScreen());
}
//var speichern
scorepref.putBoolean("tut", tut);
scorepref.putBoolean("music", music);
scorepref.putBoolean("sounds", sounds);
scorepref.flush();
}
#Override
public void dispose() {
musicPl.dispose();
}
}
MusicPlayer:
public class MusicPlayer{
Music menuMusic;
boolean isrunning;
Preferences scorepref;
public void play (){
scorepref = Gdx.app.getPreferences("Highscore");
if(scorepref.getBoolean("running") == true){
} else {
menuMusic = Gdx.audio.newMusic(Gdx.files.internal("Theme.mp3"));
menuMusic.setLooping(true);
scorepref.putBoolean("running", true);
scorepref.flush();
menuMusic.play();
}
}
public void stop(){
scorepref = Gdx.app.getPreferences("Highscore");
scorepref.putBoolean("running", false);
scorepref.flush();
menuMusic.stop(); //Error line 33 is here
}
public void dispose(){
menuMusic.dispose();
}
}
My Problem:
When I am in the Settings Screen the tunring on and off is working fine.
When I turn the music on and go back to the MainScreen the music is still playing. So far so good.
But when I return to the SettingsScreen with running music and I want to turn it off the App crashes.
I think the crash is caused by the stop method, because the MusicPlayer doesn't know what to stop. But how can I tell him or how can i solve this problem with a different technique?
Thanks for helping.
P.S.
Here is the error I get, when I run the App on the desktop:
Exception in thread "LWJGL Application" java.lang.NullPointerException
at de.hatgames.canone.MusicPlayer.stop(MusicPlayer.java:33)
at de.hatgames.canone.SettingScreen.render(SettingScreen.java:206)
at com.badlogic.gdx.Game.render(Game.java:46)
at de.hatgames.canone.CanoneMain.render(CanoneMain.java:26)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:215)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:120)
CanoneMain.java:26 is just this:
super.render();
when you return to SettingScreen you create a new MusicPlayer instance each time.
musicPl = new MusicPlayer();
The Music member is only instanciated when play() is called:
menuMusic = Gdx.audio.newMusic(Gdx.files.internal("Theme.mp3"));
That is why menuMusic is null when you call stop() before calling play()
There are several solutions. You can make the MusicPlayer static and global accesable.
Or you can make sure MusicPlayer is only instanitated once like this:
if(musicPl == null) {
musicPl = new MusicPlayer();
}
But this will only work when you make sure that SettingsScreen is only instantiated once.
Good topics to read are probably singleton and factory pattern since this case will often occur when programming games.
Additionally using a Singleton for the music player as part of it being a global object will be helpful.

Dynamic List never Loading - Using parse.com

SEE REVISION AT BOTTOM This is a fight card, so it has two people fighting one another, a red vs blue. It has to be a dynamic list that is populated information from parse.com. The first Query is fightOrder. This is a class on Parse.com that has two objectId's on a row. The redCorner and blueCorner find this information in my database (also on parse.com) and display the information accordingly. My problem, is my progressDialog box appears, and it never goes away. My list is never populated. I tried doing it without the dialog box, and populating my list with ever query and had same results.
NOTE: the list is working properly. This is a list I have used successfully before when I would load my information differently. I am just changing the way I load information because I need to have a database of all fighters, and load my fight card from that list.
NOTE: GetCallBack and FindCallBack are asynchronous, that is why this is an odd loop. I have to wait for the done().
Here is the java
public class databaseFightCard extends Activity {
int I;
int size;
private HomeListAdapter HomeListAdapter;
private ArrayList<HomeItem> HomeItemList;
private SeparatedListAdapter adapter;
//this int is to test for main and coMain events. If one is TRUE, It will assign the array position to main or coMain.
int main, coMain;
ParseQuery<ParseObject> blueCorner = ParseQuery.getQuery("FightersDB");
ParseQuery<ParseObject> redCorner = ParseQuery.getQuery("FightersDB");
String name1, name2;
List<String> red = new ArrayList<String>();
List<String> blue = new ArrayList<String>();
private ListView listView;
ProgressDialog progressDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_list);
progressDialog = ProgressDialog.show(this, "", "Loading bout...", true);
initialization();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
HomeItem homeItem = (HomeItem) adapter.getItem(position);
AlertDialog.Builder showFighter = new AlertDialog.Builder(databaseFightCard.this, android.R.style.Theme_DeviceDefault_Dialog);
showFighter.setTitle(homeItem.getHomeItemLeft().toString() + " and " + homeItem.getHomeItemRight().toString());
showFighter.setMessage("166 - 165\nLogan Utah - Richmond Utah");
showFighter.setPositiveButton("DONE", null);
showFighter.setNegativeButton("Cancel", null);
AlertDialog dialog = showFighter.show();
TextView messageView = (TextView) dialog.findViewById(android.R.id.message);
messageView.setGravity(Gravity.CENTER);
Toast.makeText(getBaseContext(), homeItem.getHomeItemLeft().toString() + " " + homeItem.getHomeItemRight().toString(), Toast.LENGTH_LONG).show();
System.out.println("Selected Item : " + homeItem.getHomeItemID());
}
});
HomeListAdapter = new HomeListAdapter(getApplicationContext(), 0, HomeItemList);
//find the fight card, and read the ids
ParseQuery<ParseObject> fightOrder = ParseQuery.getQuery("FightCard");
fightOrder.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> parseObjects, ParseException e) {
if (e == null) {
size = parseObjects.size();
int i = 0;
while (i < size) {
if (parseObjects.get(i).getBoolean("main")) {
main = i;
}
if (parseObjects.get(i).getBoolean("coMain")) {
coMain = i;
}
red.add(i, parseObjects.get(i).getString("redCorner"));
blue.add(i, parseObjects.get(i).getString("blueCorner"));
i++;
}
displayRed();
} else {
e.printStackTrace();
}
}
});
}
private void displayRed() {
adapter = new SeparatedListAdapter(this);
//find one fighter at a time. in the done() method, start the second fighter.
redCorner.getInBackground(red.get(I), new GetCallback<ParseObject>() {
#Override
public void done(ParseObject parseObject, ParseException e) {
if (e == null) {
HomeItemList = new ArrayList<HomeItem>();
HomeItem homeItem = new HomeItem();
homeItem.setHomeItemID(I);
name1 = parseObject.getString("Name");
homeItem.setHomeItemLeft(name1);
HomeItemList.add(homeItem);
if (HomeListAdapter != null) {
if (I == main) {
adapter.addSection(" MAIN EVENT ", HomeListAdapter);
} else if (I == coMain) {
adapter.addSection(" Co-MAIN EVENT ", HomeListAdapter);
} else {
adapter.addSection(" FIGHT CARD ", HomeListAdapter);
}
}
displayBlue();
} else {
e.printStackTrace();
}
I++;
while (I < size){
displayRed();
}
if (size == I) {
listView.setAdapter(adapter);
progressDialog.dismiss();
}
}
});
}
private void displayBlue() {
//find the red fighters then call the dismiss();
blueCorner.getInBackground(blue.get(I), new GetCallback<ParseObject>() {
#Override
public void done(ParseObject parseObject, ParseException e) {
if (e == null) {
HomeItemList = new ArrayList<HomeItem>();
HomeItem homeItem = new HomeItem();
homeItem.setHomeItemID(I);
name2 = parseObject.getString("Name");
homeItem.setHomeItemLeft(name2);
HomeItemList.add(homeItem);
if (HomeListAdapter != null) {
if (I == main) {
adapter.addSection(" MAIN EVENT ", HomeListAdapter);
} else if (I == coMain) {
adapter.addSection(" Co-MAIN EVENT", HomeListAdapter);
} else {
adapter.addSection(" FIGHT CARD ", HomeListAdapter);
}
}
} else {
e.printStackTrace();
}
//if it is done running through all the IDS, set the listView, and dismiss the dialog.
I++;
while (I < size){
displayRed();
}
if (size == I) {
listView.setAdapter(adapter);
progressDialog.dismiss();
}
}
});
}
private void initialization() {
listView = (ListView) findViewById(R.id.Listview);
}
LogCat
java.lang.RuntimeException: This query has an outstanding network
connection. You have to wait until it's done.
That is pointing to this line:
while (I < size){
displayRed();
}
EDIT
I believe that it is the async tasks that are causing this.
On a previous build: I would call for one line item at a time, add it to my list, repeat until finished, then display list.
On the this build: I want to call for redCorner add it to my list, call blueCorner add it to the same line, repeat until finished, then display the list. Here is what it would look like (previous build):
Revised My question is still unanswered. Maybe I need to simplify it. I will have +-20 objectId's from one class. I took out all the code that is irrelevant. Still getting unexpected results with this code.
redCorner.getInBackground(red.get(i), new GetCallback<ParseObject>() {
#Override
public void done(ParseObject parseObject, ParseException e) {
if (e == null) {
Log.d("NAME " + i, name1 + " ");
i++;
while (i < size) {
redCorner.cancel();
displayRed();
}
if (i == size) {
progressDialog.dismiss();
}
} else {
e.printStackTrace();
}
}
});
This is yet another case of not understanding the nature of Async coding (I've seen a lot of questions with the same issue).
In your case you are calling the displayRed() method that fires off some async code, then returns.
Here's how your code might run:
First call to displayRed() (dr1)
(dr1) Async redCorner.getInBackground(..) (async1) started
(dr1) returns
.. some time passes ..
(async1) getInBackground(..) call returns with data, runs code block
calls displayBlue() (db1)
(db1) blueCorner.getInBackground(..) (async2) started
(db1) returns
begins the while loop
calls displayRed() (dr2)
(dr2) Async redCorner.getInBackground(..) (async3) started
(dr2) nothing has touched I yet, tries to start another async redCorner.getInBackgroud(..) (async4)
ERROR
You're writing your code as if the async blocks are running sync instead. Keep in mind that getInBackground means "make a web call to get this data, and when something happens (error or success) run this block of code I'm giving you, possibly on another thread".
Think about the order you want to achieve things, realise that you're asking it to start a process that takes some time, and adjust your code accordingly.

Categories

Resources