2012-12-26 1 views
0

내 앱에 배경 음악을 추가하려고합니다. 많은 예제를 읽었지만 항상 같은 문제가 있습니다. 음악이 시작되지 않고 오류 메시지가 없습니다.내 앱의 배경 음악이 시작되지 않습니다.

나는 배경 음악을 menages 클래스가

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 메서드 내, 나는이 내 매니페스트

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

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

이다 사용

아마도 문제는 minSdkVersion = "8"입니다! MinSdkVersion 8에 대한 배경 음악이 지원되지 않을 수 있습니까?

+0

'player.prepare();'를 추가하기 전에. –

+0

아니, 아직 시작되지 않습니다 :( – MDP

+0

나는 BackgroundSoundService 클래스를 호출 할 수 있다고 생각합니다. 을 BackgroundSoundService 클래스의에서 onCreate 메서드에서, 나는 텍스트 뷰에서 뭔가를 작성했습니다.하지만 could't이 있음을 의미 내 주요 클래스에서 나는 BackgroundSoundService 클래스를 호출 할 수 아니에요! – MDP

답변

1

서비스서비스을 시작하기 전에 android manifest 파일로 선언해야합니다. 이 프로세스는 다음과 같이 태그 내에서 수행됩니다.

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

<service android:name="BackgroundSoundService" android:enabled="true"></service> 
</application> 
+0

감사합니다. 이제 작동합니다! 그게 문제였습니다. 감사합니다. D – MDP

+0

사용자가 다른 앱으로 전환 할 때 음악을 멈추는 데 문제가 있습니다. 인터넷에서 읽는 것으로 충분하다고 생각했습니다. public void onPause() { \t player.stop(); \t player.release(); } – MDP

+0

Google 다시 한번. 그리고 뭔가 시험해보십시오. – Rahul

관련 문제