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!
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 trying to open a default intent of android gallery (screenshot given below).
for this i have referred some
SO Questions but none of them are worked for me Question and this Question
here's a screenshot what i want...samsung device's gallery
this is a working app on playstore..wallpaperApp
can Anyone please tell me how can i achieve this?
Thanks In Advance!!
You can use wallpaper manager to set image as wallpaper.
WallpaperManager myWallpaperManager
= WallpaperManager.getInstance(getApplicationContext());
try {
myWallpaperManager.setResource(R.drawable.wallpaper);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}});
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;
}
Until now I have been using the Timer that's in javax.swing.timer - all I had to do was to choose the speed and have a method with a specific name that executes what I want the timer to do every time.
I have now started programming an Android application recently and I have faced a couple of problems...
It wouldn't let me use or import javax.swing.timer
I tried using java.util.timer but I couldnt figure out how it works
All I want my timer for is so I can display my logo for 3 seconds and then proceed to the main menu - even if there is an easier solution for this, I'd still like to know how to use the timer
For the person who told me to try using a thread - here is my code - it doesnt paint the first screen at all, it just stays blank for 3 seconds then moves on to the next line of code...
public class logo extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
setContentView(R.layout.logoview);
Thread.sleep(3000);
}
catch (InterruptedException e){
e.printStackTrace();
}
setContentView(R.layout.main);
}
public void startup (View view){
Intent intent0 = new Intent (this,ExpiramantingActivity.class);
startActivity(intent0);}
}
}
in onCreate() function of your SplashActivity class, schedule a handler to display your main menu activity after 3 seconds. this should be done after setContentView(R.layout.splashscreen);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
startActivity(new Intent(SplashActivity.this,
YourMainMenuActivity.class));
finish();
}
}, 3000);
I believe that what you are trying to do is similar to the exhibition Splash Screen. If so, please check this Oracle tutorial.
try this:
// logo
Thread timer = new Thread(){
public void run(){
try{
sleep(3000);
} catch (InterruptedException e){
e.printStackTrace();
}
};
};
timer.start();
//next code
Maybe you can use a Thread? I think it's the simplest way to do what you want:
Where you display your logo:
try {
//show your logo here
Thread.sleep(3000); //3 seconds
} catch (InterruptedException e) {
e.printStackTrace();
}
Surrounding the code in a try/catch block is very important, because of the posibility of an Exception.
I have this problem, I have some audio I wish to play...
And I have two buttons for it, 'Play' and 'Stop'...
Problem is, after I press the stop button, and then press the Play button, nothing happens. -The stop button stops the song, but I want the Play button to play the song again (from the start) Here is my code:
final MediaPlayer mp = MediaPlayer.create(this, R.raw.megadeth);
And then the two public onclicks:
(For playing...)
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
button.setText("Playing!");
try {
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.start();
//
}
});
And for stopping the track...
final Button button2 = (Button) findViewById(R.id.cancel);
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mp.stop();
mp.reset();
}
});
Can anyone see the problem with this? If so could you please fix it... (For suggest)
Thanks alot...
James
You need to call prepare() or preparAsync() before start().
See the API for details (look at the state diagram).
There is a bug in the Android documentation, in this page it is said that you could stop() a "raw resource" mediaplayer, and then replay it just by calling reset() and prepare() before calling start() again. This doesn't work, as you have noticed.
The problem is that reset() clears the audio source and returns to the initial state, so you must set again the data source. Unfortunatelly you can't set a "raw resource" data source, because there is no API for this besides create().
I don't know a clean way of solving this issue. stealthcopter's way works great, but is a pain for your design, as you need the context for each start() call :( And involves destroying and creating a complex object, which has a price for realtime apps like games...
Another way that ensures that context will only be needed for the create() call, is to stop the media player this way:
stop()
prepare()
but if you call start() now, it won't restart from the beginning. You could call seekTo(0), but the sound will have a bit of noise from the previous play position.
I keep investigating on this. There must be a clean and efficient way of stopping and restarting the mediaplayer when created on a raw resource...
This is what I have working in my program. It releases the media player each time as I use different sounds each time it is called, however it should work as a work-around for your usage.
Creation:
public MediaPlayer mp=null;
Starting:
if (mp!=null){
mp.reset();
mp.release();
}
mp = MediaPlayer.create(test.this, soundResource);
mp.start();
Stopping:
mp.stop();
Also note that you do not require to use prepare because the create method already calls prepare for you (API REF).