【问题标题】:How to play streamed audio in background on Android?如何在 Android 上在后台播放流式音频?
【发布时间】:2012-10-10 15:02:43
【问题描述】:

我有一个 Android 应用程序,它使用 MediaPlayer 类播放来自 Internet 的流式音频。

如何让它在后台继续播放音频 用户点击主页按钮来运行其他应用程序?

在运行其他应用时,我希望它继续播放音频。

【问题讨论】:

    标签: android media-player


    【解决方案1】:

    你必须使用一个叫做 Android 服务的东西。

    来自文档:

    “服务是一个应用程序组件,代表应用程序希望在不与用户交互的情况下执行更长时间运行的操作,或提供功能供其他应用程序使用。”

    以下是使用服务帮助您入门的优秀官方指南: http://developer.android.com/guide/components/services.html

    这是一个关于构建音频播放器的好教程: http://www.androidhive.info/2012/03/android-building-audio-player-tutorial/

    以下是构建流媒体音乐播放器的视频教程: http://www.youtube.com/watch?v=LKL-efbiIAM

    【讨论】:

    • 谢谢,我去看看服务!
    【解决方案2】:

    您需要实现一个服务才能在后台播放媒体,而不会将其绑定到开始播放的 Activity。看看this example

    【讨论】:

    • 感谢有关服务的提示和链接!
    【解决方案3】:

    关键是定义Service.START_STICKY继续在后台播放:

       public int onStartCommand(Intent intent, int flags, int startId) {
            myMediaPlayer.start();
            return Service.START_STICKY;
        }
    

    Service.START_STICKY : 如果这个服务的进程在它被杀死的时候被杀死 启动系统会尝试重新创建服务。

    这是一个这样做的例子:

    import android.app.Service;
    import android.content.Intent;
    import android.media.MediaPlayer;
    import android.os.IBinder;
    import android.util.Log;
    import android.widget.Toast;
    
    /**
     * Created by jorgesys.
     */
    
    /* Add declaration of this service into the AndroidManifest.xml inside application tag*/
    
    public class BackgroundSoundService extends Service {
    
        private static final String TAG = "BackgroundSoundService";
        MediaPlayer player;
    
        public IBinder onBind(Intent arg0) {
            Log.i(TAG, "onBind()" );
            return null;
        }
    
        @Override
        public void onCreate() {
            super.onCreate();
            player = MediaPlayer.create(this, R.raw.jorgesys_song);
            player.setLooping(true); // Set looping
            player.setVolume(100,100);
            Toast.makeText(this, "Service started...", Toast.LENGTH_SHORT).show();
            Log.i(TAG, "onCreate() , service started...");
    
        }
        public int onStartCommand(Intent intent, int flags, int startId) {
            player.start();
            return Service.START_STICKY;
        }
    
        public IBinder onUnBind(Intent arg0) {
            Log.i(TAG, "onUnBind()");
            return null;
        }
    
        public void onStop() {
            Log.i(TAG, "onStop()");
        }
        public void onPause() {
            Log.i(TAG, "onPause()");
        }
        @Override
        public void onDestroy() {
            player.stop();
            player.release();
            Toast.makeText(this, "Service stopped...", Toast.LENGTH_SHORT).show();
            Log.i(TAG, "onCreate() , service stopped...");
        }
    
        @Override
        public void onLowMemory() {
            Log.i(TAG, "onLowMemory()");
        }
    }
    

    启动服务:

    Intent myService = new Intent(MainActivity.this, BackgroundSoundService.class);
    startService(myService);
    

    停止服务:

    Intent myService = new Intent(MainActivity.this, BackgroundSoundService.class);
    stopService(myService);
    

    Complete code of this example.

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-19
    • 2012-09-02
    • 2014-04-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多