问题描述
该问题的原因是询问您如何播放m3u8格式的音频流。
是否可以在不使用myvideoview的情况下在后台播放m3u8?
不利之处在于,如果我使用myvideoview,则在手机屏幕关闭时无法在后台运行音频流。
此代码无效,并且直到更改为m3u8才起作用
public class ForegroundService extends Service {
public static final String CHANNEL_ID = "ForegroundServiceChannel";
MediaSession mMediaSession;
private MediaPlayer mediaPlayer;
FFmpegMediaPlayer mp;
//SimpleExoPlayerView exoPlayerView;
SimpleExoPlayer exoPlayer;
@Override
public void onCreate() {
super.onCreate();
}
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public int onStartCommand(Intent intent,int flags,int startId) {
String input = intent.getStringExtra("inputExtra");
createNotificationChannel();
Intent notificationIntent = new Intent(this,Principal.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,notificationIntent,0);
Notification notification = new NotificationCompat.Builder(this,CHANNEL_ID)
.setContentTitle("FM UNIVERSIDAD")
.setContentText(input)
.setSmallIcon(R.drawable.icono)
.setContentIntent(pendingIntent)
.build();
startForeground(1,notification);
mp = new FFmpegMediaPlayer();
mp.setonPreparedListener(new FFmpegMediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(FFmpegMediaPlayer mp) {
mp.start();
}
});
mp.setonErrorListener(new FFmpegMediaPlayer.OnErrorListener() {
@Override
public boolean onError(FFmpegMediaPlayer mp,int what,int extra) {
mp.release();
return false;
}
});
try {
mp.setDataSource("https://5f93e494d5906.streamlock.net:443/rtmputnfrp/utnfrp.stream/playlist.m3u8");
//mp.setDataSource("http://mediacontrol.jargon.com.ar:8168");
mp.prepareAsync();
} catch (IllegalArgumentException e) {
e.printstacktrace();
} catch (SecurityException e) {
e.printstacktrace();
} catch (IllegalStateException e) {
e.printstacktrace();
} catch (IOException e) {
e.printstacktrace();
}
return START_NOT_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
mp.release();
mp=null;
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel serviceChannel = new NotificationChannel(
CHANNEL_ID,"Foreground Service Channel",notificationmanager.IMPORTANCE_DEFAULT
);
notificationmanager manager = getSystemService(notificationmanager.class);
manager.createNotificationChannel(serviceChannel);
}
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void initMediaSession(MediaPlayer mediaPlayer) {
mMediaSession = new MediaSession(this,"MEDIA_SESSION_TAG");
mMediaSession.setCallback(new MediaSession.Callback() {
@Override
public boolean onMediaButtonEvent(Intent mediaButtonIntent) {
// Consume the media button event here. Should not send it to other apps.
return true;
}
});
mMediaSession.setFlags(MediaSession.FLAG_HANDLES_MEDIA_BUTTONS |
MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS);
if (!mMediaSession.isActive()) {
mMediaSession.setActive(true);
}
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)