adding background audio in android app - android

I was trying to add background sound for 5 sec. in the introductory screen of my app. I tried it on avd as well as on real phone but sound isn't working. where am i wrong?
I am copying java code of my main screen...
package com.example.akg;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
public class Opening extends Activity {
MediaPlayer song;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_opening);
Thread tim=new Thread(){
public void run(){
try{
song=MediaPlayer.create(Opening.this, R.raw.music);
sleep(5000);
}catch(InterruptedException e){
e.getStackTrace();
}finally{
Intent inten=new Intent(Opening.this, MainActivity.class);
startActivity(inten);
}
}
};
tim.start();
}
protected void onPause(){
super.onPause();
song.release();
finish();
}
}

To play background sound to your activity, use following code:
try {
song=MediaPlayer.create(Opening.this, R.raw.music);
song.prepare();
song.start();
} catch (Exception e) {
e.printStackTrace();
}
And to stop background sound, use:
#Override
public void onPause() {
super.onPause();
if(song != null && song.isPlaying()) {
song.stop();
}
}
This link might be helpful to you:
Media Player with Play/Pause, Rewind, Forward, Previous and Next functionality with Seekbar

To play the sound, you need to call:
song.start();
From the android documentation for MediaPlayer:
To start the playback, start() must be called. After start() returns successfully, the MediaPlayer object is in the Started state. isPlaying() can be called to test whether the MediaPlayer object is in the Started state.

Related

How to add BGM to my Android game

I've search alot but i couldn't find the full answer , anyway i've added a service to my game but now the problem is when i press the home/app list button the service ( BGM ) is still on , i've alot try to stop the service on (onPause function) in the main activity but my game use more then one activity , so when i start another activity the BGM is lost ( cuz it's calling MainActivity->onPause function )
so what i sohuld do ?
(my Serivce class)
package com.hema.mrlibya.firstgame;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
/**
* Created by MrLibya on 18/04/17.
*/
public class BGMusic extends Service {
MediaPlayer mMediaPlayer;
#Override
public void onCreate(){
super.onCreate();
mMediaPlayer = MediaPlayer.create(this,R.raw.bg);
}
public int onStartCommand(Intent intent, int flags, int startId) {
mMediaPlayer.setLooping(true); // Set looping
mMediaPlayer.start();
return START_NOT_STICKY;
}
#Override
public IBinder onBind(Intent arg0) {
return null;
}
#Override
public void onDestroy() {
mMediaPlayer.stop();
mMediaPlayer.release();
}
/*
public void onPause() {
mMediaPlayer.pause();
length=mMediaPlayer.getCurrentPosition();
}
public void onResume(){
mMediaPlayer.seekTo(length);
mMediaPlayer.start();
}
*/
}
(Manifest)
<service android:enabled="true" android:name="BGMusic" />
(In the MainActivity i've only write on ( onCreate function )
startService(new Intent(this, BGMusic.class));
Firstly: I suggest not to use a service at all, since it is not necessary. Just instantiate the music player in the application. But that is up to your preference.
Secondly (your actual problem): When switching activies just set a flag e.g. keepMusicOn = true; and then
public void onPause() {
if(!keepMusicOn){
mMediaPlayer.pause();
}
//reset flag so that the music stops next time onPause is called
keepMusicOn = false;
...
}
I found the solotuion here
it's work 100%

Losing the control of the mediaplayer when the activity is recreated

In my project I am trying to make a media player which plays a shoutcast stream. Everything seemed to be working well until I pressed the back button on my device, which I think stops the activity and causes the device to recreate the activity when launched again. The problem is , when the activity is recreated , I lose the control of the mediaplayer and a new mediaplayer is created.
I need to be able to have the mediaplayer's control back at that point. How is it possible?
This part of code belongs to onCreate
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button);
if (mediaPlayer == null){
mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(getString(R.string.yayin));
} catch (Exception e) {
e.printStackTrace();
}
mediaPlayer.prepareAsync();
}
if(!isPlaying){
btn.setBackgroundResource(R.drawable.oynat);
}
else{
btn.setBackgroundResource(R.drawable.durdur);
}
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(!isPlaying){
playOnReady();
isPlaying = true;
btn.setBackgroundResource(R.drawable.durdur);
}
else{
mediaPlayer.reset();
isPlaying = false;
btn.setBackgroundResource(R.drawable.oynat);
}
}
});
This part of code belongs to the function playOnReady()
private void playOnReady(){
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
}
Take a look at the Android Activity lifecycle flowchart: https://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
You need to account for the path where onPause is called when you leave the Activity and then onResume is called when you enter it again. The solution for you could be as simple as moving some/all of your code from onCreate into onResume.
Using this tutorial I have managed to build a service which handles the mediaplayer that I can have the control of anytime I need.

Sound comes from the app when it is paused

I have problems with my app because multimedia sound is heard when the app is in background
I have defined my Media player like this;
private void playLocalAudio(int R1)throws Exception
{
MediaPlayer mediaPlayer = MediaPlayer.create(this,R1);
mediaPlayer.start();
}
For calling PlayLocalAudio I do:
try{
playLocalAudio(R.raw.fartw1);
}
catch (Exception e) {
e.printStackTrace();
}
}});
But I am not able to call correctly MediaPlayer.Stop()
I am trying:
public void onPause()
{
super.onPause();
mediaplayer.stop();
}
But it doesn't work. Could you help me?
I'm guessing that your code has a class variable mediaPlayer that's not visible in your example. In that case you have variable shadowing, because you're instantiating a new mediaPlayer in playLocalAudio and that instance is not visible inside the pause method. So stop is never called. Remove the MediaPlayer class name from the declaration in playLocalAudio.

Android Mediaplayer Play/Stop help needed

I am trying to create my first android application and what I'm trying to accomplish here is to play a sound and then stop it via the same button.
It kind of works as it plays the sound when I click it and stops when I click it again but will not play when I click it the third time to start the sound again.
I'm eventually going to have a few sounds in here and so would like to know if how my project is laid out correctly? Can I save some time anywhere? Have I got something the wrong way round?
package test.soundy.com;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class TestActivity extends Activity {
private MediaPlayer sound;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sound = MediaPlayer.create(Test.this, R.raw.sound1);
Button test = (Button)this.findViewById(R.id.button1);
test.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (sound.isPlaying()) {
sound.stop();
} else {
sound.start();
}
}
});
}
}
Any help would be much appreciated, thanks.
WHEN START/PAUSE:
if(sound.isPlaying()){
sound.pause();
}else{
sound.start();
}
WHEN START/STOP:
if(sound.isPlaying()) {
sound.stop();
} else {
sound.reset();
sound.setDataSource(yourURL); //or InputStream etc.
sound.prepare();
sound.start();
}
Also you can use sound.seekTo(time) to skip to a position.
Remember when you want to play a new sound(or restart) you should first reset, setDataSource, prepare and then start it.
EDIT: get the FileDescripter
AssetManager assetManager=Context.getAssets();
AssetFileDescriptor fileDescriptor = assetManager.openFd("a2.mp3");
mediaPlayer.setDataSource(fileDescriptor.getFileDescriptor());
EDIT: I haven't found a way to turn raw file into filedescriptor so I use the static method of MediaPlayer
MediaPlayer mediaPlayer = MediaPlayer.create(Activity.this,R.raw.a1);
mediaPlayer.setOnCompletionListener(new musicCompletionListener());
mediaPlayer.start();
private class musicCompletionListener implements OnCompletionListener {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
mediaPlayer.release();
}
}
alse release the mediaplayer when stop it.
Have you tried resetting the MP?
if (sound.isPlaying()) {
sound.stop();
} else {
sound.reset();
sound.prepare();
sound.start();
}
Edited...
The full state diagram is here: http://developer.android.com/reference/android/media/MediaPlayer.html

How to resume the mediaplayer?

I am using a media player.
I have the option for starting ,stopping and pausing the player. The problem I have is that I cannot find the option to resume the song from the point that it previously was paused..
Any help provide would be really helpful.
Thank you for your attention but I've got it myself
for pausing the Mediaplayer I used:
Mediaplayer.pause();
length=Mediaplayer.getCurrentPosition();
and for resuming the player from the position where it stopped lately is done by:
Mediaplayer.seekTo(length);
Mediaplayer.start();
I think you should go through the documentation found here: http://developer.android.com/reference/android/media/MediaPlayer.html
Some quotes from the docs:
Playback can be paused and stopped,
and the current playback position can
be adjusted. Playback can be paused
via pause(). When the call to pause()
returns, the MediaPlayer object enters
the Paused state. Note that the
transition from the Started state to
the Paused state and vice versa
happens asynchronously in the player
engine. It may take some time before
the state is updated in calls to
isPlaying(), and it can be a number of
seconds in the case of streamed
content.
Calling start() to resume
playback for a paused MediaPlayer
object, and the resumed playback
position is the same as where it was
paused. When the call to start()
returns, the paused MediaPlayer object
goes back to the Started state.
Calling pause() has no effect on a
MediaPlayer object that is already in
the Paused state.
States explained:
And quote from the start() method of the MediaPlayer
public void start ()
Starts or resumes
playback. If playback had previously
been paused, playback will continue
from where it was paused. If playback
had been stopped, or never started
before, playback will start at the
beginning.
So to answer your question directly, to resume the paused MediaPlayer instance from the point where it was paused use start() again on that instance.
In the accepted answer, the correct order is:
Mediaplayer.start();
Mediaplayer.seekTo(length);
in case you use two Buttons one for play and one for pause then the below code is working and tried:
playbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mPlayer = MediaPlayer.create(MainActivity.this,
R.raw.adhan);
if (mPlayer.isPlaying()) {
} else {
mPlayer.seekTo(length);
mPlayer.start();
}
}
});
pausebtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mPlayer.pause();
length = mPlayer.getCurrentPosition();
}
});
public class MainActivity extends AppCompatActivity {
MediaPlayer mediaPlayer;
public void play(View view) {
mediaPlayer.start();
}
public void pause(View view){
mediaPlayer.pause();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mediaPlayer= MediaPlayer.create(this,R.raw.someaudio);
}
}
Make two buttons for play and pause. And use this code. It worked for me.
what about this way?
package com.mycompany.audiodemo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.media.MediaPlayer;
import android.view.View;
public class MainActivity extends AppCompatActivity {
MediaPlayer mediaPlayer=null;
int playPosition=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mediaPlayer = MediaPlayer.create(this,R.raw.sampleaudio);
}
public void playAudio(View view){
//mediaPlayer.seekTo(playPosition);
mediaPlayer.start();
}
public void pauseAudio(View view){
mediaPlayer.pause();
//playPosition = mediaPlayer.getCurrentPosition();
}
}

Categories

Resources