0

アプリにバックグラウンドミュージックを追加しようとしています。私はたくさんの例を読みましたが、私はいつも同じ問題を抱えています。音楽が始まらず、エラーメッセージもありません。

身元音楽を管理するクラスがあります

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;

public class BackgroundSoundService 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.jingle);
        player.setLooping(true); // Set looping
        player.setVolume(100,100);
        player.start();
}
@Override
public void onStart(Intent intent, int startId) {

    super.onStart(intent, startId);
}
public void onDestroy() {

    super.onDestroy();
}

protected void onNewIntent() {
    player.pause();
}
}

そして、私のメインクラスでは、onCreateメソッド内で

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

これは私のマニフェストです

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.giocobambini.MainActivity"
            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="com.example.giocobambini.BackgroundSoundService"
            android:label="@string/title_activity_background_sound_service" >
        </activity>


    </application>

</manifest>

たぶん問題はminSdkVersion="8"です!! たぶんminSdkVersion8のバックグラウンドミュージックはサポートされていませんか?

4

1 に答える 1

1

サービスを開始する前に、Androidマニフェストファイルを使用してサービスを宣言する必要があります。このプロセスは、次のようにタグ内で実行されます。

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >

<service android:name="BackgroundSoundService"  android:enabled="true"></service>
</application>
于 2012-12-26T18:04:10.503 に答える