I have been working on an app, and it involves sound.
Now, I use this to start it:
final MediaPlayer tick = MediaPlayer.create(getApplicationContext(), R.raw.tick_16);
tick.start();
But for some reason, after some time (depends on the device, on HTC One it is a few seconds and in others a few minuets) all sound stopps.
What is the cause of it and how can I solve it?
Use this tutorial to play audio :
http://www.tutorialspoint.com/android/android_mediaplayer.htm
you may be use tick.start(); in onResume() method of your activity class
Try starting the MediaPlayer in a Service and a Notification inside startForeground().
Related
I have a List of Voice Recordings. Everything works well until, when one MediaPlayer is running and i clicked on another MediaPlayer without pausing the current one, seekbar of the current MediaPlayer also runs with the new MediaPlayer.
I tried everything possible i could from setting certain conditions to it but nothing is really working.
Please anyone there to Help me!
I have a problem regarding playing a sound in an activity, I have alarm manager and I use media player to play sounds, now when I try to set two alarms (assume that these two has only 1 minute difference from their time) when the first alarm alarms and I don't close that activity then waiting for another minute another activity pops out then plays the sound too so there's two sounds playing in background.
I want to make the previous activity's sound stop when another alarms on front
In your manifest put this attribute into activity:
<activity
android:name=".YourActivity"
android:launchMode="singleTop">
What the launchMode means and which possible states there are, see here in the docs: https://developer.android.com/guide/topics/manifest/activity-element.html
Then your activity will only be started once. If it is already started, it just comes to foreground and no second one comes up. For MediaPlayer:
if(mediaPlayer!=null){
if(mediaPlayer.isPlaying()){
mediaPlayer.stop();
mediaPlayer.release();
}
mediaPlayer = MediaPlayer.create(this, R.raw.your_sound);
mediaPlayer.start();
}
Be sure that you donĀ“t call mediaPlayer.isPlaying() if you have released the mediaPlayer before. This will throw an illegalStateException.
This is just from scratch, you have to adjust it to your needs.
I've figured out that the MediaPlayer on Android 4.4.2 seems to fade-in the Audiofile automatically.
I am using the MediaPlayer for playing a Sound that mustn't be faded in.
It worked well with a Smarthpone with 4.2.1, but on another Phone with 4.4.2 the Fading occurs.
I've also had a look at the SoundPool, but it misses the Feature of letting me know if the File is still playing.
Am I able to switch off the automatic fade-in or do I need to use the Soundpool and keep an eye on the length of the Track on my own?
Thanks,
VanDahken
You can try to setAudioAttributes(AudioAttributes attributes) before start playing.
atrs = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_MEDIA)
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.build();
Try to play with CONTENT_TYPE.
I have the same issue. The good thing is that it worked on a couple other Android versions, so hopefully only 4.4.2 has the problem. My first solution I discovered was to use an uncompressed music file... (.wav with data format LEI16) but the music file was way too big.
I also found that the fade-in doesn't seem to happen if you set the audio stream type to STREAM_RING but that's probably not a good idea since it's meant for phone rings.
For my app I only have one critical spot (when the app first starts) where the music has to play without the fade-in, and I was able to get a fix for that. The intro takes a few seconds before the music starts, so what I am doing is:
-prepareAsync() at the start of the intro
-then when prepared, setVolume() to 0f and start()
-then, after a small delay (for my case that ends up being a few seconds: the time it takes for the app's intro to complete), add a seek listener, and seekTo(0)
-when the seek completes, set the volume to the desired value
I'm making a small android app like image below:
I have a sound button. All I want is background music will play across all activity and when I click on sound button, music will stop. Or click to play music again.
I dont know how to make it. PLease help me! or give me link to refer.
I'd gladly appreciate your help it Thanks!
Hi please Follow below steps
initialized
MusicPlayer object when your app starts MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.song);
set this in your button's click event.
Start it by mediaPlayer.start();
3.Stop it by mediaPlayer.stop();
but when you stopping mediaPlayer before that just check mediaPlayer.isPlaying() with if Condition. then Stop. else error thrown.
or follow this Example Turorial Link
In order for media to be played in the background of your app location when the user is interacting with it you must start a Service from your application's main activity and the service shall contain all the methods related to playback. To allow activity to interact with the service, a service connection is also required. In short, we need to implement a bounded service.
Refer http://www.codeproject.com/Articles/258176/Adding-Background-Music-to-Android-App
I had the Android App which play sound when specific times , I did my
code well but when I locked the screen the sound isnot play , I
checked my code and I find that the code that I added when user press
lock button is the reason of the problem .How to solve this issue ?
#Override
protected void onPause() {
Player.stopAzan();
finish();
super.onPause();
}
Maybe try using a service? Services are basically same that activities but they run in background and have no content view if I get them right :D
http://www.vogella.com/tutorials/AndroidServices/article.html
What you are trying to achieve is impossible since when the screen is locked, the Activity is stopped.
You either decide if want to play in background or not, because when the screen is locked, the Activity goes background.
If you don't need to play in background then you're good to go, just remove the finish() method.
If you do need to play the music in background, use a Service to start and stop player based on Intents passed from Activity user controls.