使用蓝牙时,Android音乐会在睡眠模式下跳过

时间:2012-11-21 02:14:31

标签: android audio bluetooth skip standby

几天前我编写了一个简单的MP3播放器。代码位于Playing music in sleep/standby mode in Android 2.3.3

当MP3播放器(Samsung 3.6,Android 2.2)自行播放音乐时,一切正常。我买了一个蓝牙音箱(A2DP 3.0)并将其连接到MP3播放器。音乐仍然可以播放,但是当MP3播放器屏幕进入睡眠模式时,音乐开始跳过蓝牙音箱。

  • 如果mp3在睡眠模式下独立播放音乐,则不会发生这种情况
  • 即使在蓝牙音箱的睡眠模式下,mp3播放器中的其他音乐播放器应用似乎也能正常播放
  • 如果我禁用蓝牙,并使用通过电缆连接的扬声器播放音乐,则即使在待机模式下音乐也能正常播放。

这让我相信我的代码可能有问题。

这是我的完整代码:

public class MainActivity extends Activity {

private static String audioPath;
private static ArrayList<String> devotionalList;
private static ArrayList<String> christmasList;
private static ArrayList<String> cinemaList;
private static ImageButton playPauseButton;
private static TextView appMsg;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    int maxVolume = audioManager
            .getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    int curVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
    SeekBar volControl = (SeekBar) findViewById(R.id.volumeBar);
    volControl.setMax(maxVolume);
    volControl.setProgress(curVolume);
    volControl
            .setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                @Override
                public void onStopTrackingTouch(SeekBar arg0) {
                }

                @Override
                public void onStartTrackingTouch(SeekBar arg0) {
                }

                @Override
                public void onProgressChanged(SeekBar arg0, int arg1,
                        boolean arg2) {
                    audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
                            arg1, 0);
                }
            });

    File baseDir = Environment.getExternalStorageDirectory();
    audioPath = baseDir.getAbsolutePath() + "/external_sd/music/";

    devotionalList = loadSongs(audioPath + "devotional/");
    christmasList = loadSongs(audioPath + "christmas/");
    cinemaList = loadSongs(audioPath + "cinema/");
}

private ArrayList<String> loadSongs(String songDirectory) {
    File directory = new File(songDirectory);
    ArrayList<String> al1 = new ArrayList<String>();
    for (String filename : directory.list()) {
        al1.add(songDirectory + filename);
    }
    return al1;
}

public void playPauseSong(View v) {
    if (MP3PlayerService.isPlaying) {
        playPauseButton.setImageResource(android.R.drawable.ic_media_play);
        // Intent i1 = new Intent(this, MP3PlayerService.class);
        stopService(new Intent(this, MP3PlayerService.class));
    } else {
        if (MP3PlayerService.playerStarted) {
            playPauseButton
                    .setImageResource(android.R.drawable.ic_media_pause);
            startService(new Intent(this, MP3PlayerService.class));
        }
    }
}

public void playNextSong(View v) {
    if (MP3PlayerService.playerStarted) {
        startService(new Intent(this, MP3PlayerService.class));
    }
}

public void playPreviousSong(View v) {
    if (MP3PlayerService.playerStarted) {
        MP3PlayerService.previousSong = true;
        startService(new Intent(this, MP3PlayerService.class));
    }
}

@Override
protected void onPause() {
    super.onPause();
    playPauseButton = null;
    appMsg = null;
}

@Override
protected void onResume() {
    super.onResume();
    appMsg = (TextView) this.findViewById(R.id.txtAppMsg);
    appMsg.setText("");
    playPauseButton = (ImageButton) this.findViewById(R.id.btnPlayPause);
}

public void playDevotional(View v) {
    playPlayList(devotionalList);
}

public void playChristmas(View v) {
    playPlayList(christmasList);
}

public void playCinema(View v) {
    playPlayList(cinemaList);
}

private void playPlayList(ArrayList<String> playList) {
    if (MP3PlayerService.isPlaying) {
        MP3PlayerService.mediaPlayer.stop();
        MP3PlayerService.mediaPlayer.reset();
        MP3PlayerService.mediaPlayer.release();
    }
    MP3PlayerService.songPosition = 0;
    playPauseButton.setImageResource(android.R.drawable.ic_media_pause);
    Intent i1 = new Intent(this, MP3PlayerService.class);
    i1.putExtra("playListChoice", playList);
    startService(i1);
}

}

这是我的媒体播放器服务代码:

public class MP3PlayerService extends Service implements OnCompletionListener {
private static ArrayList<String> al1;
private static FileInputStream fis;
private static FileDescriptor fd;
static boolean previousSong;
static boolean playerStarted = false;
static int songPosition;
static boolean isPlaying = false;
static String currentSong;
static ArrayList<String> songHistory;
static MediaPlayer mediaPlayer;

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    songHistory = new ArrayList<String>();
}

@Override
public void onDestroy() {
    songPosition = mediaPlayer.getCurrentPosition();
    mediaPlayer.pause();
    isPlaying = false;
}

@Override
public void onStart(Intent intent, int startid) {

    if (songPosition == 0) {
        Bundle extras = intent.getExtras();
        if (extras == null) {
            if (previousSong) {
                playSong();
            } else {
                // Next Song Button clicked
                playNextSong();
            }
        } else {
            // A Playlist button is clicked
            al1 = (ArrayList<String>) extras.get("playListChoice");
            playSong();
        }
    } else {
        songPosition = 0;
        mediaPlayer.start();
    }
    isPlaying = true;
}

private void playNextSong() {
    if (isPlaying) {
        mediaPlayer.release();
        playSong();
    }
}

private void playSong() {
    if (previousSong) {
        mediaPlayer.release();
        if (songHistory.size() > 1) {
            currentSong = songHistory.get(songHistory.size() - 2);
            songHistory.remove(songHistory.size() - 1);
        } else {
            currentSong = songHistory.get(0);
        }
        previousSong = false;
    } else {
        currentSong = al1.get(new Random().nextInt(al1.size()));
        songHistory.add(currentSong);
    }
    try {
        fis = new FileInputStream(currentSong);
        fd = fis.getFD();
        mediaPlayer = new MediaPlayer();
        mediaPlayer.setDataSource(fd);
        mediaPlayer.prepare();
        mediaPlayer.seekTo(songPosition);
        mediaPlayer.setOnCompletionListener(this);
        mediaPlayer.start();
        playerStarted = true;
    } catch (FileNotFoundException e) {
        Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
        e.printStackTrace();
    } catch (IOException e) {
        Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
        e.printStackTrace();
    }
}

@Override
public void onCompletion(MediaPlayer arg0) {
    playNextSong();
}
}

1 个答案:

答案 0 :(得分:0)

我想我修好了。滑块卷代码是问题的根源。没有那件,应用程序仍然在使用蓝牙扬声器的待机模式下工作。我想要滑块,所以我尝试在为每首歌曲实例化媒体播放器后获取部分唤醒锁定,并使应用程序在待机模式下正常运行(滑块音量控制也是应用程序的一部分)。我会在每首歌后发布锁。