2

几天前我编写了一个简单的 mp3 播放器。该代码位于在 Android 2.3.3 中以睡眠/待机模式播放音乐

当 mp3 播放器(三星 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();
}
}
4

1 回答 1

0

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

于 2012-11-25T22:06:12.527 回答