Playing music in sleep/standby mode in Android 2.3.3

邮差的信 提交于 2019-11-29 05:21:02

I think you should run the media in background using services So you create a service and put your media code in it and attach it to start and stop buttons maybe somthing like this :

public class MediaPlayerService extends Service {
     MediaPlayer myMediaPlayer;

     @Override
     public IBinder onBind(Intent intent) {
            // TODO Auto-generated method stub
            return null;
     }
     @Override
     public void onCreate() {
         FileInputStream fis = new FileInputStream(songList.get(0));
         FileDescriptor fd = fis.getFD();

         if (mediaPlayer != null) {
           if (mediaPlayer.isPlaying()) {
               mediaPlayer.release();
               mediaPlayer = null;
              }
            }

         mediaPlayer = new MediaPlayer();
         mediaPlayer.setDataSource(fd);
         mediaPlayer.prepare();
         playPauseButton.setImageResource(android.R.drawable.ic_media_pause);
         mediaPlayer.seekTo(songPosition);
         appMsg.setText(songList.get(0));
     }
     @Override
     public void onStart(Intent intent, int startid) {
         myMediaPlayer.start();
     }
     @Override
     public void onDestroy() {
         myMediaPlayer.stop();
     }
}

After that you start that service when a start button is pressed using methodestartService and stop it using methode stopService in your Activity class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!