0

我有一个场景,我在一个活动中使用铃声播放器启动铃声

Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
r.play();

但是当我移动到另一个活动时,它会停止播放。

我的尝试:我使用 onResume 和 onPause 方法播放和暂停铃声播放,但这样做不是好主意。

如果用户导航到另一个活动,我想通过应用程序在后台播放铃声。

4

2 回答 2

0

使用服务或意向服务。服务就像没有 UI 部分的活动,并且生命周期略有不同。但是您可以与活动等 UI 组件进行很好的沟通。

于 2018-02-27T11:36:18.427 回答
0

这是音乐服务您可以使用此服务在后台播放音乐:

public class MusicService extends Service implements
    MediaPlayer.OnPreparedListener, MediaPlayer.OnErrorListener,
    MediaPlayer.OnCompletionListener {

//media player
private MediaPlayer player;
//song list
private ArrayList<Song> songs;
//current position
private int songPosn;
//binder
private final IBinder musicBind = new MusicBinder();
//title of current song
private String songTitle="";
//notification id
private static final int NOTIFY_ID=1;
//shuffle flag and random
private boolean shuffle=false;
private Random rand;

public void onCreate(){
    //create the service
    super.onCreate();
    //initialize position
    songPosn=0;
    //random
    rand=new Random();
    //create player
    player = new MediaPlayer();
    //initialize
    initMusicPlayer();
}

public void initMusicPlayer(){
    //set player properties
    player.setWakeMode(getApplicationContext(),
            PowerManager.PARTIAL_WAKE_LOCK);
    player.setAudioStreamType(AudioManager.STREAM_MUSIC);
    //set listeners
    player.setOnPreparedListener(this);
    player.setOnCompletionListener(this);
    player.setOnErrorListener(this);
}

//pass song list
public void setList(ArrayList<Song> theSongs){
    songs=theSongs;
}

//binder
public class MusicBinder extends Binder {
    MusicService getService() {
        return MusicService.this;
    }
}

//activity will bind to service
@Override
public IBinder onBind(Intent intent) {
    return musicBind;
}

//release resources when unbind
@Override
public boolean onUnbind(Intent intent){
    player.stop();
    player.release();
    return false;
}

//play a song
public void playSong(){
    //play
    player.reset();
    //get song
    Song playSong = songs.get(songPosn);
    //get title
    songTitle=playSong.getTitle();
    //get id
    long currSong = playSong.getID();
    //set uri
    Uri trackUri = ContentUris.withAppendedId(
            android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
            currSong);
    //set the data source
    try{
        player.setDataSource(getApplicationContext(), trackUri);
    }
    catch(Exception e){
        Log.e("MUSIC SERVICE", "Error setting data source", e);
    }
    player.prepareAsync();
}

//set the song
public void setSong(int songIndex){
    songPosn=songIndex;
}

@Override
public void onCompletion(MediaPlayer mp) {
    //check if playback has reached the end of a track
    if(player.getCurrentPosition()>0){
        mp.reset();
        playNext();
    }
}

@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
    Log.v("MUSIC PLAYER", "Playback Error");
    mp.reset();
    return false;
}

@Override
public void onPrepared(MediaPlayer mp) {
    //start playback
    mp.start();
    //notification
    Intent notIntent = new Intent(this, MainActivity.class);
    notIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendInt = PendingIntent.getActivity(this, 0,
            notIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    Notification.Builder builder = new Notification.Builder(this);

    builder.setContentIntent(pendInt)
            .setSmallIcon(R.drawable.ic_play)
            .setTicker(songTitle)
            .setOngoing(true)
            .setContentTitle("Playing")
            .setContentText(songTitle);
    Notification not = builder.build();
    startForeground(NOTIFY_ID, not);
}

//playback methods
public int getPosn(){
    return player.getCurrentPosition();
}

public int getDur(){
    return player.getDuration();
}

public boolean isPng(){
    return player.isPlaying();
}

public void pausePlayer(){
    player.pause();
}

public void seek(int posn){
    player.seekTo(posn);
}

public void go(){
    player.start();
}

//skip to previous track
public void playPrev(){
    songPosn--;
    if(songPosn<0) songPosn=songs.size()-1;
    playSong();
}

//skip to next
public void playNext(){
    if(shuffle){
        int newSong = songPosn;
        while(newSong==songPosn){
            newSong=rand.nextInt(songs.size());
        }
        songPosn=newSong;
    }
    else{
        songPosn++;
        if(songPosn>=songs.size()) songPosn=0;
    }
    playSong();
}

@Override
public void onDestroy() {
    stopForeground(true);
}

//toggle shuffle
public void setShuffle(){
    if(shuffle) shuffle=false;
    else shuffle=true;
}
}

并将此代码放入您要启动服务的主 Activity 中:

private ServiceConnection musicConnection = new ServiceConnection() {

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        MusicService.MusicBinder binder = (MusicService.MusicBinder) service;
        //get service
        musicSrv = binder.getService();
        //pass list
        musicSrv.setList(songList);
        musicBound = true;
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        musicBound = false;
    }
};

  //start and bind the service when the activity starts
  @Override
  protected void onStart() {
    super.onStart();
    if (playIntent == null) {
        playIntent = new Intent(this, MusicService.class);
        bindService(playIntent, musicConnection, Context.BIND_AUTO_CREATE);
        startService(playIntent);
    }
}
于 2018-04-12T04:59:28.443 回答