将 MediaPlayer 服务绑定到 Activity 时出错

Error binding MediaPlayer service to Activity

一直在学习以下教程: [1]: http://www.codeproject.com/Articles/258176/Adding-Background-Music-to-Android-App

关于“开始、暂停、恢复和停止音乐”这一步 按照给定的步骤操作:

第 1 步:首先通过在 activity 的 onCreate 上调用 doBindService 将服务绑定到 activity 并将意图传递给服务。”

我通过添加绑定服务来添加:doBindService(); 在我的 onCreate();方法,想知道为什么它不起作用? 不需要深入的解释,但对它的工作很有用,发现教程某些部分的措辞方式有点混乱。

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="cct.mad.lab"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="19"
        android:targetSdkVersion="19" />

    <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >  
    <service android:name=".MusicService" android:enabled="true"/>
    <activity
        android:name="cct.mad.lab.MainMenu"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".GameActivity"
        android:label="@string/app_name" >

     </activity>
</application>

</manifest>

音乐服务Class:

package cct.mad.lab;

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();

   mPlayer = MediaPlayer.create(this, R.raw.soundtrack);
   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 我试图将服务绑定到的游戏Activity:

package cct.mad.lab;

import android.os.Bundle;
import android.os.IBinder;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class GameActivity extends Activity {

GameView gameView;// Reference the gameView
MusicService musicplayer;

public boolean mIsBound = false;
private MusicService mServ;
private ServiceConnection Scon =new ServiceConnection(){

    public void onServiceConnected(ComponentName name, IBinder
     binder) {
        mServ = ((MusicService.ServiceBinder) binder).getService();
    }

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

    public void doBindService(){
        Intent bindIntent = new Intent(this,MusicService.class);
         mIsBound = bindService(bindIntent,Scon,this.BIND_AUTO_CREATE);
    }

     public void doUnbindService()
    {
        if(mIsBound)
        {
            unbindService(Scon);
            mIsBound = false;
        }
    }


protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    doBindService();
    Intent recieve = getIntent();
    String message = recieve.getExtras().getString("MESSAGE");
    // remove title bar
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    // Set the layout
    setContentView(R.layout.game_view_container);
    // Get the id of the layout
    RelativeLayout mainscreen = (RelativeLayout) findViewById(R.id.mainscreen);
    // Make the GameView
    gameView = new GameView(this,message);
    // Get  data from intent and config gameView here

    this.doBindService();
    Intent music = new Intent(this,MusicService.class);
    startService(music);

    gameView.setLayoutParams(new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.MATCH_PARENT));
    // Add GameView
    mainscreen.addView(gameView);
}

/* Called when activity is done and should be closed. 
 * The ActivityResult is propagated back to whoever launched via         onActivityResult()
 */
public void finish(){
    Intent returnIntent = new Intent();
    returnIntent.putExtra("GAME_SCORE",(gameView.getHitCount()));
    setResult(RESULT_OK, returnIntent);
    doUnbindService();
    super.finish();
}


}

我编辑了这个答案以使其干净。 问题是 MediaPlayer.create() returns 为空。根据 doc 如果创建失败就会发生,所以我猜是文件格式或文件本身有问题。 因此,请检查支持的音频格式 here.