如何在android中改变方向时连续播放音频

时间:2014-02-07 08:02:20

标签: java android android-mediaplayer android-orientation

我开发了类似媒体播放器的应用程序,用于从源播放音频文件,但是当我将方向画像更改为横向按钮和其他不工作的小部件以及当我再次将方向风景更改为肖像时发出问题按钮和搜索栏无效

我的代码在这里

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);
        }
    };
}

2 个答案:

答案 0 :(得分:4)

您需要使用Service来播放歌曲。即使应用程序处于后台,它也可以让您播放歌曲 Tutorial

答案 1 :(得分:0)

基本上,从 MVC 模型 - 视图 - 控制器)的角度来看,Activity是 Controller ,视图层次结构是< em> View ,每次方向更改时都会重新创建 View Controller 。同时,歌曲播放属于 Model

请注意,活动的方法为onSaveInstanceState(Bundle)onCreate(Bundle),如果传递给 onCreate()的包不为空,则它包含以前存储的信息< EM>的onSaveInstanceState()的。 而且还有onRetainNonConfigurationInstance()。当屏幕转动时,旧活动被销毁并创建一个新活动,这些方法让旧活动将重要数据传递给新活动。

即使您创建了服务,也必须牢记这个 MVC