在后台播放时音乐停止。我不想将其用作服务。我有什么选择?



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

这是我使用的代码

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

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

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

startForeground(R.string.app_name, aNotification);

最新更新