启动服务以播放背景音乐会出错

时间:2014-09-04 01:58:31

标签: java android service background

我正在尝试在我的Android应用程序中运行 背景音乐 ,但我收到错误

如果我的问题格式错误,请纠正我

如何解决这个问题?

我有一项主要活动 MainActivity

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent svc=new Intent(this, BackgroundMusic.class);
        startService(svc);
}

在服务活动 BackgroundMusic

package com.example.tttgame;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class BackgroundMusic extends Service {
    private static final String TAG = null;
    MediaPlayer player;
    public IBinder onBind(Intent arg0) {

    return null;
}
@Override
public void onCreate() {
    super.onCreate();
    player = MediaPlayer.create(this, R.raw.music);
    player.setLooping(true); // Set looping
    player.setVolume(100,100);

}
public int onStartCommand(Intent intent, int flags, int startId) {
    player.start();
    return 1;
}

public void onStart(Intent intent, int startId) {
    // TO DO
}
public IBinder onUnBind(Intent arg0) {
    // TO DO Auto-generated method
    return null;
}

public void onStop() {

}
public void onPause() {

}
@Override
public void onDestroy() {
    player.stop();
    player.release();
}

@Override
public void onLowMemory() {

}

}

AndroidManifiest中的

    <activity
        android:name="com.example.tttgame.BoardActivity"
        android:label="@string/app_name" >
    </activity>
    <service android:enabled="true" android:name="com.example.tttgame.BackgroundMusic" />

</application>

我的错误讯息:

09-03 21:45:20.387: E/MainActivity(1392): starting Main Activity ..
09-03 21:45:22.077: E/MainActivity(1392): checking if key 'flag' exists in preferences..
09-03 21:45:22.377: E/MediaPlayer(1392): Should have subtitle controller already set
09-03 21:45:22.797: E/MediaPlayer(1392): error (1, -2147483648)
09-03 21:45:22.837: E/AndroidRuntime(1392): FATAL EXCEPTION: main
09-03 21:45:22.837: E/AndroidRuntime(1392): Process: com.example.tttgame, PID: 1392
09-03 21:45:22.837: E/AndroidRuntime(1392): java.lang.RuntimeException: Unable to create service com.example.tttgame.BackgroundMusic: java.lang.NullPointerException
09-03 21:45:22.837: E/AndroidRuntime(1392):     at android.app.ActivityThread.handleCreateService(ActivityThread.java:2563)
09-03 21:45:22.837: E/AndroidRuntime(1392):     at android.app.ActivityThread.access$1700(ActivityThread.java:135)
09-03 21:45:22.837: E/AndroidRuntime(1392):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1479)
09-03 21:45:22.837: E/AndroidRuntime(1392):     at android.os.Handler.dispatchMessage(Handler.java:102)
09-03 21:45:22.837: E/AndroidRuntime(1392):     at android.os.Looper.loop(Looper.java:137)
09-03 21:45:22.837: E/AndroidRuntime(1392):     at android.app.ActivityThread.main(ActivityThread.java:4998)
09-03 21:45:22.837: E/AndroidRuntime(1392):     at java.lang.reflect.Method.invokeNative(Native Method)
09-03 21:45:22.837: E/AndroidRuntime(1392):     at java.lang.reflect.Method.invoke(Method.java:515)
09-03 21:45:22.837: E/AndroidRuntime(1392):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
09-03 21:45:22.837: E/AndroidRuntime(1392):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
09-03 21:45:22.837: E/AndroidRuntime(1392):     at dalvik.system.NativeStart.main(Native Method)
09-03 21:45:22.837: E/AndroidRuntime(1392): Caused by: java.lang.NullPointerException
09-03 21:45:22.837: E/AndroidRuntime(1392):     at com.example.tttgame.BackgroundMusic.onCreate(BackgroundMusic.java:21)
09-03 21:45:22.837: E/AndroidRuntime(1392):     at android.app.ActivityThread.handleCreateService(ActivityThread.java:2553)
09-03 21:45:22.837: E/AndroidRuntime(1392):     ... 10 more

2 个答案:

答案 0 :(得分:0)

player = MediaPlayer.create(this, R.raw.music);

我认为这是问题所在 因为错误是java.lang.NullPointerException

答案 1 :(得分:0)

查看我的班级的声音并参考。

public class SoundService extends Service {

private static MediaPlayer player;

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onStart(Intent intent, int startId) {

    try {
        if (player != null) {
            player.stop();
            player.release();
            player= null;
        }

        player = MediaPlayer.create(this, R.raw.background);
        player.setLooping(true);
        player.start();
    } catch (Exception e) {
    }
}

public static boolean isMediaPlaying() {
    if (player != null) {
        try {
            return player.isPlaying();
        } catch (Exception e) {
        }
    }

    return false;
}

@Override
public void onDestroy() {
    if (player!= null) {
        player.stop();
        player.release();
        player = null;
    }

    super.onDestroy();
}

}

如果已经存在,我已经在onStart中重新评估了媒体播放器。并且无需使用onstart命令。