【问题标题】:how to play sound Over background music如何在背景音乐中播放声音
【发布时间】:2016-05-05 07:59:12
【问题描述】:

我有一个具有单一活动的应用程序,并且我知道如何使用媒体播放器播放背景音乐。由于我只有一项活动,所以我认为我真的不需要在服务中播放此音乐。所以我有点困惑所以这就是我想要的。

我想要什么:

在我的应用程序中,我有一个活动和一些按钮。我想在按钮点击时播放不同长度的声音。但是虽然我真的不想停止背景音乐,但它应该同时在背景中播放。

所以我的问题是(基于我给定的上限)

  1. 是否可以在播放两个声音时不干扰另一个声音?

  2. 什么是合适的方法?

  3. 我不想在服务中播放背景音乐,因为我会使用静音按钮来静音背景音乐。那么使用 UI 按钮处理服务会很困难吗?那么两种方法都是什么

如果您有任何代码和想法,请与我分享。任何链接将不胜感激。提前致谢。

【问题讨论】:

    标签: android android-mediaplayer android-music-player


    【解决方案1】:

    您可以将ServiceMediaPlayer 一起使用

    服务类-

    import android.app.Service;
    import android.content.Intent;
    import android.media.MediaPlayer;
    import android.media.MediaPlayer.OnErrorListener;
    import android.os.Binder;
    import android.os.IBinder;
    import android.widget.Toast;
    
    public class MusicService extends Service  implements MediaPlayer.OnErrorListener{
    
        private final IBinder mBinder = new ServiceBinder();
        MediaPlayer mPlayer;
        private int length = 0;
    
        public MusicService() { }
    
        public class ServiceBinder extends Binder {
             MusicService getService()
             {
                return MusicService.this;
             }
        }
    
        @Override
        public IBinder onBind(Intent arg0){return mBinder;}
    
        @Override
        public void onCreate (){
          super.onCreate();
    
           Player = MediaPlayer.create(this, R.raw.jingle);
           mPlayer.setOnErrorListener(this);
    
           if(mPlayer!= null)
            {
                mPlayer.setLooping(true);
                mPlayer.setVolume(100,100);
            }
    
    
            mPlayer.setOnErrorListener(new OnErrorListener() {
    
          public boolean onError(MediaPlayer mp, int what, int
              extra){
    
                onError(mPlayer, what, extra);
                return true;
            }
              });
        }
    
        @Override
        public int onStartCommand (Intent intent, int flags, int startId)
        {
             mPlayer.start();
             return START_STICKY;
        }
    
        public void pauseMusic()
        {
            if(mPlayer.isPlaying())
            {
                mPlayer.pause();
                length=mPlayer.getCurrentPosition();
    
            }
        }
    
        public void resumeMusic()
        {
            if(mPlayer.isPlaying()==false)
            {
                mPlayer.seekTo(length);
                mPlayer.start();
            }
        }
    
        public void stopMusic()
        {
            mPlayer.stop();
            mPlayer.release();
            mPlayer = null;
        }
    
        @Override
        public void onDestroy ()
        {
            super.onDestroy();
            if(mPlayer != null)
            {
            try{
             mPlayer.stop();
             mPlayer.release();
                }finally {
                    mPlayer = null;
                }
            }
        }
    
        public boolean onError(MediaPlayer mp, int what, int extra) {
    
            Toast.makeText(this, "music player failed", Toast.LENGTH_SHORT).show();
            if(mPlayer != null)
            {
                try{
                    mPlayer.stop();
                    mPlayer.release();
                }finally {
                    mPlayer = null;
                }
            }
            return false;
        }
    }
    

    在您的 Activity 类中使用 ServiceConnection

    private boolean mIsBound = false;
    private MusicService mServ;
    private ServiceConnection Scon =new ServiceConnection(){
    
        public void onServiceConnected(ComponentName name, IBinder
         binder) {
        mServ = ((MusicService.ServiceBinderbinder).getService();
        }
    
        public void onServiceDisconnected(ComponentName name) {
            mServ = null;
        }
        };
    
        void doBindService(){
            bindService(new Intent(this,MusicService.class),
                    Scon,Context.BIND_AUTO_CREATE);
            mIsBound = true;
        }
    
        void doUnbindService()
        {
            if(mIsBound)
            {
                unbindService(Scon);
                mIsBound = false;
            }
        }
    }
    

    开始、暂停、恢复和停止音乐

    按照步骤进行

    1. 首先通过调用 Activity 的 onCreate 上的 doBindService 将服务绑定到 Activity,将 Intent 传递给服务。
    2. 通过显式Intent 启动服务: Intent music = new Intent(); music.setClass(this,MusicService.class); startService(music);
    3. 从您的活动中,无论您想暂停、恢复或停止音乐,请调用相应的服务函数,如下所示: mServ.pauseMusic(); mServ.resumeMusic(); mServ.stopMusic();
    4. 不要忘记在您想解除服务与活动绑定的地方致电doUnbindService。一个理想的地方是调用活动的onDestroy()method。
    5. 在应用程序的 AndroidManifest 文件中,粘贴以下 XML 代码:

    "service android:name="MusicService" android:enabled="true"

    希望对你有所帮助。

    【讨论】:

    • 太好了,所以这将继续播放服务,然后使用按钮我可以创建本地媒体播放器并播放声音?是这样吗?
    • 嗯,但是如果你想从外部而不是从活动中完成所有事情,你将需要制作一个小部件。或者您也可以在通知栏中执行此操作。关键在于你需要如何执行你的代码
    • 不,你没有得到我的第二个想法。我的意思是我希望背景音乐继续播放,在按钮点击时,让我们说按钮点击一个新的声音播放,即“你点击了下一个按钮”,所以我想在点击按钮时播放这个声音,同时保持背景音乐保持玩
    • @AllayKhalil 看看这个stackoverflow.com/a/15931595/842607。在您的情况下,解决方法可能是您可以使用相同的代码制作多个服务并同时播放它们。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-24
    • 2011-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多