0

我正在开发一个音乐播放器应用程序,它可以很好地播放音乐,但是当它在后台播放时,问题就来了,一段时间后它停止了....我在网上搜索过如何解决这个问题,几乎他们所有人都在使用服务而不是活动,我不想那样做,那么我该如何实现呢?

这是我正在使用的代码

public class NowPlaying extends Activity implements Serializable  {
    static MediaPlayer mp =new MediaPlayer();



    /*SeekBar songProgressBar = (SeekBar) findViewById(R.id.songProgressBar);
    TextView songCurrentDurationLabel = (TextView) findViewById(R.id.songCurrentDurationLabel);
    TextView songTotalDurationLabel = (TextView) findViewById(R.id.songTotalDurationLabel);
    private Handler mHandler = new Handler();
    private Utilities utils;
    */

    @SuppressLint("NewApi")
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.now_playing);
        Intent i = getIntent();
        final int position=i.getIntExtra("Data2", 0);


        final ArrayList<SongDetails> songs = getIntent().getParcelableArrayListExtra("Data1"); 
        SongDetails songDetails = songs.get(position) ;



        Button bfake=(Button) findViewById(R.id.bFake);
        LinearLayout LL=(LinearLayout) findViewById(R.id.LL);
        Button Pause=(Button)findViewById(R.id.bPlayPause);



















        Playservice(songs,position,0  );

         bfake.setOnTouchListener(new OnSwipeTouchListener()
         { int z=0;

                public void onSwipeRight() {
                    z=z-1;
                    Playservice(songs,position,z  );
                }
                public void onSwipeLeft() {
                 z=z+1;     
                    Playservice(songs,position,z  );
                }
                public void onSwipeBottom() {
                    mp.stop();
                }
               });

            LL.setOnTouchListener(new OnSwipeTouchListener() {
               public void onSwipeBottom() {
                    mp.stop();
                }
               });  

            Pause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) 
            {if(mp.isPlaying())
                mp.pause();
            else
                mp.start();
            }
            });
    }
         private void Playservice( ArrayList<SongDetails> songs, int position, int i    )

         {


            /* Utilities utils = new Utilities();
             songProgressBar.setOnSeekBarChangeListener((OnSeekBarChangeListener) this); // Important
             *///mp.setOnCompletionListener((OnCompletionListener) this);
            try {
              String ab=songs.get(position+i).getPath2().toString();
                    if(mp.isPlaying())
                    {   mp.stop();
                        }
                    mp.reset();
                    mp.setDataSource(ab) ;
                    mp.prepare();
                    mp.start();
                } catch (IllegalArgumentException e) {

                    e.printStackTrace();
                } catch (SecurityException e) {

                    e.printStackTrace();
                } catch (IllegalStateException e) {

                    e.printStackTrace();
                } catch (IOException e) {

                    e.printStackTrace();
                }
4

2 回答 2

2

您必须为此使用前台服务。后台 Activity 或后台 Service 被 Android 杀死的概率非常高。这是你观察到的问题。

于 2013-08-23T09:33:13.600 回答
1

我遇到了同样的问题,我所做的是在服务 OnCreate 方法中将服务设置在前台

startForeground(R.string.app_name, aNotification);
于 2013-08-23T09:42:25.510 回答