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;
}
Related
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();
}
});
}
I am a completely begginer guy in Android Studio and mobile apps in general. I used to create websites on Wordpress and i don't have so much experience on coding. Recently i started experimenting with mobile apps and mostly Android Studio. I bought a template for a Logo Quiz game and i managed to make it run without errors and publish it in Play Store as my first game. The player can see a part of the logo and guess the brand name using the given letters
But i would like to use the same template with new graphics and create a music quiz.
Instead of the logo guess game, the player will be able to listen a part of a song and guess the song's title.
The current project is getting the file names from a database stored in assets/databases folder.
So i managed to add start, pause and stop buttons in my activity_play.xml and succesfully created a mediaplayer in activityPlay.java file like:
public void music(View view) {
switch (view.getId()){
case R.id.button:
// Check if mediaPlayer is null. If true, we'll instantiate the MediaPlayer object
if(mediaPlayer == null){
mediaPlayer = MediaPlayer.create(this, R.raw.music);
}
// Then, register OnCompletionListener that calls a user supplied callback method onCompletion() when
// looping mode was set to false to indicate playback is completed.
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
// Here, call a method to release the MediaPlayer object and to set it to null.
stopMusic();
}
});
// Next, call start() method on mediaPlayer to start playing the music.
mediaPlayer.start();
break;
case R.id.button2:
if(mediaPlayer != null) {
// Here, call pause() method on mediaPlayer to pause the music.
mediaPlayer.pause();
}
break;
case R.id.button3:
if(mediaPlayer != null){
// Here, call stop() method on mediaPlayer to stop the music.
mediaPlayer.stop();
// Call stopMusic() method
stopMusic();
}
break;
}
}
private void stopMusic() {
mediaPlayer.release();
mediaPlayer = null;
}
// Call stopMusic() in onStop() overridden method as well.
#Override
protected void onStop() {
super.onStop();
stopMusic();
}
The above code can succesfully play the music.mp3 file located in raw folder. The app i bought is using the following code to load the images and display them for each level:
String image_a = listDataBase.get(1);
String image_q = listDataBase.get(2);
if (isTrue == 1) {
String imgPath;
if (numImage == 0) {
imgPath = "file:///android_asset/logos/" + image_a;
} else {
imgPath = "file:///android_asset/logos/" + image_q;
}
Picasso.get().load(imgPath).into(imageView);
linearLayoutNullClick.setVisibility(View.VISIBLE);
recyclerViewKeys.setVisibility(View.GONE);
trueLogo = 2;
} else {
String imgPath = "file:///android_asset/logos/" + image_q;
Picasso.get().load(imgPath).into(imageView);
recyclerViewKeys.setVisibility(View.VISIBLE);
recyclerViewLogoKeys.setVisibility(View.VISIBLE);
}
So is it possible to use the same code and load the imgPath into mediaPlayer = MediaPlayer.create(this, R.raw.music);
I tried loading imgPath directly to mediaplayer like this but didn't work:
mediaPlayer = MediaPlayer.create(this, imgPath);
Then i tried:
private String audioPath;
audioPath = imgPath;
mediaPlayer = MediaPlayer.create(this, audioPath);
but also didn't work.
Tried many more methods i found on the web, but always i am missing something
As i said before i am a newbie in coding and programming so the solution probably will be very easy .
Anyone can help please?
Are you sure you are getting a audio path in your imgPath object?
Then you can try getting actual storage path from device using code mentioned
here
If you change the value in the database of pic_1.png with audio_1.mp3
then path will start like:
"file:///android_asset/logos/" + YOUR_AUDIO;
So are you sure audio exists in this path?
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();
}
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"
I'm making a simple splash screen so that when an app loads it shows a small logo and plays a little jingle.
I've set it up as so:
splashSong = MediaPlayer.create(MainActivity.this, R.raw.splash);
splashSong.start();
Thread splashThread = new Thread(){
public void run(){
try{
sleep(6000);
}
}catch (InterruptedException e){
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
Intent openMenu = new Intent("com.mmm.MAINMENU");
startActivity(openMenu);
}
}
};
splashThread.start();
However there is an issue with the play count. When it loads the splash class, the jingle is played twice. I've changed a series of things such as preparing the song and setting the datasource. This is, however, not successful and the screen still plays the song twice.
Anybody have any ideas as to why it might be doing this?
Thanks,
Add
splashSong.setLooping(false);
Thanks to Mr. Me for the help. It was to do with the applications orientation. Removed that and all worked!