我有一个包含MediaPlayer的活动和服务。我想从活动中控制这个玩家。但是当我试图重置播放器时,我的应用程序在LogCat中崩溃并出现IllegalStateException。 这是我的代码:
MainActivity
Player service;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
//blah-blah-blah
servIntent = new Intent(FeatherPlayerActivity.this, Player.class);
connection = new ServiceConnection() {
public void onServiceConnected(ComponentName name, IBinder binder) {
Log.e("Feather Player", "Сервис подключен");
if (!isTrayed) initialize();
}
public void onServiceDisconnected(ComponentName name) {
}
};
bindService(servIntent, connection, 0);
startService(servIntent);
service = new Player();
service.mediaPlayer = new MediaPlayer(); //just trying to define it again...
}
Player.class(服务)
public class Player extends Service
{
public MediaPlayer mediaPlayer = null;
public Player() {}
public void onCreate() {
super.onCreate();
if (mediaPlayer == null) mediaPlayer = new MediaPlayer();
}
public IBinder onBind(Intent intent) {
if (mediaPlayer == null) mediaPlayer = new MediaPlayer();
return new Binder();
}
public void onRebind(Intent intent) {
super.onRebind(intent);
try {
mediaPlayer.stop();
} catch (Exception ex) {}
}
public boolean onUnbind(Intent intent) {
return super.onUnbind(intent);
}
public void onDestroy() {
super.onDestroy();
try {
mediaPlayer.stop();
} catch (Exception ex) {}
}
}
我如何尝试与玩家互动:
service.player.(command_name);
请帮帮我! 附:对不起,我的英语很糟糕。
更新 logcat的
build.bootloader: unknown
build.brand: acer
build.cpu_abi: armeabi-v7a
build.cpu_abi2: armeabi
build.device: B1-A71
build.display: Acer_AV051_B1-A71_1.174.00_WW_GEN1
build.fingerprint: acer/B1-A71_ww_gen1/B1-A71:4.1.2/JZO54K/1361327270:user/release-keys
build.hardware: mt6517
build.host: asd1sd3
build.id: JZO54K
build.manufacturer: acer
build.model: B1-A71
build.product: B1-A71_ww_gen1
build.radio: unknown
build.serial: 31108826315
build.tags: release-keys
build.time: 1361327270000
build.type: user
build.user: root
version.codename: REL
version.incremental: eng.root.1361327219
version.release: 4.1.2
version.sdk_int: 16
11-10 11:42:14.727 E/AndroidRuntime(32280): FATAL EXCEPTION: main
11-10 11:42:14.727 E/AndroidRuntime(32280): java.lang.IllegalStateException
11-10 11:42:14.727 E/AndroidRuntime(32280): at android.media.MediaPlayer._reset(Native Method)
11-10 11:42:14.727 E/AndroidRuntime(32280): at android.media.MediaPlayer.reset(MediaPlayer.java:1336)
11-10 11:42:14.727 E/AndroidRuntime(32280): at ru.vspr.FeatherPlayerActivity.loadMusic(FeatherPlayerActivity.java:332)
11-10 11:42:14.727 E/AndroidRuntime(32280): at ru.vspr.FeatherPlayerActivity.loadTrack(FeatherPlayerActivity.java:321)
11-10 11:42:14.727 E/AndroidRuntime(32280): at ru.vspr.FeatherPlayerActivity.initialize(FeatherPlayerActivity.java:185)
11-10 11:42:14.727 E/AndroidRuntime(32280): at ru.vspr.FeatherPlayerActivity.access$1000002(FeatherPlayerActivity.java)
11-10 11:42:14.727 E/AndroidRuntime(32280): at ru.vspr.FeatherPlayerActivity$100000000.onServiceConnected(FeatherPlayerActivity.java:72)
11-10 11:42:14.727 E/AndroidRuntime(32280): at android.app.LoadedApk$ServiceDispatcher.doConnected(LoadedApk.java:1127)
11-10 11:42:14.727 E/AndroidRuntime(32280): at android.app.LoadedApk$ServiceDispatcher$RunConnection.run(LoadedApk.java:1144)
11-10 11:42:14.727 E/AndroidRuntime(32280): at android.os.Handler.handleCallback(Handler.java:615)
11-10 11:42:14.727 E/AndroidRuntime(32280): at android.os.Handler.dispatchMessage(Handler.java:92)
11-10 11:42:14.727 E/AndroidRuntime(32280): at android.os.Looper.loop(Looper.java:153)
11-10 11:42:14.727 E/AndroidRuntime(32280): at android.app.ActivityThread.main(ActivityThread.java:4987)
11-10 11:42:14.727 E/AndroidRuntime(32280): at java.lang.reflect.Method.invokeNative(Native Method)
11-10 11:42:14.727 E/AndroidRuntime(32280): at java.lang.reflect.Method.invoke(Method.java:511)
11-10 11:42:14.727 E/AndroidRuntime(32280): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
11-10 11:42:14.727 E/AndroidRuntime(32280): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
11-10 11:42:14.727 E/AndroidRuntime(32280): at dalvik.system.NativeStart.main(Native Method)