Vivek
Vivek

Reputation: 1493

Android Does not Stop Music on call and Dont Resume on app icon click

I am new to android developemt. I made a music player with 3 buttons play,pause,stop. When i click on app icon Splash will run and after 3 sec it go out and when i click on play it pays the song other buttons works perfectly. But if i play the music and back to homescreen music will play but if i click on the app icon again it will start a new instance of it and if click over the play button it will start another song means 2 songs are running now.

Another problem is my songs resumes when i got the call and when i pick the call it still palying how to solve these issues.

My code for playing the music is

MediaPlayer mySong;
    Button playButton,pauseButton,stopButton,creditsButton;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activitymain);

        playButton=(Button) findViewById(R.id.play);
        pauseButton=(Button) findViewById(R.id.pause);
        stopButton=(Button) findViewById(R.id.stop);
        creditsButton=(Button) findViewById(R.id.credit);
        mySong=MediaPlayer.create(StartingPoint.this, R.raw.song);

        playButton.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                mySong.start();
            };
        });

pauseButton.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                mySong.pause();
            };
        });
stopButton.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        mySong.pause();
        mySong.seekTo(0);
    };
});

My manifest file is

 <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="9" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        >
        <activity
            android:name="com.simplyitsols.hanumanchalisa.Splash"
            android:label="@string/app_name" 
            >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.simplyitsols.hanumanchalisa.StartingPoint"
            android:label="@string/app_name" 
            android:launchMode="singleTask">
            <intent-filter>
                <action android:name="com.simplyitsols.hanumanchalisa.STARTINGPOINT" />
                <category android:name="android.intent.category.Default" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.simplyitsols.hanumanchalisa.Credits"
            android:label="@string/app_name" >
        </activity>
    </application>

Upvotes: 0

Views: 971

Answers (3)

Zahra Jamshidi
Zahra Jamshidi

Reputation: 667

According to this tutorial http://www.androidhive.info/2012/03/android-building-audio-player-tutorial/

you need to put this code in the activity's class:

@Override
public void onDestroy(){
    super.onDestroy();
    mp.release();
...
}

Upvotes: 0

Rashmi.B
Rashmi.B

Reputation: 1787

Before you hit back, clear the stack by calling finish() and kill your current activity. And for the new instance getting generated, put a boolean check whether song is currently being played or not.

   if( isSongPlaying==true)
     {
        //do not start  
      }
   else
     {
      //start playing
      }

Upvotes: 0

Daniele Vrut
Daniele Vrut

Reputation: 2873

You have to create a service: http://developer.android.com/reference/android/app/Service.html

After you have achieved that, you have to implement onPause and onResume of your activity in order to do what you want. onPause is triggered when you go back to home or you receive an incoming call. OnResume is self explanatory :)

http://developer.android.com/guide/components/fundamentals.html

Upvotes: 2

Related Questions