Sound-Wiedergabe beim onCreate einer Activity

  • Antworten:5
Kraeusi
  • Forum-Beiträge: 8

12.07.2012, 23:05:59 via Website

Hallo,

ich bin neu in der Android Programmierung und habe da eine kleine Frage (ich glaube so fängt jeder zweite Thread hier an :grin:).

Ich habe mit folgenden Tutorials angefangen mir einen Alarm zu basteln, der beim Erscheinen einen Alarm abspielen soll. Leider funktioniert dies nicht, dass ein Alarm bei OnCreate wiedergegeben wird. Geht das nicht oder wo liegt der Fehler bei mir?

Mit funktioniert nicht, meine ich, es erscheint kein Fehler, aber der Sound wird auch nicht wiedergegeben. :(

Die Beiden Tutorials:
http://www.droidnova.com/creating-sound-effects-in-android-part-2,695.html
http://android-er.blogspot.co.at/2011/05/using-alarmmanager-to-start-scheduled.html

Mein Code [Auszug aus der Activity]:
1@Override
2 protected void onCreate(Bundle savedInstanceState) {
3 // TODO Auto-generated method stub
4 super.onCreate(savedInstanceState);
5 setContentView(R.layout.layout_scheduledactivity);
6
7 //Create, Initialise and then load the Sound manager
8 SoundManager.getInstance();
9 SoundManager.initSounds(this);
10 SoundManager.loadSounds();
11 SoundManager.playSound(1, 1);

[Auszug aus SoundManager]
1package com.exercise.AndroidScheduledActivity;
2
3import java.util.HashMap;
4
5import android.content.Context;
6import android.media.AudioManager;
7import android.media.SoundPool;
8
9
10public class SoundManager {
11
12 static private SoundManager _instance;
13 private static SoundPool mSoundPool;
14 private static HashMap<Integer, Integer> mSoundPoolMap;
15 private static AudioManager mAudioManager;
16 private static Context mContext;
17
18 private SoundManager()
19 {
20 }
21
22 /**
23 * Requests the instance of the Sound Manager and creates it
24 * if it does not exist.
25 *
26 * @return Returns the single instance of the SoundManager
27 */
28 static synchronized public SoundManager getInstance()
29 {
30 if (_instance == null)
31 _instance = new SoundManager();
32 return _instance;
33 }
34
35 /**
36 * Initialises the storage for the sounds
37 *
38 * @param theContext The Application context
39 */
40 public static void initSounds(Context theContext)
41 {
42 mContext = theContext;
43 mSoundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);
44 mSoundPoolMap = new HashMap<Integer, Integer>();
45 mAudioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
46 }
47
48 /**
49 * Add a new Sound to the SoundPool
50 *
51 * @param Index - The Sound Index for Retrieval
52 * @param SoundID - The Android ID for the Sound asset.
53 */
54 public static void addSound(int Index,int SoundID)
55 {
56 mSoundPoolMap.put(Index, mSoundPool.load(mContext, SoundID, 1));
57 }
58
59 /**
60 * Loads the various sound assets
61 * Currently hardcoded but could easily be changed to be flexible.
62 */
63 public static void loadSounds()
64 {
65 mSoundPoolMap.put(1, mSoundPool.load(mContext, R.raw.starwars, 1));
66 mSoundPoolMap.put(2, mSoundPool.load(mContext, R.raw.terminator, 1));
67 }
68
69 /**
70 * Plays a Sound
71 *
72 * @param index - The Index of the Sound to be played
73 * @param speed - The Speed to play not, not currently used but included for compatibility
74 */
75 public static void playSound(int index,float speed)
76 {
77 float streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
78 streamVolume = streamVolume / mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
79 mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 999, speed);
80 }
81
82 /**
83 * Stop a Sound
84 * @param index - index of the sound to be stopped
85 */
86 public static void stopSound(int index)
87 {
88 mSoundPool.stop(mSoundPoolMap.get(index));
89 }
90
91 public static void cleanup()
92 {
93 mSoundPool.release();
94 mSoundPool = null;
95 mSoundPoolMap.clear();
96 mAudioManager.unloadSoundEffects();
97 _instance = null;
98
99 }
100}

Wäre toll, wenn ihr mir weiterhelfen könntet.

Danke schon im Voraus.


Gruß,
Kraeusi

— geändert am 12.07.2012, 23:07:04

Antworten
Gelöschter Account
  • Forum-Beiträge: 5.136

13.07.2012, 07:57:51 via Website

Lieber Kraeusi,

zunächst herzlich willkommen hier im Forum.

Es wird Dir jeder sicherlich gerne hier helfen bei Deinem Problem, nur hat leider keiner der hier mitlesenden eine Glaskugel in der er lesen kann was an Deinem Code nicht stimmt.

Du musst den Leuten die Dir helfen sollen schon mal sagen welche Fehlermeldungen Dein Logcat ausspuckt. (Eclipse - Logcat ausgabe [Window - Schow View - Others - Logcat)]
Der kleinste Fehler, der in Deinem Programm passiert, wird im Logcat ausgegeben. Diese Zeilen deuten in 99,9% aller Fälle genau auf den Fehler hin, der begangen wurde.

Ohne diese Information wird es extrem schwierig einen Fehler in dem von Dir gezeigten Code Schnipsel zu finden.

Also, mach Deinen Helfern das Leben ein bisschen leichter und dann wird man Dir auch weiter helfen können.

Btw. Es würde sicherlich helfen, wenn Du an der einen oder anderen Stelle eine Logausgabe mit einbauen würdest. Du machst Dir damit in der Entwicklung das Leben wesentlich leichter. Bspw. Log.d("Mein Programm", "at onCreate... "); So kannst Du zum Beispiel leicht erkennen ob die onCreate Methode überhaupt aufgerufen wird.

lg Voss

Antworten
André
  • Forum-Beiträge: 77

13.07.2012, 09:46:53 via Website

Du rufst SoundPool::load() auf, und anschließend SoundPool::play() ohne zu warten, dass der Sound fertig geladen wurde. (Das Laden der Sounds passiert asynchron.) Wenn der abzuspielende Sound noch nicht fertig geladen wurde, macht play() einfach nichts.

Um das zu beheben, kannst du den "on load complete listener" benutzen:

1soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
2 public void onLoadComplete(SoundPool soundPool,int sampleId,int status)
3 {
4 soundPool.play(sampleId,1,1,1,0,1);
5 }
6 });

Es ist übrigens Unsinn, beim play() Aufruf den stream volume zu übergeben - den benutzt Android schon von ganz alleine.

— geändert am 13.07.2012, 09:48:18

Antworten
Kraeusi
  • Forum-Beiträge: 8

13.07.2012, 22:18:25 via Website

Danke an euch zwei, mit euren schnellen antworten. :)

Ich habe es nun über den SoundPool versucht und mich auch über google schlau gemacht, jedoch habe ich es nicht lauffähig bekommen. Entweder er steigt beim onLoad aus oder er macht nichts. Aktuell steigt er beim onLoad aus.

Mein Code ist folgender:
1package com.exercise.AndroidScheduledActivity;
2
3import java.util.Locale;
4import android.app.Activity;
5import android.media.SoundPool;
6import android.media.SoundPool.OnLoadCompleteListener;
7import android.os.Bundle;
8import android.view.View;
9import android.view.View.OnClickListener;
10import android.widget.Button;
11import android.widget.Toast;
12import com.exercise.AndroidScheduledActivity.SoundManager;
13import android.media.AudioManager;
14
15public class MyScheduledActivity extends Activity{
16 private Button mPlaySound1Button;
17 private Button mPlaySound2Button;
18 private SoundPool soundPool;
19 private int soundID;
20
21 @Override
22 protected void onCreate(Bundle savedInstanceState) {
23 // TODO Auto-generated method stub
24 super.onCreate(savedInstanceState);
25 setContentView(R.layout.layout_scheduledactivity);
26
27 //Create, Initialise and then load the Sound manager
28 SoundManager.getInstance();
29 SoundManager.initSounds(this);
30 SoundManager.loadSounds();
31
32 mPlaySound1Button = (Button) this.findViewById(R.id.PlaySound1);
33 mPlaySound1Button.setOnClickListener(new OnClickListener() {
34 public void onClick(View v) {
35 SoundManager.playSound(1, 1);
36 }
37 });
38
39 mPlaySound2Button = (Button) this.findViewById(R.id.PlaySound2);
40 mPlaySound2Button.setOnClickListener(new OnClickListener() {
41 public void onClick(View v) {
42 SoundManager.playSound(2, 1);
43 }
44 });
45
46 //--------------- SoundPool Anfang
47 soundID = soundPool.load(this, R.raw.terminator, 1);
48 soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
49 soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
50 public void onLoadComplete(SoundPool soundPool,int sampleId,int status){
51 soundPool.play(soundID,1,1,1,0,1);
52 }
53 });
54 //---------------- SoundPool Ende
55
56 Button buttonDismiss = (Button)findViewById(R.id.dismiss);
57 buttonDismiss.setOnClickListener(new Button.OnClickListener(){
58
59 //@Override
60 public void onClick(View arg0) {
61 // TODO Auto-generated method stub
62 finish();
63 }});
64 }
65
66 @Override
67 public void onDestroy() {
68 super.onDestroy();
69 SoundManager.cleanup();
70 }
71}

Meine Fehlerlog:
07-13 22:14:41.953: D/CLIPBOARD(32691): Hide Clipboard dialog at Starting input: finished by someone else... !
07-13 22:14:44.848: D/AndroidRuntime(32691): Shutting down VM
07-13 22:14:44.863: W/dalvikvm(32691): threadid=1: thread exiting with uncaught exception (group=0x40c441f8)
07-13 22:14:44.868: E/AndroidRuntime(32691): FATAL EXCEPTION: main
07-13 22:14:44.868: E/AndroidRuntime(32691): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.exercise.AndroidScheduledActivity/com.exercise.AndroidScheduledActivity.MyScheduledActivity}: java.lang.NullPointerException
07-13 22:14:44.868: E/AndroidRuntime(32691): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1968)
07-13 22:14:44.868: E/AndroidRuntime(32691): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993)
07-13 22:14:44.868: E/AndroidRuntime(32691): at android.app.ActivityThread.access$600(ActivityThread.java:127)
07-13 22:14:44.868: E/AndroidRuntime(32691): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159)
07-13 22:14:44.868: E/AndroidRuntime(32691): at android.os.Handler.dispatchMessage(Handler.java:99)
07-13 22:14:44.868: E/AndroidRuntime(32691): at android.os.Looper.loop(Looper.java:137)
07-13 22:14:44.868: E/AndroidRuntime(32691): at android.app.ActivityThread.main(ActivityThread.java:4507)
07-13 22:14:44.868: E/AndroidRuntime(32691): at java.lang.reflect.Method.invokeNative(Native Method)
07-13 22:14:44.868: E/AndroidRuntime(32691): at java.lang.reflect.Method.invoke(Method.java:511)
07-13 22:14:44.868: E/AndroidRuntime(32691): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
07-13 22:14:44.868: E/AndroidRuntime(32691): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
07-13 22:14:44.868: E/AndroidRuntime(32691): at dalvik.system.NativeStart.main(Native Method)
07-13 22:14:44.868: E/AndroidRuntime(32691): Caused by: java.lang.NullPointerException
07-13 22:14:44.868: E/AndroidRuntime(32691): at com.exercise.AndroidScheduledActivity.MyScheduledActivity.onCreate(MyScheduledActivity.java:47)
07-13 22:14:44.868: E/AndroidRuntime(32691): at android.app.Activity.performCreate(Activity.java:4465)
07-13 22:14:44.868: E/AndroidRuntime(32691): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
07-13 22:14:44.868: E/AndroidRuntime(32691): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1932)
07-13 22:14:44.868: E/AndroidRuntime(32691): ... 11 more

Habt ihr vielleicht noch einen kleinen Tipp für mich? :unsure:

Gruß,
Kraeusi

Antworten
André
  • Forum-Beiträge: 77

14.07.2012, 02:33:01 via Website

1soundID = soundPool.load(this, R.raw.terminator, 1);
2soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);

Die Reihenfolge kommt dir nicht irgendwie komisch vor? ;)

Antworten
Kraeusi
  • Forum-Beiträge: 8

14.07.2012, 06:33:58 via Website

ahhhh ich dreh durch :(

hatte zuvor die soundID am schluss und auch mal beide seiten einfach nicht benutzt. nun gehts. Danke.:grin:

1soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
2 soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
3 public void onLoadComplete(SoundPool soundPool,int sampleId,int status){
4 soundPool.play(soundID,1,1,1,0,1);
5 }
6 });
7 soundID = soundPool.load(this, R.raw.terminator, 1);

Danke noch mal für eure hilfe.

Gruß,
Kraeusi

Antworten