按照以下教程: [1]: http://www.codeproject.com/Articles/258176/Adding-Background-Music-to-Android-App
在步骤"开始,暂停,恢复和停止音乐 按照给定的步骤:
步骤1:首先通过在活动的onCreate上调用doBindService将服务绑定到活动,同时将意图传递给服务。"
我添加了绑定服务,只包括: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>
音乐服务类:
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,GameActivity:
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();
}
}
答案 0 :(得分:1)
我编辑这个答案以使其干净。
问题是DataId FieldId Amount year
1 1 100 2015
2 2 200 2015
3 3 300 2015
4 4 600 2016
返回null。根据{{3}},如果创建失败,则会发生这种情况,因此我认为文件格式或文件本身有问题。
因此,请检查支持的音频格式doc。