Hey guys I'm totally new to java, trying to finish my first project for college. I want to finish the code with setting OnCompletionListener to set the sound to null. So, it looks like this:
package com.example.alaiborys.newfuckingshit;
public class campfire extends AppCompatActivity {
MediaPlayer campfiresound;
int paused;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_campfire);
}
public void play(View view) {
if (campfiresound == null) {
campfiresound = MediaPlayer.create(this, R.raw.campfire);
campfiresound.start();
}
else if(!campfiresound.isPlaying())
{
campfiresound.seekTo(paused);
campfiresound.start();
}
}
public void stop(View view) {
campfiresound.release();
campfiresound = null;
}
public void pause(View view) {
campfiresound.pause();
paused = campfiresound.getCurrentPosition();
}
}
So, I want to add OnCompletionListener(): but have no idea where to put it and how to code it, any idea?
Well that is pretty easy...you have a media player object campfiresound
so you need to regisdter the interface OnCompletionListener
campfiresound.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
//toast...am done!!
}
});
Related
I have a problem with my countdown and I want to play an individual sound at the end. For this I have created a list. When the corresponding picture is clicked, the selection should be saved and played in this method:
private void loadMusic() {
if (timerStatus == TimerStatus.PAUSED)
new Runnable() {
#Override
public void run() {
playSound(R.raw.sound1);
}
public void playSound(int sound1) {
MediaPlayer mp = MediaPlayer.create(getBaseContext(), (R.raw.sound2));
mp.start();
}
}.run()
}
I tried to solve this with an array in the onCreate method, but this is "void" and therefore I don't get a return:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(layout.sound);
final int[] selection = new int[1];
imageButton1 = findViewById(id.iv_bowl1);
imageButton1.setOnClickListener(v -> {
selection[0] = 1;
Toast.makeText(Sound.this, "1-click", Toast.LENGTH_SHORT).show();
});
return selection[0];
}
Thanks for help.
button4 = findViewById(R.id.button4);
musicSound = MediaPlayer.create(this, R.raw.music);
buttonClick(); }
private Button button4;
private MediaPlayer musicSound;
public void buttonClick() {
button4.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
soundPlay(musicSound);
}
} ); }
public void soundPlay(MediaPlayer sound) {
{ sound.start();
sound.setLooping(true); }
buttonMusicStop = findViewById(R.id.buttonMusStop);
buttonClick2();}
private ImageButton buttonMusicStop;
public void buttonClick2() {
buttonMusicStop.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
soundPlay2(musicSound);
}});}
public void soundPlay2(MediaPlayer sound) {
if (sound.isPlaying()) {
sound.pause();
} else {
sound.start();
sound.setLooping(true);
} }
Hello! I start music in my application, but when I minimize the application, the music does not stop. What do I need to write so that when the application is minimized and also when the screen is turned off, the music stops. Any help would be greatly appreciated!
Seem your app is playing in background. So create onResume and onStop to stop play and release resource, somethings look like this
onStop{
sound.stop();
sound.reset();
sound.release();
mediaplayer = null;
supper.onStop();
}
Currently working on an app in Android Studio to play music from a live audio source. I got the play and pause button to work for certain url's such as this, which may only work either because it's http. However, when doing it with this link, which is live audio from a college radio station, it doesn't play anything (the static when the station is not on can't be heard in the app).
My code is below:
public class RadioSelection extends AppCompatActivity {
ImageView fm;
ImageView digital;
MediaPlayer mediaPlayer;
ImageView playPause;
String stream = "http://wmuc.umd.edu:8000/wmuc-hq";
boolean prepared = false;
boolean started = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_radio_selection);
mediaPlayer=new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
new PlayerTask().execute(stream);
playPause = findViewById(R.id.play_pause);
fm = findViewById(R.id.fm);
digital = findViewById(R.id.digital);
playPause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (started) {
started = false;
mediaPlayer.pause();
Toast.makeText(RadioSelection.this, "Paused", Toast.LENGTH_SHORT).show();
} else {
started = true;
mediaPlayer.start();
Toast.makeText(RadioSelection.this, "Playing", Toast.LENGTH_SHORT).show();
}
}
});
fm.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
digital.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
private class PlayerTask extends AsyncTask<String, Void, Boolean> {
#Override
protected Boolean doInBackground(String... strings) {
try {
mediaPlayer.setDataSource(strings[0]);
mediaPlayer.prepare();
prepared = true;
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("It's prepared!");
return prepared;
}
#Override
protected void onPostExecute(Boolean aBoolean) {
super.onPostExecute(aBoolean);
}
}
#Override
protected void onPause() {
super.onPause();
if (started) {
mediaPlayer.pause();
}
}
#Override
protected void onResume() {
super.onResume();
if (started) {
mediaPlayer.start();
}
}
#Override
protected void onDestroy() {
super.onDestroy();
if (prepared) {
mediaPlayer.release();
}
}
}
I feel like it has to do with the protocol but I'm not sure, and I don't really know of a way to test if it actually successfully connected to the audio source. I'd really appreciate any help.
i have buttons to play sounds, they all works individually, after 14+ uses they dont work anymore if i doesn't restart the app
already tried to mediaplayer.realease after each use, doesnt work, think it is a memory problem dont know how do deal with it
MediaPlayer SonDeadliest
protected void onCreate(Bundle savedInstanceState)
{
boutonDeadliest.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
SonDeadliest = MediaPlayer.create(getApplicationContext(),R.raw.themostdeadliestganginuganda);
SonDeadliest.start();
fin(SonDeadliest);
}
});
public void fin(MediaPlayer m)
{
if(m.isPlaying())
{
}
else
{
m.stop();
m.release();
}
}
i have like 24 buttons, i'm trying to get an app with everything that works without having to restart the application thank you :)
The problem is that using MediaPlayer repeatedly in this way can be misleading:
SonDeadliest = MediaPlayer.create(getApplicationContext(),R.raw.themostdeadliestganginuganda);
SonDeadliest.start();
It will consume unnecessary resources.
For a better understanding I suggest that you read this post. It is written using Kotlin but with a little of kotlin study you can understand.
Here is a implementation in Java, trying to use your scenario:
MediaPlayer myMediaPlayer;
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//Media Player setup
myMediaPlayer = new MediaPlayer();
myMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
myMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.reset();
}
});
//Buttons
Button button1 = view.findViewById(R.id.button_record1);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
playSound(R.raw.record1);
}
});
Button button2 = view.findViewById(R.id.button_record2);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
playSound(R.raw.record2);
}
});
}
void playSound(int rawResId) {
try {
AssetFileDescriptor afd = getContext().getResources().openRawResourceFd(rawResId);
if (afd != null) {
myMediaPlayer.reset();
myMediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
myMediaPlayer.prepareAsync();
}
} catch (Exception ex) {
Log.d("MediaPLayer", "playSound exception: " + ex.getMessage());
}
}
The code can be improved, but my intention here was just to facilitate the comprehension. I'm using only 2 buttons for simplification. But I suggest you to consider using better solutions instead of creating a listener to each one of the 24 buttons. (Take a look on DataBinding, passing the resource as reference).
I'm using a simple activity (from this library) with ZXing to scan a barcode.
Everything works fine, but the transition between the MainActivity and the ScannerActivity doesn't work well. Halfway through it gets interrupted. When going back from the ScannerActivity to the MainActivity the transition works like desired. Only while loading the barcodescanner it doesn't look really good.
Do you have any idea how to fix this?
MainActivity:
private void invokeScanner() {
try {
Intent intent = new Intent(this, ScannerActivity.class);
stealFocus(et_loadInput);
startActivityForResult(intent,0);
this.overridePendingTransition(R.anim.detail_anim_up, R.anim.detail_anim_down);
} catch (Exception e){
Log.e(TAG, "couldn't open scanner");
e.printStackTrace();
openAlertDialog(Const.MSG_PROCESSING_ERROR);
}
}
ScannerActivity:
public class ScannerActivity extends AbstractTitleBarActivity implements ZXingScannerView.ResultHandler{
private ZXingScannerView mScannerView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mScannerView = new ZXingScannerView(this);
setContentView(mScannerView);
this.tv_backTitle.setText(getString(R.string.TITLE_SEARCH_VIEW));
this.tv_heading.setText("");
TextView titleClose = (TextView) findViewById(R.id.btn_close);
titleClose.setVisibility(View.VISIBLE);
titleClose.setOnClickListener(titleCloseListener);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
#Override
public void onResume() {
super.onResume();
mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
mScannerView.startCamera(); // Start camera on resume
}
#Override
public void onPause() {
super.onPause();
mScannerView.stopCamera(); // Stop camera on pause
}
#Override
protected boolean isBackBtnVisible() {
return false;
}
#Override
protected boolean isLogoVisible() {
return false;
}
#Override
public void handleResult(Result rawResult) {
Log.v(TAG, rawResult.getText()); // Prints scan results
Log.v(TAG, rawResult.getBarcodeFormat().toString()); // Prints the scan format (qrcode, pdf417 etc.)
MainActivity.scanVal = rawResult.toString();
MainActivity.loadVal = MainActivity.scanVal;
finish();
overridePendingTransition(R.anim.detail_anim_back_down,R.anim.detail_anim_back_up);
}
// listener for close button
protected TextView.OnClickListener titleCloseListener = new TextView.OnClickListener() {
public void onClick(View v) {
finish();
overridePendingTransition(
R.anim.detail_anim_back_down, R.anim.detail_anim_back_up);
}
};
}
I actually solved my own problem :D with this answer.
I just put the ZXingScannerView into a fragment and added a surface view with 0px height as a sibling. Now the animation works fine and the barcode scanner loads without refreshing the complete activity.