代码之家  ›  专栏  ›  技术社区  ›  Michel

在Android上后台播放音频超过一分钟

  •  0
  • Michel  · 技术社区  · 1 年前

    我正在开发一款处理音频数据的安卓应用程序,现在我看到了当应用程序在后台使用服务时,如何让音频继续播放。

    这是 主活动.kt

    package me.soft.myapp
    
    import android.content.Intent
    import android.graphics.Color
    import androidx.appcompat.app.AppCompatActivity
    import android.os.Bundle
    import android.view.View
    
    class MainActivity : AppCompatActivity() {
        private var toggleFlag = false
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
        }
    
    
        fun startHandler(view: View) {
            val serviceIntent = Intent(this, TheService::class.java)
            startService(serviceIntent)
        }
    
    
        fun stopHandler(view: View) {
            val serviceIntent = Intent(this, TheService::class.java)
            stopService(serviceIntent)
        }
    
    
        fun toggleHandler(view: View) {
            toggleFlag = !toggleFlag
    
            when(toggleFlag) {
                true -> view.setBackgroundColor(Color.rgb(0xFF,0x00,0x00))
                false -> view.setBackgroundColor(Color.rgb(0x00,0x00,0xFF))
            }
        }
    }
    

    文件:

    package me.soft.myapp
    
    import android.R
    import android.app.Notification
    import android.app.Service
    import android.content.Intent
    import android.media.MediaPlayer
    import android.os.IBinder
    import android.provider.Settings
    import android.widget.Toast
    import androidx.core.app.NotificationCompat
    
    
    class TheService: Service() {
        private lateinit var audioPlayer:MediaPlayer
    
        override fun onCreate() {
            super.onCreate()
        }
    
    
        override fun onBind(p0: Intent?): IBinder? {
            return null
        }
    
    
        override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
            val runnable = Runnable {
                audioPlayer = MediaPlayer.create(this, Settings.System.DEFAULT_RINGTONE_URI)
                audioPlayer?.setLooping(true)
                audioPlayer.start()
            }
    
            val thread = Thread(runnable)
            thread.start() // Stops after one minute when in the background.
            return START_NOT_STICKY
        }
    
    
        override fun onDestroy() {
            super.onDestroy()
            audioPlayer.stop()
        }
    }
    

    acticity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/   res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            tools:layout_editor_absoluteX="1dp"
            tools:layout_editor_absoluteY="1dp">
    
            <Button
                android:id="@+id/strtBtn"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Start"
                android:onClick="startHandler"
                android:textAllCaps="false" />
    
            <Button
                android:id="@+id/stpBtn"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Stop"
                android:onClick="stopHandler"
                android:textAllCaps="false" />
    
            <Button
                android:id="@+id/bgctgBtn"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="BCG Toggle"
                android:onClick="toggleHandler"
                android:textAllCaps="false" />
        </LinearLayout>
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    AndroidManifest.xml 文件:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        package="me.soft.myapp">
    
        <application
            android:allowBackup="true"
            android:dataExtractionRules="@xml/data_extraction_rules"
            android:fullBackupContent="@xml/backup_rules"
            android:icon="@mipmap/ic_launcher"
            android:label="myapp"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/Theme.ServiceTry"
            tools:targetApi="31">
            <activity
                android:name=".MainActivity"
                android:exported="true">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <service android:name=".TheService" />
        </application>
    
    </manifest>
    
    1 回复  |  直到 1 年前
        1
  •  1
  •   Gabe Sechan    1 年前

    您需要使用前台服务。如果前景应用程序未使用后台服务,则后台服务将在2分钟后终止。除非手机资源不足,否则前台服务可以持续很长时间。为此,请使用startForegroundService启动该服务,并让该服务的onStartCommand调用startForeground。