微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

m3u8 Android Studio音频流

如何解决m3u8 Android Studio音频流

该问题的原因是询问您如何播放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 举报,一经查实,本站将立刻删除。