hi I am using the sound class for my music so it will loop correctly (as advised here Libgdx: Lags in soundtrack looping) but when i close the app(it stills runs in the background) and open it again the sound starts to play over the previous sound, how can i avoid this?
This is my current music handler class
public class MusicHandler {
private Sound bgMusic;
public MusicHandler(Sound backgroundMusic){
bgMusic = backgroundMusic;
}
public void update(boolean play){
if(play){
bgMusic.loop();
}
else{
bgMusic.pause();
}
}
public void dispose(){
bgMusic.dispose();
}
}
Related
I am playing a music in Libgdx game. I wish to have a listener like this to play the music and do a specific process after that. But the program control never reaches the onCompletionListerner part.
private void playMusic() {
gameMusic = Gdx.audio.newMusic(Gdx.files.internal("data/bgm.mp3"));
gameMusic.play();
gameMusic.setOnCompletionListener(Music.OnCompletionListener(){
#Override
public void onCompletion(Music aMusic){
actionResolver.getLeaderboardGPGS();
}
}
);
Just add new before Music.OnCompletionListener() and it should work.
gameMusic.setOnCompletionListener(new Music.OnCompletionListener() {
#Override
public void onCompletion(Music aMusic) {
actionResolver.getLeaderboardGPGS();
}
});
Since you're passing an object (that implements OnCompletionListener), it has to be created with new as always.
Can Android live wallpaper create sound effects based on the touch of users?
I don't see alot of live wallpaper with sound effects out on the market. Is there a reason for it? For e.g, battery drain or other programming issues?
You can use a MediaPlayer in your WallpaperServiceClass.
You need to put the .mp3 or whatever sound resource you
want to use in your /res/raw folder.
public class MyWallpaperService extends WallpaperService {
private MediaPlayer mediaPlayer;
#Override
public void onCreate() {
super.onCreate();
mediaPlayer =
MediaPlayer.create(getApplicationContext(),
R.raw.your_sound);
}
// and in your WallpaperEngine SubClass start the sound on touch :
class MyWallpaperEngine extends Engine {
#Override
public void onCreate(SurfaceHolder surfaceHolder) {
super.onCreate(surfaceHolder);
// touch is not activated on default so do it here :
setTouchEventsEnabled(true);
}
#Override
public void onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mediaPlayer.start();
}
}
}
To stop the sound use :
mediaPlayer.pause()
or when you're done kill the mediaPlayer with :
mediaPlayer.release();
Shouldn't drain unnecessary resources then since its only reacting on touch.
Bit of a strange one this and I can't work out what's happening.
When I launch my app (a game) the music starts playing. I have a button which turns the music on and off. The settings are saved to shared prefs so they are retained.
All works well, you can press the home key, re-invoke the app, leave it in the background while doing other things etc. However, if the app is left in the background for a while (say, overnight), and then re-invoked. Everything works apart from the music.
You can go into the main menu, hit the 'music on/off' button multiple times, but get nothing.
The only way to start the music is to kill the app (or exit correctly, ie, press the 'back' key from the main menu) and then relaunch it so everything is re-created from scratch.
I've confirmed that the 'music' object is still valid and the 'music on/off' button presses are being registered.
Has anyone has similar issues with Media Player? I can't work out what I am (or am not doing) to cause this.
Code
This is my media player class:
public class MusicMan implements MediaPlayer.OnPreparedListener {
MediaPlayer musicPlayer;
MusicMan(Context myContext){
musicPlayer = MediaPlayer.create(myContext, R.raw.music);
musicPlayer.setVolume(.6f, .6f);
}
public void listener(){};
public void start(){
musicPlayer.setLooping(true);
musicPlayer.start();
}
public void stop(){
musicPlayer.stop();
}
public void pause(){
musicPlayer.pause();
}
public int getPos(){
return musicPlayer.getCurrentPosition();
}
public void skipTo(int position){
musicPlayer.seekTo(position);
}
#Override
public void onPrepared(MediaPlayer arg0) {
}
}
And then I simply crate an object like so:
MusicMan music = new MusicMan(view.getContext());
And then I just start and stop the music using the methods in the MusicMan class:
music.start();
You need to use
musicPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
Use this where you set new MediaPlayer player. It sets the wake lock to MediaPlayer and don't let CPU go sleep till you yourself didn't kill or stop application.
I have 6 activities in my app. I want to play different sound throughout whole app. But the problem is that when I start second activity from first activity I want same sound continue from point where its playing in first activity. But unable to find any solution on SO. how to do this? Please help me.
I am using MediaPlayer.
use the application class, or any singleton pattern for that matter you can persist data across activities
https://developer.android.com/reference/android/app/Application.html
In your activities onPause() you have to save the current progress of the track to the shared preferences, then in your activities onResume() get the progress and resume the mediaplayer...
public void onPause() {
super.onPause();
mediaplayer.pause();
int progress = mediaplayer.getCurrentPosition();
getSharedPreferences(getPackageName(), Activity.MODE_PRIVATE).edit().putInt("track_progress",progress).commit();
}
public void onResume() {
int progress = getSharedPreferences(getPackageName(), Activity.MODE_PRIVATE).getInt("track_progress",0);
mediaplayer.seekTo(progress);
mediaplayer.start();
}
Well yeah, or as #Brian says, use a singleton ... :)
Update as a singleton e.g.
public class BackgroundPlayer {
private static final String TAG = BackgroundPlayer.class.getName();
private static BackgroundPlayer instance;
public static BackgroundPlayer instance() {
if(instance==null) instance = new BackgroundPlayer();
return instance;
}
private MediaPlayer mediaPlayer;
public void startBackgroundMusic(Context ctx, int res) {
if(mediaPlayer==null) {
mediaPlayer = MediaPlayer.create(ctx, res);
mediaPlayer.setLooping(true);
}
if(mediaPlayer!=null&&!mediaPlayer.isPlaying())
Log.i(TAG,"started");
mediaPlayer.start();
}
public void stopBackgroundMusic() {
if(mediaPlayer!=null)
Log.i(TAG,"stopped");
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
}
}
Now you can access the same instance of the BackgroundPlayer anywhere in your code with
BackgroundPlayer.instance()
and you can call
BackgroundPlayer.instance().startBackgroundMusic(Context, resource);
BackgroundPlayer.instance().stopBackgroundMusic();
anywhere. Now you have to make sure you call .startBackgroundMusic in your first activities onCreate() and call .stopBackgroundMusic in the last activity on the stack, so the sound doesn't keep going when the user leaves your app.
I have 3 activities. I want to play one background music to all this activity. I made this possible by. Doing this.
In activity 1:
bgmp = MediaPlayer.create(this, R.raw.menu);
bgmp.setLooping(true);
bgmp.start();
This will make my music play up to the 3rd acitivity. At activity three. I need to stop this background music because another background music will be played when I go to the 4th activity. How can I stop the music at the 3rd acitivity that was created at the 1st activity. Any ideas? Thanks!
Define Method in common class with require parameters and use that Method in your activities.
public class CommonMethod {
public static MediaPlayer player;
public static void SoundPlayer(Context ctx,int raw_id){
player = MediaPlayer.create(ctx, raw_id);
player.setLooping(false); // Set looping
player.setVolume(100, 100);
//player.release();
player.start();
}
}
Within in your third activity, code for stop media.
CommonMethod.player.stop();
Create a service to play sound and move all your player code to service.
After that bind your activity to this service and control the music player for (play next, back, pause, stop etc.) features.
Make a singleton class and add you music playing code into it for stopping and starting and use that singleton class in all your 3 activities for eg:
public class MusicManager {
private static MusicManager refrence = null;
public static MusicManager getInstance(){
if(refrence == null){
refrence = new MusicManager ();
}
return refrence;
}
}
add a public method to this singleton class to start and stop music like
public void initalizeMediaPlayer(Context context, int musicId){
// add initalization of media player in it and loop it
}
public void startPlaying(){
// add code to start playing music
}
public void stopPlaying(){
// add code to stop playing music
}
And for making use this class use like this
MusicManager.getInstance().initalizeMediaPlayer(this, R.raw.menu); // to initalize of media player
MusicManager.getInstance().startPlaying();// to start playing music
MusicManager.getInstance().stopPlaying(); // to stop playing music
Let me know if this helps you.
Mean while you can also use service to perform this task as service runs in background . you can start service and stop service any time in your code
You Should Start Service which runs in background and write code for playing music in the service..
All you have to do is add this piece of code to your intent to the last activity:
yourMediaPlayer.pause()
So the whole code would be :
yourMediaPlayer.pause();
Intent i ...
startActivity(i);
Just put player.stop() function inside button which takes you to next activity:
example: #Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
player.stop();
Intent i=new Intent(iotmain.this,MainActivity.class);
startActivity(i);
Toast.makeText(getBaseContext(),"Chatting mode ON",Toast.LENGTH_SHORT).show();
}