如何在Android应用程序中使用直播流URL启动服务背景音乐
问题描述:
我开发了android应用程序RADIO流。如何在Android应用程序中使用直播流URL启动服务背景音乐
我的应用程序上的音乐工作正常发挥,所以我想添加电台现场直播的网址,如:http://162.244.80.118:4900/;stream.mp3
所以任何人都可以帮助我如何可以添加URL和播放背景音乐service
现场广播应用。
这里开始service
代码,我使用的音乐播放Android上的背景音乐
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.provider.Settings;
import android.support.annotation.Nullable;
/**
* Created by User on 9/22/2017.
*/
public class MyService extends Service {
private MediaPlayer player;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
player = MediaPlayer.create(this, Settings.System.DEFAULT_RINGTONE_URI);
player.setLooping(true);
player.start();
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
player.stop();
}
}
答
您可以使用ExoPlayer这是建立在Android的低水平媒体API之上的应用程序级的媒体播放器,你可以创建它从服务流。请参阅此链接的实现部分。 https://github.com/ayalus/ExoPlayer-2-Example/blob/master/ExoPlayer2Example/app/src/main/java/com/ayalus/exoplayer2example/MainActivity.java