在Android应用程式游戏上玩游戏时使用背景音乐

时间:2018-10-18 20:04:46

标签: java android

我是Android Developer的新手 我正在Android Studio上构建游戏应用程序。但是,我在玩游戏时遇到有关背景音乐的问题。我已经使用 Bound Service 处理该类:MusicService扩展Service实现了MediaPlayer.OnErrorListener。但是,我的应用程序在运行时已停止。 我试图修复和探索解决方案,但是无法运行应用程序... 这是源代码:

MusicService.java

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 MediaPlayer Player;


    public MusicService() {
    }

    public class ServiceBinder extends Binder {
        public 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.one);
        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;
    }
}

以及 MainActivity.java

public class MainActivity extends AppCompatActivity {
    private boolean mIsBound = false;
    private MusicService mServ;
    private ServiceConnection Scon =new ServiceConnection(){
    public void onServiceConnected(ComponentName name, IBinder service) {
            MusicService.ServiceBinder binder = (MusicService.ServiceBinder)service;
            mServ =binder.getService();
        }

        public void onServiceDisconnected(ComponentName name) {
            mServ = null;
        }
    };

    // Activity create UI its
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }
    // Activity start.
    @Override
    protected void onStart() {
        super.onStart();
        // create Intent for MusicService.
        Intent intent = new Intent(MainActivity.this,MusicService.class);
        // call method bindService(..) to bind activity
        this.bindService(intent, Scon, Context.BIND_AUTO_CREATE);
    }
    //Activity stop
    @Override
    protected void onStop() {
        super.onStop();
        if (mIsBound) {
            this.unbindService(Scon);
            mIsBound = false;
        }
    }

然后,我也没有忘记在AndroidManifest中添加以下代码

<service
            android:name=".MusicService"
            android:enabled="true"
            android:exported="true"></service>

这就是我的所有问题。这让我很困惑 请帮忙 非常感谢

1 个答案:

答案 0 :(得分:1)

您可以在logcat中添加更多日志调试吗?不确定,但我想问题是您打电话给了

 mPlayer.start();

在onStartComand()中。您的服务应实现MediaPlayer.OnPreparedListener,MediaPlayer.OnErrorListener,MediaPlayer.OnCompletionListener,并且以上函数应在

中调用
void onPrepared(MediaPlayer mp)