【问题标题】:how to play audio from WebView even when I exit the app?即使退出应用程序,如何从 WebView 播放音频?
【发布时间】:2022-06-10 17:45:53
【问题描述】:

我正在 Android Studio 中开发一个 Android 应用程序,并且必须在 WebView 上播放声音。但是当我关闭应用程序时,它会自动停止播放音频。现在我想知道如何让音频在后台播放,即使我关闭了应用程序。 (请提供代码,我是 android 开发新手。)提前致谢。

【问题讨论】:

    标签: android android-studio webview android-webview


    【解决方案1】:

    对不起,我认为这是不可能的。 要在后台播放声音,您需要在 Service 中播放音频。 这只是播放背景声音的一种方式。

    我将为此显示一个代码:

    public class serv extends Service{
    
        MediaPlayer mp;
        @Override
        public IBinder onBind(Intent intent) {
            // TODO Auto-generated method stub
            return null;
        }
        public void onCreate()
        {   
            mp = MediaPlayer.create(this, R.raw.b);
            mp.setLooping(false);
        }
        public void onDestroy()
        {       
            mp.stop();
        }
        public void onStart(Intent intent,int startid){
    
            Log.d(tag, "On start");
            mp.start();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多