Android Exo Player在用户离开后继续播放
创始人
2024-08-13 17:01:44
0

要实现Android Exo Player在用户离开后继续播放的功能,可以使用Service来管理Exo Player的播放状态。以下是一个示例代码,演示如何在用户离开应用后,通过Service继续播放音频。

  1. 创建一个继承自Service的类,比如AudioPlayerService
public class AudioPlayerService extends Service {
    private SimpleExoPlayer player;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        player = new SimpleExoPlayer.Builder(getApplicationContext()).build();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (intent != null && intent.getAction() != null) {
            if (intent.getAction().equals(Constants.ACTION_PLAY)) {
                String audioUrl = intent.getStringExtra(Constants.EXTRA_AUDIO_URL);
                playAudio(audioUrl);
            } else if (intent.getAction().equals(Constants.ACTION_PAUSE)) {
                pauseAudio();
            } else if (intent.getAction().equals(Constants.ACTION_RESUME)) {
                resumeAudio();
            } else if (intent.getAction().equals(Constants.ACTION_STOP)) {
                stopAudio();
            }
        }
        return START_STICKY;
    }

    private void playAudio(String audioUrl) {
        Uri uri = Uri.parse(audioUrl);
        MediaItem mediaItem = MediaItem.fromUri(uri);
        player.setMediaItem(mediaItem);
        player.prepare();
        player.play();
    }

    private void pauseAudio() {
        player.pause();
    }

    private void resumeAudio() {
        player.play();
    }

    private void stopAudio() {
        player.stop();
        stopSelf(); // 停止Service
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        player.release();
    }
}
  1. 在你的Activity中启动和控制Service。
public class MainActivity extends AppCompatActivity {
    private Intent serviceIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        serviceIntent = new Intent(this, AudioPlayerService.class);
        serviceIntent.setAction(Constants.ACTION_PLAY);
        serviceIntent.putExtra(Constants.EXTRA_AUDIO_URL, "音频URL");

        // 启动Service
        startService(serviceIntent);

        // 控制播放
        Button playButton = findViewById(R.id.play_button);
        Button pauseButton = findViewById(R.id.pause_button);
        Button resumeButton = findViewById(R.id.resume_button);
        Button stopButton = findViewById(R.id.stop_button);

        playButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                serviceIntent.setAction(Constants.ACTION_PLAY);
                startService(serviceIntent);
            }
        });

        pauseButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                serviceIntent.setAction(Constants.ACTION_PAUSE);
                startService(serviceIntent);
            }
        });

        resumeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                serviceIntent.setAction(Constants.ACTION_RESUME);
                startService(serviceIntent);
            }
        });

        stopButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                serviceIntent.setAction(Constants.ACTION_STOP);
                startService(serviceIntent);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 停止Service
        stopService(serviceIntent);
    }
}
  1. 创建一个Constants类,用于定义Action和Extra常量。
public class Constants {
    public static final String ACTION_PLAY = "com.example.app.ACTION_PLAY";
    public static final String ACTION_PAUSE = "com.example.app.ACTION_PAUSE";
    public static final String ACTION_RESUME = "com.example.app.ACTION_RESUME";
    public static final String ACTION_STOP = "com.example.app.ACTION_STOP";
    public static final String EXTRA_AUDIO_URL = "com.example.app.EXTRA_AUDIO_URL";
}

这样,当用户离开应用时,Service仍然在后台运行,Exo Player会继续播放音频。用户可以通过发送不同的Action来控制播放状态。

相关内容

热门资讯

安卓换鸿蒙系统会卡吗,体验流畅... 最近手机圈可是热闹非凡呢!不少安卓用户都在议论纷纷,说鸿蒙系统要来啦!那么,安卓手机换上鸿蒙系统后,...
安卓系统拦截短信在哪,安卓系统... 你是不是也遇到了这种情况:手机里突然冒出了很多垃圾短信,烦不胜烦?别急,今天就来教你怎么在安卓系统里...
app安卓系统登录不了,解锁登... 最近是不是你也遇到了这样的烦恼:手机里那个心爱的APP,突然就登录不上了?别急,让我来帮你一步步排查...
安卓系统要维护多久,安卓系统维... 你有没有想过,你的安卓手机里那个陪伴你度过了无数日夜的安卓系统,它究竟要陪伴你多久呢?这个问题,估计...
windows官网系统多少钱 Windows官网系统价格一览:了解正版Windows的购买成本Windows 11官方价格解析微软...
安卓系统如何卸载app,轻松掌... 手机里的App越来越多,是不是感觉内存不够用了?别急,今天就来教你怎么轻松卸载安卓系统里的App,让...
怎么复制照片安卓系统,操作步骤... 亲爱的手机控们,是不是有时候想把自己的手机照片分享给朋友,或者备份到电脑上呢?别急,今天就来教你怎么...
安卓系统应用怎么重装,安卓应用... 手机里的安卓应用突然罢工了,是不是让你头疼不已?别急,今天就来手把手教你如何重装安卓系统应用,让你的...
iwatch怎么连接安卓系统,... 你有没有想过,那款时尚又实用的iWatch,竟然只能和iPhone好上好?别急,今天就来给你揭秘,怎...
iphone系统与安卓系统更新... 最近是不是你也遇到了这样的烦恼?手机更新系统总是失败,急得你团团转。别急,今天就来给你揭秘为什么iP...