0

我想用另一個mediaplayer在另一個活動中播放音樂。示例 MediaPlayer主要活動以及如何在另一個活動中播放的內容(如何將值從一個活動傳遞給mediaplayer)的說明。mediaPlayer global on other activity

回答

0

您可以使用服務:

public class BackgroundSound 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.audiofile); 
    player.setLooping(true); // Set looping 
    player.setVolume(100, 100); 

} 

public int onStartCommand(Intent intent, int flags, int startId) { 
    player.start(); 
    return 1; 
} 

public void onStart(Intent intent, int startId) { 
    // TO DO 
} 

public IBinder onUnBind(Intent arg0) { 
    // TO DO Auto-generated method 
    return null; 
} 

public void onStop() { 

} 

public void onPause() { 

} 

@Override 
public void onDestroy() { 
    player.stop(); 
    player.release(); 
} 

@Override 
public void onLowMemory() { 

} 
    } 

然後在您的另一活動:

public class ActivityB extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activityb); 

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

    msg.show(); 

} 

} 
+0

很抱歉,但你可以扔存檔與節目),我甚至不掌握lemaster) – user3708320

+0

那麼我沒有這個程序,但我可以告訴你,你可以複製這個並從公共類聲明粘貼到底部。如果你想添加任何東西,只要確保你在onCreate或out。否則告訴我你的錯誤,我會盡力幫助你。 – Umitk