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来控制播放状态。

相关内容

热门资讯

安装apache-beam==... 出现此错误可能是因为用户的Python版本太低,而apache-beam==2.34.0需要更高的P...
避免在粘贴双引号时向VS 20... 在粘贴双引号时向VS 2022添加反斜杠的问题通常是由于编辑器的自动转义功能引起的。为了避免这个问题...
Android Recycle... 要在Android RecyclerView中实现滑动卡片效果,可以按照以下步骤进行操作:首先,在项...
omi系统和安卓系统哪个好,揭... OMI系统和安卓系统哪个好?这个问题就像是在问“苹果和橘子哪个更甜”,每个人都有自己的答案。今天,我...
原生ios和安卓系统,原生对比... 亲爱的读者们,你是否曾好奇过,为什么你的iPhone和安卓手机在操作体验上有着天壤之别?今天,就让我...
Android - 无法确定任... 这个错误通常发生在Android项目中,表示编译Debug版本的Java代码时出现了依赖关系问题。下...
Android - NDK 预... 在Android NDK的构建过程中,LOCAL_SRC_FILES只能包含一个项目。如果需要在ND...
Akka生成Actor问题 在Akka框架中,可以使用ActorSystem对象生成Actor。但是,当我们在Actor类中尝试...
Agora-RTC-React... 出现这个错误原因是因为在 React 组件中使用,import AgoraRTC from “ago...
安装了Anaconda之后找不... 在安装Anaconda后,如果找不到Jupyter Notebook,可以尝试以下解决方法:检查环境...