I am using Push Notification in my app. i am able to play the default sound for push . Now i want to use some Mp3. I don't where to place mp3 in project and how to use it in activity . please help me .
            Asked
            
        
        
            Active
            
        
            Viewed 2,229 times
        
    2
            
            
        - 
                    and how to use in activity ?? notification.sound = Uri.parse("file:///sdcard/notification/SOUND.mp3"); – Gaurav kumar Nov 26 '13 at 05:36
- 
                    how can i use your answer in my code . i am asking about when i put sound in raw folder then how i use it ? – Gaurav kumar Nov 26 '13 at 05:38
- 
                    Right now, where is the code to play default sound?? replace it with my code – SweetWisher ツ Nov 26 '13 at 05:39
2 Answers
3
            Put the file in raw folder.
If you want to use .ogg file use this :
Thread t = new Thread()
        {
                public void run()
                {
                    MediaPlayer player = null;
                    player = MediaPlayer.create(context,R.raw.push_sound);
                    player.start();
                    try 
                    {
                        Thread.sleep(player.getDuration()+100);
                    } 
                    catch (InterruptedException e) 
                    {
                    }
                }
            }
        };
        t.start();   
EDIT:
Please use below code when you get notification in BroadcastReceiver, then call activity in that activity class.
Use below code so play sound file.
mMediaPlayer = new MediaPlayer();
mMediaPlayer = MediaPlayer.create(this, R.raw.sound1);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setLooping(true);
mMediaPlayer.start();
 
    
    
        SweetWisher ツ
        
- 7,296
- 2
- 30
- 74
- 
                    1
- 
                    1Never ask for upvote.. You might loose reputation.. keep this in your mind :) – SweetWisher ツ Nov 26 '13 at 06:48
- 
                    
- 
                    what i put in this (float leftVolume, float rightVolume). and i also want to control push when phone on silent mode then push sound also be off – Gaurav kumar Nov 28 '13 at 06:52
- 
                    1" i also want to control push when phone on silent mode then push sound also be off" means?? – SweetWisher ツ Nov 28 '13 at 06:55
- 
                    when my phone in silent mode, then i want that push sound also not play – Gaurav kumar Nov 28 '13 at 06:57
- 
                    let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/42097/discussion-between-sweetwisher-and-gaurav-kumar) – SweetWisher ツ Nov 28 '13 at 07:01
0
            
            
        You have to get the help of a IntentReceiver:
public class IntentReceiver extends BroadcastReceiver {
    private static final String logTag = "PushSample";
    public static String APID_UPDATED_ACTION_SUFFIX = ".apid.updated";
    public static String gcmId="";
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(logTag, "Received intent: " + intent.toString());
        String action = intent.getAction();
        if (action.equals(PushManager.ACTION_PUSH_RECEIVED)) {
            int id = intent.getIntExtra(PushManager.EXTRA_NOTIFICATION_ID, 0);
            // Id
            String ap_id = intent.getStringExtra(PushManager.EXTRA_APID);
            System.out.println("IntentReceiver::- ID::-" + ap_id);
            Log.i(logTag,
                    "Received push notification. Alert: "
                            + intent.getStringExtra(PushManager.EXTRA_ALERT)
                            + " [NotificationID=" + id + "]");
            logPushExtras(intent);
        } 
}
PushManager.ACTION_PUSH_RECEIVED gets fired when a push is received. You need declare the IntentReceiver in the manifest.
This is a good tutorial to follow: http://www.vogella.com/articles/AndroidCloudToDeviceMessaging/article.html
 
    
    
        TharakaNirmana
        
- 10,237
- 8
- 50
- 69
