2012-02-09 647 views
1

我做Android上背景音樂播放小的模擬,但它不工作...有代碼:音樂播放

[我的活動,在的onCreate]

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

[音樂服務類]

public class MusicService extends Service implements MediaPlayer.OnErrorListener { 

private final IBinder mBinder = new ServiceBinder(); 
MediaPlayer mPlayer; 
private int length = 0; 

public class ServiceBinder extends Binder { 
    MusicService getService() 
    { 
     return MusicService.this; 
    } 
} 

@Override 
public IBinder onBind(Intent arg0){ 
    return mBinder; 
} 

@Override 
public void onCreate() { 

    super.onCreate(); 
    mPlayer = MediaPlayer.create(this, R.raw.song1); 
    mPlayer.setOnErrorListener(this); 

    if(mPlayer!= null) 
    { 
    mPlayer.setLooping(true); 
    mPlayer.setVolume(100,100); 
    } 


    mPlayer.setOnErrorListener(new OnErrorListener() { 

     public boolean onError(MediaPlayer mp, int what, int extra){ 

      onError(mPlayer, what, extra); 
      return true; 
     } 
    }); 
} 

@Override 
public int onStartCommand (Intent intent, int flags, int startId) 
{ 
    mPlayer.start(); 
    return START_STICKY; 
} 

public void pauseMusic() 
{ 
    if(mPlayer.isPlaying()) 
    { 
     mPlayer.pause(); 
     length=mPlayer.getCurrentPosition(); 
    } 
} 

public void resumeMusic() 
{ 
    if(mPlayer.isPlaying()==false) 
    { 
     mPlayer.seekTo(length); 
     mPlayer.start(); 
    } 
} 

public void stopMusic() 
{ 
    mPlayer.stop(); 
    mPlayer.release(); 
    mPlayer = null; 
} 

@Override 
public void onDestroy() 
{ 
    super.onDestroy(); 
    if(mPlayer != null) 
    { 
     try{ 
      mPlayer.stop(); 
      mPlayer.release(); 
     } 
     finally { 
      mPlayer = null; 
     } 
    } 
} 

public boolean onError(MediaPlayer mp, int what, int extra) { 

    Toast.makeText(this, "music player failed", Toast.LENGTH_SHORT).show(); 
    if(mPlayer != null) 
    { 
     try{ 
      mPlayer.stop(); 
      mPlayer.release(); 
     } 
     finally { 
      mPlayer = null; 
     } 
    } 
    return false; 
} 
} 

我在模擬器中聽不到任何歌曲。我沒有完成清單文件是這樣的:

<service 
    android:name="MusicService" 
    android:enabled="true"> 
</service> 

有別的添加到清單,有沒有我的代碼或者用我的模擬器有問題? 謝謝!

+0

logcat中的任何錯誤? – Deva 2012-02-09 19:31:23

+0

我想我有一個與頻道有關的錯誤 - 已損壞。它會影響嗎? – Madrugada 2012-02-09 19:47:00

回答

0

我正在剪切Android上的背景音樂播放代碼。這將解決你的問題。

我頭創建了兩個類,第一個是MainActivity,第二個是MyService。

MainActivity代碼是

公共類MainActivity延伸活動實現OnClickListener {

}

現在爲MyService代碼是

公共類爲MyService延伸服務{

private static final String TAG = "MyService"; 
MediaPlayer player; 

@Override 
public IBinder onBind(Intent intent) { 

    return null; 
} 

@Override 
public void onCreate() { 
    Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show(); 
    Log.d(TAG, "onCreate"); 

    player = MediaPlayer.create(this, R.raw.braincandy); 
    player.setLooping(false); // Set looping 
} 

@Override 
public void onDestroy() { 
    Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show(); 
    Log.d(TAG, "onDestroy"); 
    player.stop(); 
} 

@Override 
public void onStart(Intent intent, int startid) { 
    Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show(); 
    Log.d(TAG, "onStart"); 
    player.start(); 
} 

}

您必須在您的「res」文件夾中創建一個「原始」文件夾,而不是將您的音樂文件粘貼到該原始文件夾中。

+0

並在您的manifest.xml中添加以下代碼 2013-12-30 12:02:40