Android Interaction - Music - java

I have an Android app that contains a String with numbers between 1 and 6 (e.g 165231543214).
I need my app to look through each char of the string and depending on the number, play a 1 second clip from a .wav file. (This I can manage)
However whenever code comes across the number 2, I require the app to wait for the user to press the screen (or a button) before the .wav file is played, and then carry on playing other .wav files until it comes across a number 2 again or runs out of numbers to play.
I just started out with Android a couple of weeks ago and have read into Services and asynkTasks but I am unsure how to proceed exactly.
Any guidance would be great!

try this -
String song = {"path to sound for 1","path to sound for 2"and so on..}
MediaPlayer mediaPlayer = new MediaPlayer();
int i=0;
//initialise playButton
playButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){ //implement onClick to play first sound
playSong((Integer.parseInt(str.charAt(i++)))-1);
}
}
playSong(int songIndex){
mediaPlayer.reset();
try{
mediaPlayer.setDataSource(song[songIndex]);
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.start();
}

Related

Java - Playing music one after another without breaks

I'm trying to play a music file, and then a second file which is a second part of the song. The problem is, the transition between the two is very lacking. After one part finishes playing, there's about a second of silence, and then the second part starts playing.
In best case, both files would play seamlessly one after another. I tried using a second MediaPlayer, preparing it beforehand so the only thing left is to start it, but it didn't help. Is there a way to start playing the second part earlier?
Here's the code:
// MediaPlayers are created as "music" and "musicfollow"
//setting up files for source
AssetFileDescriptor anticip = getAssets().openFd("file1.ogg");
AssetFileDescriptor afdass1 = getAssets().openFd("file2.ogg");
//setting a new source for player
music.reset();
try {
music.setDataSource(anticip.getFileDescriptor(), anticip.getStartOffset(),anticip.getLength());
music.prepare();
} catch (IOException e) {
e.printStackTrace();
}
//preparing follow-up music player
musicfollow.reset();
musicfollow.setLooping(true);
try {
musicfollow.setDataSource(afdass1.getFileDescriptor(), afdass1.getStartOffset(),afdass1.getLength());
musicfollow.prepare();
} catch (IOException e) {
e.printStackTrace();
}
//playing part 1
music.start();
//changing the MediaPlayer to follow-up player
music.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
musicfollow.start();
}
});
}

mediaPlayer.release() crashes my app java

I have a project where I added a MediaPlayer in a custom Array Adapter and I make it take the Resource ID of the audio file from the Array List the problem is that I want to release the media player after the audio completes but every time I try to add release in OnCompletion and try to press a view while the media player plays a sound the app crashes .. I searched online and found a code but it gives me an error and I can't find the actual reason for such a crash.
that's the code for the on Click Listener and the Media player:
final MediaPlayer mediaPlayer = MediaPlayer.create(getContext(), currentWord.getVoiceResourceID());
// Wait for user's input by his click on the list's item to play the sound.
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
// Storing the return value of getVoiceResourceID() into mVoice.
// I think this line can be ignored so we call the method inside the creation of the Media Player.
// Creating our media player and put our track on it.
mediaPlayer.selectTrack(currentWord.getVoiceResourceID());
mediaPlayer.start();
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mediaPlayer.release();
}
});
}
};
I hope I could fix that problem.

Android MediaPlayer seekTo() doesn't work

I have an Android app with two media players close to eachother, playing videos. The loop is enabled. When it is running, I notice one of the videos get a bit off from the other for a few hundred ms which is noticeable. The two videos are crops of the same video that's why when the guy moves, it gets clear there is an edge. I was trying to manually add a few milliseconds to the other one using
mp[1].seekTo(mp[0].getCurrentPosition()+700)
but unfortunately it gets off as time passes. And everytime it gets different. I was using Thread and set the mediaPlayer.start() of the two players at the same time (as this post suggested), but that also didn't help. How can I solve the problem? Any thoughts?
mp[0] = mediaPreparation("waldo_short_1.mp4", false);
mp[1] = mediaPreparation("waldo_short_2.mp4", false);
mp[0].start();
mp[1].start();
mp[1].seekTo(mp[0].getCurrentPosition()+705);
MediaPlayer mediaPreparation(String filename, boolean setMute) {
// create mediaplayer instance
MediaPlayer mediaPlayer = new MediaPlayer();
AssetFileDescriptor afd;
try {
afd = getContext().getAssets().openFd(filename);
mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
afd.close();
mediaPlayer.prepare();
} catch (IOException e) {
}
mediaPlayer.setLooping(true);
// mediaPlayer.start();
return mediaPlayer;
}

mp3 stops playing after it worked a few times?

I have a program that plays a short, 1 sec. .mp3 sound on every buttonclick (every time a different sound). After clicking the buttons a few times, the sound suddenly stops at all for the whole program, I have another background sound on another activity, If I start this activity after no sound is coming anymore, no sound is coming on this activity either.
code example of the buttonclickers:
mp1 = MediaPlayer.create(this, R.raw.s1);
mp2 = MediaPlayer.create(this, R.raw.s2);
i++;
if (i == 1) {
mp1.start();
}
if (i == 2) {
mp2.start();
}
Make sure you reset and release your media player when it is done playing!
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
mediaPlayer.reset();
mediaPlayer.release();
}
});
Also reset the integer variable.
if(i == 2){
i = 0;
}

Check and see if a sound is playing, wait for it to finish and then play another

What i am trying to acomplish is for me to click on a button and then play a sound based on a substring.
This is my current code for pressing the button :
display.setText("start");
thefull = thef.getText().toString();
for(int a=0;a<thefull.length();a++)
{
letter=thefull.substring(a,a+1);
if(letter=="m")
{
oursong = MediaPlayer.create(MainActivity.this, R.raw.m);
oursong.start();
while(oursong.isPlaying())
{
display.setText("start");
}
oursong.release();
}
else if(letter=="a")
{
oursong = MediaPlayer.create(MainActivity.this, R.raw.a);
oursong.start();
while(oursong.isPlaying())
{
display.setText("start");
}
oursong.release();
}
display.setText("done");
but for some reason when i click my button no sound is played.
I am also new to android and java programming, so am i going right about doing this?
Because what i really want is for the program to keep checking if a sound is playing(in this case "oursound) when the button is clicked and if the sound is playing i want the program to wait for it to finish to start another sound right after it.
But for now my code doesn´t play any sounds
First of all you need to stop the media player before releasing it(its safer.)
Second, instead of using while(oursong.isPlaying())
{
display.setText("start");
} ; you have to register OnCompletionListener using setOnCompletionListener method. When the song is played endCompletion will be called. Like so;
MediaPlayer mp = new MediaPlayer();
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener(){
public void onCompletion(MediaPlayer player) {
player.release();
}});
and inside the onComplete should reset the text "done"

Categories