Kraeusi
- Forum-Beiträge: 8
12.07.2012, 23:05:59 via Website
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 ).
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]:
[Auszug aus SoundManager]
Wäre toll, wenn ihr mir weiterhelfen könntet.
Danke schon im Voraus.
Gruß,
Kraeusi
ich bin neu in der Android Programmierung und habe da eine kleine Frage (ich glaube so fängt jeder zweite Thread hier an ).
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);
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}
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
Empfohlener redaktioneller Inhalt
Mit Deiner Zustimmung wird hier ein externer Inhalt geladen.
Mit Klick auf den oben stehenden Button erklärst Du Dich damit einverstanden, dass Dir externe Inhalte angezeigt werden dürfen. Dabei können personenbezogene Daten an Drittanbieter übermittelt werden. Mehr Infos dazu findest Du in unserer Datenschutzerklärung.