Hintergrundmusik über mehrere Activities

  • Antworten:4
John Do
  • Forum-Beiträge: 36

23.02.2012, 14:54:06 via Website

Hallo liebe Mitglieder,

im Folgenden präsentiere ich Lösungsvorschläge für die Implementierung von Hintergrundmusik in einer App. Es gibt drei Möglichkeiten mittels einer statischen Klasse, mittels Singelton und mittels Service.
Ein lauffähiges Programm unter Verwendung eines Services habe ich bisher noch nicht hinbekommen. Werde es ab nachliefern, wenn ich es geschafft habe. Meine Frage ist, wo Vor- und Nachteile der unterschiedlichen Lösungen liegen und auf welche Fehlerquellen man achten sollte.

static solution
1public class MyStaticMusicPlayerClass {
2 static MediaPlayer player = new MediaPlayer();
3 private static int currentsong;
4 private static int[] song = { R.raw.blendergameengine, R.raw.uffiewordy };
5 static Context mContext;
6
7 public static void playSong(int res) {
8 player = MediaPlayer.create(mContext, song[res]);
9 player.start();
10
11 player.setOnCompletionListener(new OnCompletionListener() {
12 public void onCompletion(MediaPlayer arg0) {
13 player.release();
14 nextSong();
15 }});}
16
17 private static void nextSong() {
18 if (++currentsong >= song.length) {
19 currentsong = 0;
20 playSong(currentsong);
21 } else {
22 playSong(currentsong);
23 }}
24
25 public static void stopSong() {
26 if (player != null) {
27 player.stop();}}
28
29 public static void startSong() {
30 if (player != null) {
31 player.start();}
32 }
33 public static void setContext(Context context) {
34 mContext = context;
35 }
36 public static void destroyPlayer() {
37 // TODO Auto-generated method stub
38 if (player != null) {
39 player.stop();
40 player.release();
41 player = null;
42 }
43 }
44 public static void pauseSong() {
45 // TODO Auto-generated method stub
46 if (player != null) {
47 player.pause();}
48 }
49}

1public class StaticMusicplayerActivity extends Activity {
2 Button buttonStart, buttonStop,buttonActivty;
3 /** Called when the activity is first created. */
4 @Override
5 public void onCreate(Bundle savedInstanceState) {
6 super.onCreate(savedInstanceState);
7 setContentView(R.layout.main);
8 MyStaticMusicPlayerClass.setContext(this);
9 buttonActivty = (Button) findViewById(R.id.buttonActivity);
10 buttonStart = (Button) findViewById(R.id.buttonStart);
11 buttonStop = (Button) findViewById(R.id.buttonStop);
12 buttonActivty.setOnClickListener(StartActivtyOnClickListener);
13 buttonStart.setOnClickListener(StartOnClickListener);
14 buttonStop.setOnClickListener(EndOnClickListener);
15 }
16 OnClickListener StartActivtyOnClickListener = new OnClickListener() {
17 public void onClick(View arg0) {
18 Intent gamefield = new Intent(StaticMusicplayerActivity.this, zweiteActivity.class);
19 startActivity(gamefield);
20 }
21 };
22 OnClickListener StartOnClickListener = new OnClickListener() {
23 public void onClick(View arg0) {
24 MyStaticMusicPlayerClass.playSong(0);
25 }
26 };
27 OnClickListener EndOnClickListener = new OnClickListener() {
28 public void onClick(View arg0) {
29
30 }
31 };
32protected void onPause() {
33 super.onPause();
34 MyStaticMusicPlayerClass.pauseSong();
35 };
36 protected void onDestroy(){
37 super.onDestroy();
38 MyStaticMusicPlayerClass.destroyPlayer();
39 };
40}

1public class zweiteActivity extends Activity{
2
3 Button buttonStop;
4
5 protected void onCreate(Bundle savedInstanceState) {
6 super.onCreate(savedInstanceState);
7 setContentView(R.layout.zweitemain);
8 buttonStop = (Button) findViewById(R.id.buttonStop);
9 buttonStop.setOnClickListener(AtoptheMusicClickListener);
10 }
11
12 OnClickListener AtoptheMusicClickListener = new OnClickListener() {
13 public void onClick(View arg0) {
14 MyStaticMusicPlayerClass.stopSong();
15 }
16 };
17
18 protected void onResume() {
19 super.onResume();
20 MyStaticMusicPlayerClass.startSong();
21 }
22
23 protected void onPause() {
24 super.onPause();
25 }
26
27 protected void onDestroy(){
28 super.onDestroy();
29 MyStaticMusicPlayerClass.destroyPlayer();
30 };
31}

— geändert am 23.02.2012, 14:55:09

Antworten
John Do
  • Forum-Beiträge: 36

23.02.2012, 15:01:24 via Website

Singleton Lösung
Zur Verdeutlichung des Unterschiedes werde ich den gesamten Quellcode posten, obwohl viele Teile gleich sind.

1public class SingletonMusicPlayerActivity extends Activity {
2
3 private SinletonMusicPlayer mplayer = SinletonMusicPlayer.getInstance();
4 Button buttonStart, buttonStop,buttonActivty;
5
6 /** Called when the activity is first created. */
7 @Override
8 public void onCreate(Bundle savedInstanceState) {
9 super.onCreate(savedInstanceState);
10 setContentView(R.layout.main);
11
12 mplayer.setContext(this);
13 buttonActivty = (Button) findViewById(R.id.buttonActivity);
14 buttonStart = (Button) findViewById(R.id.buttonStart);
15
16 buttonActivty.setOnClickListener(StartActivtyOnClickListener);
17 buttonStart.setOnClickListener(StartOnClickListener);
18
19 }
20
21
22 OnClickListener StartActivtyOnClickListener = new OnClickListener() {
23 public void onClick(View arg0) {
24 Intent gamefield = new Intent(SingletonMusicPlayerActivity.this, zweiteActivity.class);
25 startActivity(gamefield);
26 }
27 };
28
29 OnClickListener StartOnClickListener = new OnClickListener() {
30 public void onClick(View arg0) {
31 mplayer.playSong(0);
32 }
33 };
34
35 protected void onPause() {
36 super.onPause();
37 if(mplayer.isPlaying()){
38 mplayer.pauseSong();}
39 };
40
41 protected void onDestroy(){
42 super.onDestroy();
43 mplayer.destroyPlayer();
44 };
45}

1public final class SinletonMusicPlayer {
2
3 private static SinletonMusicPlayer instance;
4 static MediaPlayer player = new MediaPlayer();
5 private static int currentsong;
6 private static int[] song = { R.raw.uffiewordy, R.raw.blendergameengine };
7 static Context mContext;
8
9 private SinletonMusicPlayer() {}
10
11 public synchronized static SinletonMusicPlayer getInstance()
12 {
13 if (instance == null)
14 {
15 instance = new SinletonMusicPlayer();
16 }
17 return instance;
18 }
19
20 public void playSong(int res) {
21 player = MediaPlayer.create(mContext, song[res]);
22 player.start();
23 player.setOnCompletionListener(new OnCompletionListener() {
24 public void onCompletion(MediaPlayer arg0) {
25 player.release();
26 nextSong();
27 }});}
28
29 private void nextSong() {
30 if (++currentsong >= song.length) {
31 currentsong = 0;
32 playSong(currentsong);
33 } else {
34 playSong(currentsong);}}
35
36 public void stopSong() {
37 if (player != null) {
38 player.stop();}}
39
40 public void startSong() {
41 if (player != null) {
42 player.start();}}
43
44 public void setContext(Context context) {
45 mContext = context;}
46
47 public void destroyPlayer() {
48 // TODO Auto-generated method stub
49 if (player != null) {
50 if(player.isPlaying()){
51 player.stop();}
52 player.release();
53 player = null; }}
54
55 public void pauseSong() {
56 // TODO Auto-generated method stub
57 if (player != null) {
58 player.pause();}}
59
60 public boolean isPlaying() {
61 // TODO Auto-generated method stub
62 boolean played = false;
63 if (player != null) {
64 played = player.isPlaying();
65 }return played;}}

1public class zweiteActivity extends Activity{
2 private SinletonMusicPlayer mplayer = SinletonMusicPlayer.getInstance();
3 public void onCreate(Bundle savedInstanceState) {
4 super.onCreate(savedInstanceState);
5 setContentView(R.layout.zweitemain); }
6
7 protected void onResume() {
8 super.onResume();
9 mplayer.startSong(); }
10
11 protected void onPause() {
12 super.onPause();
13 mplayer.pauseSong(); }
14
15 protected void onDestroy(){
16 super.onDestroy();
17 mplayer.destroyPlayer(); };}

Antworten
Markus Gu
  • Forum-Beiträge: 2.644

23.02.2012, 18:21:29 via Website

hmm warum lässt die musik nicht in einem service im hintergrund laufen ? wäre doch einfacher oder ?

swordiApps Blog - Website

Antworten
Markus B.
  • Forum-Beiträge: 636

23.02.2012, 22:37:00 via Website

Hallo,
da würde ich Markus auch zustimmen. Für diese Geschichte biete sein ein local Service einfach super an. Hier z.B. ein Beispiel.

Gruß,
Markus

Antworten
John Do
  • Forum-Beiträge: 36

12.03.2012, 11:03:29 via Website

Moin!
Hat etwas gedauert. Marcus, ich danke dir für das Beispiel. Ich habe jedoch eine andere Lösung gewählt, da ich dein Beispiel nicht verstanden habe. Mein Beispiel verzichtet dabei auf die Verwendung von .aidl. Kann mir jemand sagen, wozu man Android Interface Definition Language benötigt. Ich werde aus der Android Developer Dokumentation nicht schlau. http://developer.android.com/guide/developing/tools/aidl.html

Im folgenden Beispiel könnte ihr die Musik, welche in der ersten Activity automatisch gestartet wird, in der zweiten Activity wieder beenden.
Folgende Seiten waren sehr hilfreich. http://marakana.com/forums/android/examples/60.html, http://developer.android.com/reference/android/app/Service.html#LocalServiceSample, http://saigeethamn.blogspot.com/2009/09/android-developer-tutorial-for_04.html und http://www.vogella.de/articles/AndroidServices/article.html

1public class MyService extends Service{
2
3 private static final String TAG = "MyService";
4 MediaPlayer player = new MediaPlayer();
5 private int currentsong;
6 private int [] song = {R.raw.blendergameengine, R.raw.uffiewordy};
7 private final IBinder mBinder = new MyBinder();
8
9 @Override
10 public IBinder onBind(Intent arg0) {
11 return mBinder;
12 }
13
14 public void onCreate() {
15 super.onCreate();
16 Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
17 Log.d(TAG, "onCreate");
18 currentsong = 0;
19 playSong(currentsong);
20 }
21
22 player = MediaPlayer.create(this, song[res]);
23 player.start();
24
25 player.setOnCompletionListener(new OnCompletionListener() {
26 public void onCompletion(MediaPlayer arg0){
27 player.release();
28 nextSong();
29 }
30 });
31}
32
33private void nextSong(){
34 if (++currentsong >= song.length) {
35 currentsong = 0;
36 playSong(currentsong);
37 } else {
38 playSong(currentsong);
39 }
40}
41
42 public class MyBinder extends Binder {
43 MyService getService() {
44 return MyService.this;
45 }
46 }
47
48 public void pauseSong() {
49 if (player != null) {
50 player.pause();}
51 }
52
53 public void startSong() {
54 if (player != null) {
55 player.start();}
56 }
57
58 @Override
59 public void onDestroy() {
60 super.onDestroy();
61 Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
62 if (player != null) {
63 player.release();
64 player = null;
65 }
66 }
67
68 @Override
69 public void onStart(Intent intent, int startid){
70 Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
71 Log.d(TAG, "onStart");
72 player.start();
73 }
74}

1public class ServiceBindActivity extends Activity {
2 private static final String TAG = "ServicesDemo";
3 private MyService s;
4 Button buttonStart, buttonStop;
5 /** Called when the activity is first created. */
6 @Override
7 public void onCreate(Bundle savedInstanceState) {
8 super.onCreate(savedInstanceState);
9 setContentView(R.layout.main);
10 doBindService();
11 startService(new Intent(this, MyService.class));
12 buttonStart = (Button) findViewById(R.id.buttonStart);
13 buttonStop = (Button) findViewById(R.id.buttonStop);
14 buttonStart.setOnClickListener(Start);
15 buttonStop.setOnClickListener(Ende);
16 }
17
18 private ServiceConnection mConnection = new ServiceConnection() {
19
20 public void onServiceConnected(ComponentName name, IBinder binder) {
21 // TODO Auto-generated method stub
22 s = ((MyService.MyBinder) binder).getService();
23 Toast.makeText(ServiceBindActivity.this, "Connected",
24 Toast.LENGTH_SHORT).show();
25 }
26
27 public void onServiceDisconnected(ComponentName name) {
28 // TODO Auto-generated method stub
29 s = null;
30 }
31 };
32
33 void doBindService() {
34 bindService(new Intent(this, MyService.class), mConnection,
35 Context.BIND_AUTO_CREATE);
36 }
37
38 OnClickListener Start = new OnClickListener() {
39 public void onClick(View arg0) {
40 startActivity(new Intent(ServiceBindActivity.this, zweiteActivity.class));
41 }
42 };
43
44 OnClickListener Ende = new OnClickListener() {
45 public void onClick(View arg0) {
46 }
47 };
48
49 public void onClick(View src) {
50 switch (src.getId()) {
51 case R.id.buttonStart:
52 Log.d(TAG, "onClick: starting srvice");
53 startService(new Intent(this, MyService.class));
54 startActivity(new Intent(this, zweiteActivity.class));
55 break;
56 case R.id.buttonStop:
57 Log.d(TAG, "onClick: stopping srvice");
58 stopService(new Intent(this, MyService.class));
59 break;
60 } }}

1public class zweiteActivity extends Activity{
2 private MyService s;
3 Button buttonStart, buttonStop;
4
5 protected void onCreate(Bundle savedInstanceState) {
6 super.onCreate(savedInstanceState);
7 setContentView(R.layout.main);
8 doBindService();
9 buttonStart = (Button) findViewById(R.id.buttonStart);
10 buttonStop = (Button) findViewById(R.id.buttonStop);
11 buttonStart.setOnClickListener(Start);
12 buttonStop.setOnClickListener(Ende);
13 }
14
15 protected void onResume(){
16 super.onPause();
17 }
18
19 protected void onPause(){
20 super.onResume();
21 }
22
23 private ServiceConnection mConnection = new ServiceConnection() {
24 public void onServiceConnected(ComponentName name, IBinder binder) {
25 // TODO Auto-generated method stub
26 s = ((MyService.MyBinder) binder).getService();
27 Toast.makeText(zweiteActivity.this, "Connected",
28 Toast.LENGTH_SHORT).show();
29 }
30
31 public void onServiceDisconnected(ComponentName name) {
32 // TODO Auto-generated method stub
33 s = null;
34 }
35 };
36
37 OnClickListener Start = new OnClickListener() {
38 public void onClick(View arg0) {
39s.startSong();
40 }};
41
42 OnClickListener Ende = new OnClickListener() {
43 public void onClick(View arg0) {
44 s.pauseSong();}};
45
46 void doBindService() {
47 bindService(new Intent(this, MyService.class), mConnection,
48 Context.BIND_AUTO_CREATE); }
49}

Antworten