3

I have to implemented application for creating audio with the status of pause and resume and when my app as an when start the audio is start and when I press the back button on the emulator the audio music is on pause state but When my activity comes back to the foreground from the stopped state my audio music is not resumed. Here is my code.

public class Audio_Activity extends Activity 
{
    private  MediaPlayer mp;
    Button btnStartStop ;
    Button btnChapter ;
    Button btnOne;
    Button btnTwo;
    Button btnThree;
    Button btnFour;
    Button btnFive;
    int length;


    ImageView imgVw;
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.audio);
        init();

        mp=MediaPlayer.create(Audio_Activity.this,R.raw.ennamo_yadho);
        Log.e("Song is playing","in  Mediya Player ");

        if(mp!=null)
        {
                mp.setLooping(false);
                mp.start();
                btnChapter.setEnabled(false);
                System.out.println("B4 button Click!!!!");
        }

        mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() 
        {
            @Override
            public void onCompletion(MediaPlayer mp) 
            {
                mp.stop();
                mp.release();
                btnChapter.setEnabled(true);
                System.out.println("Music is over and Button is enable !!!!!!");
            }
        });


        btnStartStop.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View arg0) 
            {
                if(mp.isPlaying())
                {
                    if(mp!=null)
                    {
                            mp.pause();
                    }

                }

                else
                {
                    // Resume song
                    if(mp!=null)
                    {
                            mp.start();
                    }
                }
            }
        });


        btnOne.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v) 
            {
                imgVw.setImageResource(R.raw.chocklate);
                }
            }
        );

        btnTwo.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                imgVw.setImageResource(R.raw.creame);
            }
        });

        btnThree.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                imgVw.setImageResource(R.raw.schocklate);

            }
        });

        btnFour.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                imgVw.setImageResource(R.raw.pinapple);

            }
        });

        btnFive.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                imgVw.setImageResource(R.raw.strobery);

            }
        });
    }

     @Override
        protected void onResume() 
        {
            super.onResume();
            System.out.println("Activity is Resume !!!");
        }

    @Override
    protected void onStart() 
    {
        super.onStart(); 
        System.out.println("Activity is Started !!!");
    }


     @Override
        protected void onRestart() {
            super.onRestart();
            System.out.println("Activity is Re-Started !!!");
            if(mp.isPlaying())
            {
                if(mp!=null)
                {
                    length=mp.getCurrentPosition();
                    mp.seekTo(length);
                    mp.start();
                }
            }

        }



        @Override
        protected void onPause() {
            super.onPause();
            System.out.println("Activity is Pause!!!");
        }

        @Override
        protected void onStop() {
            super.onStop();
            System.out.println("Activity is Stop !!!");
        }

        @Override
        protected void onDestroy() {
            super.onDestroy();
            System.out.println("Activity is Destroyed !!!");
        }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) 
    {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) 
        { //Back key pressed
           //Things to Do
            if(mp!= null)
            {
                if(mp.isPlaying())
                {
                    mp.pause();
                    //mp=null;
                }
            }
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}
BenMorel
  • 34,448
  • 50
  • 182
  • 322
tazeen
  • 145
  • 1
  • 5
  • 18

2 Answers2

0

You should use android MediaPlaybackService for background play,Pause, stop or just open the activity by clicking the Notification. when click on back button it will bind a PendingIntent to with the button click event or buttons on the notification bar for controlling audio playbacks.

Use this Gist Code for AudioPlayer, try o use MediaPlaybackService class or try to reverse engineer this owncloud code.

LOG_TAG
  • 19,894
  • 12
  • 72
  • 105
-1

Your code is not correct. You should not bother backbutton etc, but activity life cycle. You should pause your audio in onPause() and resume in onResume(). So move code to onPause() and get rid of onKeyDown() and move code from your onRestart() to onResume(). And remove all methods you do not need, line onDestroy(), onStart() etc

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • When i click the back-press button my music is pause but when i restart my activity my music is not resume.Music is start from the starting position not from the current position where music is in pause.How can do this .I don't understand. And i also removed method onDestroy() , onStart() , onKeyDown() .But even my music is not resum. – tazeen Jul 08 '13 at 10:06
  • how to start audio from current position when restart my activity in Android?? Please give me the solution for this . – tazeen Jul 08 '13 at 10:34
  • Why does my audio restart when i restart my activity. – tazeen Jul 08 '13 at 10:36