简体   繁体   中英

How to play audio continuously while orientation going to change in android

I have develop application like media player to use for playing audio file from source but issue when I am change the orientation portrait to landscape button and other widget not working and other that is when I am again change orientation landscape to portrait so same issue button and seek bar is not working

my code is here

public class Aarati_Activity extends Fragment
{
    private ImageButton btnplay;
    private SeekBar seekbar;
    MediaPlayer mp;
    private ImageButton btnstop;
    private int finalTime = 0;
    private int startTime = 0;
    private int oneTimeOnly = 0;
    Handler handler=new Handler();

    public Aarati_Activity() {}

    int audiocurrent;
    int audiomax;
    private int mp_progress;    

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.activity_aarti_fragment, container, false);

        btnplay=(ImageButton)v.findViewById(R.id.btnplay);
        btnstop=(ImageButton)v.findViewById(R.id.btnstop);
        seekbar=(SeekBar)v.findViewById(R.id.seekbar);
        seekbar.setClickable(true);

        seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

                seekchange();
            }
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) {
            }
        });



        mp = MediaPlayer.create(getActivity(), R.raw.arti);
        audiomax=mp.getDuration();
        audiocurrent=mp.getCurrentPosition();

        btnstop.setOnClickListener(new OnClickListener() {


            @Override
            public void onClick(View v) {
                if(mp.isPlaying())
                {
                        mp.stop();
                        Toast.makeText(getActivity(), "Stop",Toast.LENGTH_LONG).show();
                        btnplay.setImageResource(R.drawable.ic_action_play);

                        try
                        {
                            mp.prepare();
                        } catch (IllegalStateException e) {

                            e.printStackTrace();

                        } catch (IOException e) {

                            e.printStackTrace();
                        }


                }
                    else
                    {
                        Toast.makeText(getActivity(),"Aarti Currently not playing",Toast.LENGTH_SHORT).show();
                    }

            }
        });


        btnplay.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                if(mp.isPlaying())
                {
                    mp.pause();
                    btnplay.setImageResource(R.drawable.ic_action_play);
                    Toast.makeText(getActivity(), "Pause",Toast.LENGTH_SHORT).show();
                    finalTime = mp.getDuration();
                    startTime = mp.getCurrentPosition();
                    if(oneTimeOnly == 0)

                       {
                         seekbar.setMax((int) finalTime);
                         oneTimeOnly = 1;
                       } 
                    seekbar.setProgress((int)startTime);

                    handler.postDelayed(UpdateSongTime,100);

                }
                else
                {   btnplay.setImageResource(R.drawable.ic_action_pause);
                    mp.start();
                    Toast.makeText(getActivity(), "Play",Toast.LENGTH_SHORT).show();
                    finalTime = mp.getDuration();
                    startTime = mp.getCurrentPosition();
                    if(oneTimeOnly == 0)

                      {
                         seekbar.setMax((int) finalTime);
                         oneTimeOnly = 1;
                      } 
                        seekbar.setProgress((int)startTime);

                        handler.postDelayed(UpdateSongTime,100);
                }
            }
        });

        return v;

    }   
    @SuppressLint("NewApi")
    public void seekchange()
       {
           mp_progress=seekbar.getProgress();
           mp.seekTo(mp_progress);
       }

  private Runnable UpdateSongTime = new Runnable() 
    {
     public void run()
        {
         startTime = mp.getCurrentPosition();
         seekbar.setProgress((int)startTime);
         handler.postDelayed(this, 100);
        }
    };
}

You need to use Service to play song. It will allow you to play song even when app is in background Tutorial

Basically, from the MVC ( Model-View-Controller ) viewpoint, an Activity is a Controller , the view hierarchy is the View , and the View and Controller get re-created each time the orientation changes. Meanwhile, song playing belongs to the Model .

Note that an activity has the methods onSaveInstanceState(Bundle) and onCreate(Bundle) , and if the bundle passed to onCreate() is not null, it contains the information previously stored by onSaveInstanceState() . And there's also onRetainNonConfigurationInstance() . When the screen turns, the old activity is destroyed and a new activity is created, and these methods let the old activity pass important data to the new one.

Even after you create a service, you have to keep this MVC stuff in mind.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM