在后台播放铃声

时间:2018-02-27 11:29:42

标签: android background-process ringtonemanager

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

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

但是当我转到其他活动时,它就会停止播放。

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

  
    

如果用户导航到其他活动,我想通过应用在后台播放铃声。

  

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

 <uses-permission android:name="anandroid.permission.INTERNET"/>

instead of this

<uses-permission android:name="android.permission.INTERNET" />

并将此代码放入您要开始服务的主Activity:

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