Media Player is restarting the file when I re-enter the activity - java

I have a music player app which consists of a "playlist" and a "now playing" activity.
The two activities are linked using a menu for navigation.
When I select a song from the playlist is plays as expected in the background - I can go to the playlist and browse other songs...
However when I then use the menu to return to the now playing activity, it restarts the media file instead of just resuming? Surely it would just continue playing?
The below logic is inside of the onCreate() method but I have a feeling I should add to the onResume() method too?
Any assistance will be greatly appreciated!
--
if (itemSelected || !mMediaPlayer.isPlaying()){ //Gets media as normal
infoSetter();
musicPlayer();
} else if (!itemSelected && !mMediaPlayer.isPlaying()) { //Sets defaults if activity is accessed without making a selection
defaultInfoSetter();
defaultMusicPlayer();
} else if (mMediaPlayer.isPlaying()){ //TODO: Fix this part
//???????????????
}

Related

How to click button inside method Adapter in RecyclerView?

I am working on stripe-terminal-android-app, to connect to BBPOS 2X Reader device,
wanted to click-item from list,(recyclerView).
I am trying to do:
when list of devices appears(readers), I am checking if readers.size()==1, then click first-device from list,else show recyclerView();
I have very less experience in Android(coming from JS, PY), :)
After going through debugger to understand flow of program-running, I used F8 key, or stepOver the functions one by one,
and where value is assigned to convert in displayble-format in adapter as here.
public ReaderAdapter(#NotNull DiscoveryViewModel viewModel) {
super();
this.viewModel = viewModel;
if (viewModel.readers.getValue() == null) {
readers = new ArrayList<>();
} else {
readers = viewModel.readers.getValue();
if(readers.size() == 1){
Log.e(TAG, "readers.size() is 1 "+ readers.size());
}
}
}
then in ReaderHolder-file, values are bind() as
void bind(#NotNull Reader reader) {
binding.setItem(reader);
binding.setHandler(clickListener);
binding.executePendingBindings();
}
}
I tried assigining button and manually clicking when only-one device appears, by clicing on reader[0], can't do that by findViewById inside Adapter file, to call onClick() method manually,
I tired another StackOverflow's answer but didn't understood, from here.
Main fragment is discovery-fragment,
how can I click first-device by checking readers.size()==1, then click onClick()?
my final-goal is to automate, whole stripe-terminal-payment process on android.
extra-info:
I am fetching data from python-odoo server, then using url, will open app through browser, (done this part), then device will be selected automatically as everytime-no any devices will be present except one,
so will automatically select that from recyclerView, then proceed.
I have asked for help in detailed way on GitHub-issues, and started learning Android's concepts for this app(by customizing stripe's demo app, which works great, but I wanted to avoid manually clicking/selection of devices).

Allow user to select music files from computer into a processing sketch?

I'm creating a music visualiser. I am stuck on the part which allows the user to upload music tracks from their music library on their computer and have the visualiser respond to the beat of the specific song they have chosen.
I have used the minim library in Processing however I have to load the file name in the coding. I would like a way to have the user click a button which will open the file browser and allow them to select a track and then input that track into the player which will play the track and have the visualiser respond to the beats of the track.
I am not asking for code; rather I am asking on a way to go about this step by step. I am stuck on how to retrieve the files from the computer. I am not very good at coding so I am looking at libraries and tutorials online however; the videos I am finding on this topic only shows how to load text files into a sketch rather than a music file.
Sounds like you're looking for the selectInput() function.
From the reference:
void setup() {
selectInput("Select a file to process:", "fileSelected");
}
void fileSelected(File selection) {
if (selection == null) {
println("Window was closed or the user hit cancel.");
} else {
println("User selected " + selection.getAbsolutePath());
}
}
Opens a platform-specific file chooser dialog to select a file for
input. After the selection is made, the selected File will be passed
to the 'callback' function. If the dialog is closed or canceled, null
will be sent to the function, so that the program is not waiting for
additional input. The callback is necessary because of how threading
works.

Strange issue with intent startActivity causing my scanner to not work properly

So, after I finish my scanner activity with
btn_take_photo.setOnClickListener(new FloatingActionButton.OnClickListener() {
#Override
public void onClick(View view) {
String carde = cardnumberbox.getText().toString().trim();
if (carde.matches("")) {
Toast.makeText(getApplicationContext(), getString(R.string.Skan_Udfyld_Kort_Nummer), Toast.LENGTH_SHORT).show();
cardnumberbox.requestFocus();
return;
}
Intent i = new Intent(ScanActivity.this, CameraActivity.class);
i.putExtra("EXTRA_SESSION_ID", carde);
startActivity(i);
}
});
to go to my cam activity so I can take some pictures and go back with
public void btn_aprove2(View view) {
Intent i = new Intent(CameraActivity.this, ScanActivity.class);
String counts = count.getText().toString().trim();
i.putExtra("EXTRA_SESSION_IDs", counts);
String carde = cardnumberbox2.getText().toString().trim();
i.putExtra("EXTRA_SESSION_ID", carde);
startActivity(i);
finish();
to the scanneractivity again. My scanner does not work properly
but if I then press the back button it does go back to the scanneractivity again instead of my menu so it seems like the scanneractivity is running twice and only 1 of them are functional but is here where it confuses me
cause if do not press the btn_aprove2 button and just use the back button instead
i gets the exact same issue but here my scanneractivity is not runned twice as when i press the back button it just takes me back to the menu
a video of the issue
by removing my screen orientation from the manifest (so i can rotate it)
my scanner do now work but only if i first rotate to landscape and rotate it back to potrait
and i see in the log is that it is only calling the oncreate when rotating and only on resume and pause on the button's(startactivity/finnish)
I am totally lost on how to get this to work.
on github with api demo and documentation in the wiki and with thoose classes that are being used
If you just jump to the Camera activity to get some data, I'd recommend you to start the activity for a result (startActivityForResult) without finishing the Scanner activity at all. This would give you a proper working back stack (using back button to go back from Camera to Scanner).
Besides that why are you using i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);? You are starting a new activity and finishing the old one. I don't see why this flag is really needed. What is your android:launchMode in your manifest and are you sure you know what FLAG_ACTIVITY_NEW_TASK is doing and it is what you want?
Anyways from what you told us it looks like your example really should utilize startActivityForResult() without calling finish():
Press menu button on some activity
Start camera and do something
Press some button to start Scanner
Scan something and finish the scanner with the result (or cancel the scanner by clicking back)
Retrieve the result of the scanner in the camera and do something with it or continue with the previous workflow when scanner was cancelled
Once you are finished with your workflow, finish the camera so you end up in your activity where you started the camera

Android Radio App that streams my mp3 from a dropbox server

I have a dropbox media server that has a collection of mp3 files that I want to stream onto an android application.
I know that using the "MediaPlayer" is the best way to go in the API.
How my main concern is how do I automate the process, where music is being played one after another? As if it was like an internet radio app?
Could someone please point me in the correct location for guides or display example code would be great thank you in advance.
Place the music files names in an arraylist (e.g songs) then implement onCompletionListener then set it to your media player. Inside the listener restart the media player to play the next item.
myMediaPlayer.setOnCompletionListener(this);
public void onCompletion(MediaPlayer arg0) {
arg0.release();
if (counter < songs.size()) {
counter++;
arg0 = MediaPlayer.create(getApplicationContext(), songs.get(counter);
arg0.setOnCompletionListener(this);
arg0.start();
}
}
If the server gives apps audio files;put the files in a queue and play them using mediaplayer or another audio player.
Get first song from server and start playing it, meanwhile continue downloading other songs one by one.

Send data from Settings Activity to Watch Face

I just developed a simple Android Wear Watch Face to start learning something about Android Wear.
App: https://play.google.com/store/apps/details?id=com.gerardcuadras.minimalflat (So simple)
The watch face change his Background color when the user taps the screen. Now, I want to add an Activity that show's a list of colors and let the user select one instead of tapping the screen.
I added this lines to my manifest that adds the config toggle to the watch face.
<meta-data
android:name="com.google.android.wearable.watchface.wearableConfigurationAction"
android:value="com.gerardcuadras.minimalflat.wearable.watchface.CONFIG_DIGITAL" />
After that I created an Activity that handles the button click:
public void broadcastIntent(View view){
Log.d(TAG, "broadcastIntent: Pressed button");
Intent intent = new Intent();
intent.setAction("com.gerardcuadras.minimalflat.CUSTOM_INTENT");
intent.putExtra("extra", "extra data");
sendBroadcast(intent);
}
Then on MyWatchFace.java (extends CanvasWatchFaceService) I have this that it supposes to handle the broadcast:
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "onReceive: WORKS!");
}
}
When I run the emulator and click at the button, the only thing that I see on the console is:
"broadcastIntent: Pressed button"
So the receiver (maybe the sender) isn't working because it's not logging the onReceive() log.
I'm really new at Android. I'm sure I'm not doing this well or I'm missing something.
I will appreciate any help or any documentation to continue learning step by step.
Thanks
This might be a better fit for using SharedPreferences (http://developer.android.com/reference/android/content/SharedPreferences.html)
With SharedPreferences the selection will be persisted even if the user changes to a different watch face and back.
You can also use a registered Preference Change Listener to detect the changes made in your configuration activity.
http://developer.android.com/reference/android/content/SharedPreferences.OnSharedPreferenceChangeListener.html
You can use the WearPreferenceActivity library for your PreferencesActivity, which changes the SharedPreferences android:key="pref_key". Your watch face reads preferences via mSharedPref.getBoolean("pref_key", false);.
One easy way to detect preference changes is, to listen to the onVisibilityChanged event, which will be called after the PreferencesActivity gets closed.

Categories

Resources