2014-06-18 81 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>