我创建了一个使用exoplayer来传输mp3音乐的应用程序。除了向通知栏和锁定屏幕添加音乐播放器小部件外,一切正常。我搜索了很多,发现一些建议使用媒体会话的博客文章。但我对这个概念不熟悉。有人可以帮助我将exoplayer与MediaSession连接,并使用相同的方法创建一个小部件。
即使不使用mediasession,也请建议如何使用exoplayer从app创建音乐小部件。链接到git代码或代码示例将非常有用。
这是我的playmusic活动代码,我从Homeactivity调用此方法。
public class PlayMusic {
private static SimpleExoPlayer mp;
private static Handler mainHandler;
private static RenderersFactory renderersFactory;
private static BandwidthMeter bandwidthMeter;
private static LoadControl loadControl;
private static DataSource.Factory dataSourceFactory;
private static ExtractorsFactory extractorsFactory;
private static MediaSource mediaSource;
private static TrackSelection.Factory trackSelectionFactory;
private static TrackSelector trackSelector;
private AudioManager audioManager;
private static final int BUFFER_SEGMENT_SIZE = 128 * 1024;
private static final int BUFFER_SEGMENT_COUNT = 1024;
public static final String channel_url = "http://ibadat.out.airtime.pro:8000/ibadat_a";
public static void preparePlayingSound(Context con, String url) {
renderersFactory = new DefaultRenderersFactory(con);
bandwidthMeter = new DefaultBandwidthMeter();
trackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter);
trackSelector = new DefaultTrackSelector(trackSelectionFactory);
loadControl = new DefaultLoadControl();
mp = ExoPlayerFactory.newSimpleInstance(renderersFactory, trackSelector, loadControl);
dataSourceFactory = new DefaultDataSourceFactory(con, "ExoplayerDemo");
extractorsFactory = new DefaultExtractorsFactory();
mainHandler = new Handler();
mediaSource = new ExtractorMediaSource(Uri.parse(url),
dataSourceFactory,
extractorsFactory,
mainHandler,
null);
mp.prepare(mediaSource);
}
public static void startPlayingSound(Context con) {
if (mp != null && mp.getPlayWhenReady())
{
} else {
mp.setPlayWhenReady(true);
mp.setVolume(1f);
}
}
//and to stop it use this method below
public static boolean checkStatus(Context con) {
Boolean flag = false;
if (mp != null && mp.getPlayWhenReady()) {
flag = true;
} else {
flag = false;
}
return flag;
}
public static void stopPlayingSound(Context con) {
if (mp != null && mp.getPlayWhenReady()) {
mp.stop();
mp.release();
mp = null;
} else {
}
}
public static void setVolume(Context context,Float volume)
{
mp.setVolume(volume);
}
}
答案 0 :(得分:0)
根据Android IO指南使用MediaSession
是在各种设备上流式传输音乐的最佳方式之一。他们提供了一个示例项目,其实现与您在下面要求使用Exo Player的情况有关link。对于通知和LockScreen播放器,请参阅上述参考中的MediaNotificationManger
类。