【问题标题】:Push Notification using external sound in Android在 Android 中使用外部声音推送通知
【发布时间】:2013-11-26 05:26:56
【问题描述】:

我在我的应用程序中使用推送通知。我可以播放 push 的默认声音。现在我想使用一些 Mp3。我不知道在 project 中放置 mp3 的位置以及如何在 activity 中使用它。请帮帮我。

【问题讨论】:

  • 以及如何在活动中使用?? notification.sound = Uri.parse("file:///sdcard/notification/SOUND.mp3");
  • 如何在我的代码中使用您的答案。我在问我什么时候把声音放在原始文件夹中,然后我如何使用它?
  • 现在,播放默认声音的代码在哪里?用我的代码替换它

标签: android audio push-notification


【解决方案1】:

将文件放在原始文件夹中。

如果您想使用 .ogg 文件,请使用:

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();   

编辑

当您在 BroadcastReceiver 中收到通知时,请使用以下代码,然后调用该活动类中的活动。

使用下面的代码播放声音文件。

mMediaPlayer = new MediaPlayer();
mMediaPlayer = MediaPlayer.create(this, R.raw.sound1);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setLooping(true);
mMediaPlayer.start();

【讨论】:

  • 当然..希望对你有帮助:)
  • 永远不要要求支持..你可能会失去声誉..记住这一点:)
  • 我如何设置这个方法的音量?
  • 我放了什么(float leftVolume,float rightVolume)。我还想在手机处于静音模式时控制推送,然后推送声音也关闭
  • "我也想在手机静音模式下控制推送,然后推送声音也关闭"是什么意思??
【解决方案2】:

你必须得到 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 在收到推送时被触发。您需要在清单中声明 IntentReceiver。

这是一个很好的教程:http://www.vogella.com/articles/AndroidCloudToDeviceMessaging/article.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多