2014-06-18 82 views
0

我創建了一個音樂服務(它擴展了服務),但每次我打電話時,應用程序崩潰,我找不到問題,因此我在這裏問。 任何幫助將大有幫助。音樂服務崩潰

class MusicService extends Service { 
MediaPlayer mediaPlayer; 



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

@Override 

public void onCreate() { 
    super.onCreate(); 

} 

@Override 

public int onStartCommand(Intent intent, int flags, int startId) { 
    Log.d("Music","music"); 
    mediaPlayer = MediaPlayer.create(this, R.raw.song2); 
    mediaPlayer.setLooping(true); 
    mediaPlayer.setVolume(100, 100);  
    Log.d("Music","Created"); 

     mediaPlayer.start(); 

    return START_STICKY; 
} 


public void onDestroy() { 
    if (mediaPlayer.isPlaying()) { 
     mediaPlayer.stop(); 
    } 
    mediaPlayer.release(); 
} 
} 

我已經宣佈在清單的服務,我已經使用StartService把它稱爲:

 <service 
     android:name="com.example.trviaforbagrut.MusicService" 
     /> 

電話:

  Intent I2= new Intent(this,MusicService.class); 
     startService(I2); 

任何幫助將是巨大的,因爲我是真的陷入困境。 下面的答案幫助了我很多,現在音樂正在播放和停止!

+1

張貼您的logcat .... –

回答

1

嘗試在原始文件夾此代碼和資源文件夾中創建並粘貼音頻文件,這個文件夾: -

MainActivity.class

import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import android.view.View; 
import android.content.Intent; 

public class MainActivity extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 
    } 

    public void playAudio(View view) { 
     Intent objIntent = new Intent(this, PlayAudio.class); 
     startService(objIntent); 
    } 

    public void stopAudio(View view) { 
     Intent objIntent = new Intent(this, PlayAudio.class); 
     stopService(objIntent);  
    } 


} 

service.class

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

public class PlayAudio extends Service{ 
    private static final String LOGCAT = null; 
    MediaPlayer objPlayer; 

    public void onCreate(){ 
     super.onCreate(); 
     Log.d(LOGCAT, "Service Started!"); 
     objPlayer = MediaPlayer.create(this,R.raw.song2); 
    } 

    public int onStartCommand(Intent intent, int flags, int startId){ 
     objPlayer.start(); 
     Log.d(LOGCAT, "Media Player started!"); 
     if(objPlayer.isLooping() != true){ 
      Log.d(LOGCAT, "Problem in Playing Audio"); 
     } 
     return 1; 
    } 

    public void onStop(){ 
     objPlayer.stop(); 
     objPlayer.release(); 
    } 

    public void onPause(){ 
     objPlayer.stop(); 
     objPlayer.release(); 
    } 

    public void onDestroy(){ 
     objPlayer.stop(); 
     objPlayer.release(); 
    } 

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

和xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginLeft="20dp" 
     android:layout_marginTop="30dp" 
     android:onClick="playAudio" 
     android:text="Play" /> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/button1" 
     android:layout_alignBottom="@+id/button1" 
     android:layout_marginLeft="24dp" 
     android:layout_toRightOf="@+id/button1" 
     android:onClick="stopAudio" 
     android:text="Stop" /> 

</RelativeLayout> 

清單文件:

<application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name=".MainActivity" 
      android:label="@string/title_activity_main" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <service android:name="PlayAudio" android:enabled="true"> 
     </service> 
</application>