gpt4 book ai didi

android - 将 MediaPlayer 服务绑定(bind)到 Activity 时出错

转载 作者:行者123 更新时间:2023-11-30 02:11:57 24 4
gpt4 key购买 nike

<分区>

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

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

第 1 步:首先通过在 Activity 的 onCreate 上调用 doBindService 将服务绑定(bind)到 Activity ,同时将 Intent 传递给服务。”

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

list :

<?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;
}
}

最后是我尝试将服务绑定(bind)到的 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();
}


}

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com