简体   繁体   English

保持声音在App内播放,但在App进入后台时停止播放

[英]Keep sound playing within the App but stop it when App goes to the background

So I have a Sound I want to play continuously throughout the whole of my App. 所以我有一个声音,我想在我的整个App中连续播放。 But as soon as the App goes to background, the sound should stop. 但是,一旦应用进入后台,声音就会停止。 I´m trying to do this during onStop(). 我正在尝试在onStop()期间执行此操作。 The problem is that onStop() is called every time the activity is stopped, regardless of whether I stay within the app or not. 问题是,无论我是否停留在应用程序中,每次活动停止时都会调用onStop()。 Is there a way to differentiate between those cases? 有没有办法区分这些情况? Something like this: 像这样:

override fun onStop() {
    if [App is left but not destroyed] {
        super.onStop()
        mediaLength = mediaPlayer.currentPosition
        mediaPlayer.pause()
    }
    else { //i.e. activity is left but the new activity is from the same app.
        super.onStop()
    }
}

EDIT: 编辑:

I tried to implement this answer , but had to translate it to kotlin. 我尝试实现此答案 ,但不得不将其翻译为kotlin。 I think I did everything right, but I get an unresolved reference: mediaPlayer in the Lifecycleobserver. 我认为我所做的一切都正确,但是我得到了一个unresolved reference: mediaPlayer Lifecycleobserver中的unresolved reference: mediaPlayer

Here´s the Observer: 这是观察者:

package com.example.asdasd.soulfetch

import android.app.Application
import android.arch.lifecycle.Lifecycle
import android.arch.lifecycle.LifecycleObserver
import android.arch.lifecycle.OnLifecycleEvent
import android.arch.lifecycle.ProcessLifecycleOwner


class CheckLifeCycle: Application(), LifecycleObserver {
override fun onCreate() {
    super.onCreate()
    ProcessLifecycleOwner.get().getLifecycle().addObserver(this)
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
fun onAppBackground() {
    MainActivity.mediaPlayer.pause()
}
}

And here are the relevant parts of MainActivity 这是MainActivity的相关部分

class MainActivity : AppCompatActivity() {

lateinit var mediaPlayer: MediaPlayer

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    mediaPlayer = MediaPlayer()

    fun playBordun() {
        mediaPlayer = MediaPlayer.create(this, R.raw.cbordun)
        mediaPlayer.isLooping = true
        mediaPlayer.start()
        }
    playBordun()
    }
override fun onDestroy() {
    super.onDestroy()
    mediaPlayer.stop()
    mediaPlayer.reset()
    mediaPlayer.release()
    }

Any idea what I am missing here? 知道我在这里缺少什么吗?

You can create a custom Application class (extends Application) and implement it with LifecycleObserver interface, and create a function within the CustomApplication class and annotate it with @OnLifecycleEvent(Lifecycle.Event.ON_STOP) so when the app is in background the function will be triggered and inside it you will put the logic to stop the mediaPlayer (make it global to access it from different place whiting the app) 您可以创建一个自定义Application类(扩展Application)并使用LifecycleObserver接口实现它,并在CustomApplication类中创建一个函数,并使用@OnLifecycleEvent(Lifecycle.Event.ON_STOP)对其进行注释,因此当该应用程序在后台运行时,该函数将触发并在其内部放置逻辑,以停止mediaPlayer(使其全局,以便从其他位置访问该应用)

In order to use the LifecycleObserver you need to add LifeCycle to your app (it's an Android architecture component) here is the link: https://developer.android.com/topic/libraries/architecture/adding-components 为了使用LifecycleObserver,您需要将LifeCycle添加到您的应用程序(这是一个Android体系结构组件),这里是链接: https : //developer.android.com/topic/libraries/architecture/adding-components

You can't gain such functionality using activity. 您无法使用活动获得此类功能。 Activity is highly prone to be destroyed, recreated. 活动极易被破坏,重新创建。 You should use Service to attain such functionality, as the service will persist, whether your activity is destroyed or not. 无论您的活动是否被破坏,您都应使用Service来获得此类功能,因为该服务将持续存在。

This article is old and out of date, but can still learn using service with music from it 本文已过时且过时,但仍可以通过音乐学习如何使用服务

You can use the LifecycleObserver to check whether the application in background or not. 您可以使用LifecycleObserver来检查应用程序是否在后台。

public class CheckLifeCycle extends Application implements LifecycleObserver {

@Override
public void onCreate() 
{
    super.onCreate();
    ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
}

@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
public void onAppBackground()
{
    MainActivity.mediaPlayer.pause();
}

}

and add the class name CheckLifeCycle in a manifest 并在清单中添加类名称CheckLifeCycle

<manifest >
    ....
    <application
        android:name=".CheckLifeCycle"
        ......
    </application>

</manifest>

don't forget to include the basic Support library in gradle. 不要忘记在gradle中包含基本的支持库。

implementation "android.arch.lifecycle:extensions:1.1.1"
annotationProcessor "android.arch.lifecycle:compiler:1.1.1"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 应用程序在后台运行时停止MediaPlayer服务 - Stop MediaPlayer service when app goes in Background 当应用程序进入后台时如何停止runnable? - How to stop runnable when the app goes to background? 当应用程序进入后台时,回收站视图中的 Exoplayer 会继续播放 - Exoplayer in recycler view keeps playing when app goes in background Android Kotlin - 当应用程序进入后台时 ExoPlayer 继续播放 - Android Kotlin - ExoPlayer keeps playing when app goes to background 当应用程序进入后台时,MediaBrowserServiceCompat 停止播放音乐 - MediaBrowserServiceCompat stopped playing music when app goes background 当应用在后台时,Firebase推送通知不会播放声音 - Firebase Push notification is not playing sound when app is in background Android 仅当应用程序处于前台时才播放推送通知声音,但当应用程序处于后台时不播放声音 - Android Push notification sound is played only when app is in foreground but not playing sound when app is in background 推送通知声音未播放应用程序在后台 - Push notification sound is not playing app is in background 当应用程序在 Tinder/Scarlet Websocket 中进入后台时保持 websocket 打开 - keep websocket open when app goes background in Tinder/Scarlet Websocket 当应用程序被破坏或进入后台时,Android 位置服务停止工作 - Android location service stop working when app destroyed or goes in background
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM