如何在背景音乐上播放声音 [英] how to play sound Over background music

查看:135
本文介绍了如何在背景音乐上播放声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an application that has single activity and I know How to play a background music using the Media player. As I have only one activity so I think I really do not need to play this music in the service. so I have little confusion so this is what I want.

我想要的东西:

在我的应用中,我有一个活动和一些按钮.我想在单击按钮时播放不同长度的声音.但是,即使我真的不想停止播放背景音乐,它也应该同时在后台播放.

In my app I have a single activity and some buttons in it. I want to play sounds of different length on button click. But Mean while I really do not want to stop the back ground music it should play in background on the same time.

我的问题是(根据我的最高要求)

  1. 是否可以以不干扰其他声音的方式播放两种声音?

  1. Is it possible to play two sounds in such a way that they really do not disturb the other sound ?

什么是合适的方法?

我不想播放正在使用的背景音乐,因为我会给静音按钮以使背景音乐静音.因此,使用UI按钮处理服务会很困难吗?不管怎样

I do not want to play the background music in service as I would give the mute button to mute the background music. So it would be difficult to handle the service with UI button? So whats the either way

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

If you have any code and idea please share me. Any Link would be appreciated. Thanks in Advance.

推荐答案

您可以将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. 首先通过在活动的onCreate上调用doBindService来将服务绑定到活动,以将意图传递给服务.
  2. 通过显式的Intent启动服务: Intent music = new Intent(); music.setClass(this,MusicService.class); startService(music);
  3. 在活动中,无论您想暂停,恢复还是停止音乐,请按以下方式调用相应的服务功能: mServ.pauseMusic(); mServ.resumeMusic(); mServ.stopMusic();
  4. 请不要忘记从活动中取消服务绑定的地方调用doUnbindService.一个理想的地方是调用活动的onDestroy()方法.
  5. 在应用程序的AndroidManifest文件中,粘贴以下XML代码:
  1. First bind the service to the activity by calling doBindService on your activity's onCreate as passing an intent to the service.
  2. Start the service by an explicit Intent: Intent music = new Intent(); music.setClass(this,MusicService.class); startService(music);
  3. From your activity, wherever you want to pause, resume or stop music, call the corresponding service functions as follows: mServ.pauseMusic(); mServ.resumeMusic(); mServ.stopMusic();
  4. Don't forget to call doUnbindService from places where you want to unbind the service from the activity. An ideal place is the call to activity's onDestroy()method.
  5. In your application's AndroidManifest file, paste the following XML code:

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

希望这对您有所帮助.

这篇关于如何在背景音乐上播放声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆