2012-12-26 5 views
20

NotificationCompat.Builder에서 만든 알림에 사운드를 어떻게 추가합니까? res에 raw 폴더를 만들고 거기에 사운드를 추가했습니다. 이제 알림에 어떻게 추가합니까? setSound(Uri soundUri) - 이건 내 알림 코드알림에 소리를 추가하는 방법은 무엇입니까?

int NOTIFY_ID=100; 
    Intent notificationIntent = new Intent(this, Notification.class); 
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT); 

    NotificationCompat.Builder mBuilder = 
      new NotificationCompat.Builder(this) 
      .setContentIntent(pendingIntent) 
      .setSmallIcon(R.drawable.notification) 
      .setContentTitle("Warning") 
      .setContentText("Help!") 

    NotificationManager mgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    mgr.notify(NOTIFY_ID, mBuilder.build()); 
+0

[NotificationCompat.Builder]에 [setSound] (http://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#setSound (android.net.Uri)) 메소드가 있습니다. ] (http://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html). 그게 당신이 찾고있는거야? –

답변

40

내가 여기에 문제를 추측 NotificationCompat.Builder 클래스의 분명한 방법이 같은 Uri와 사운드를 참조하는 방법입니다 해요입니다.

android.resource://[PACKAGE_NAME]/[RESOURCE_ID]

그래서 코드가 그렇게보고 끝낼 수 :

Uri sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notifysnd); 
mBuilder.setSound(sound); 
+0

내 사운드 파일을 포맷해야하는 형식은 무엇입니까? 내 소리 대신 기본 소리가 들려요. – karl

+2

나는 시행 착오와 함께 내 자신의 질문에 대답했다. 1) mp3가 작동하고, wav가 작동하지 않는다. 2) 알림을 작성한 다음 n.defaults & = ~ Notification.DEFAULT_SOUND를 실행하여 기본 사운드를 비활성화해야한다. ; ' – karl

+3

Notification.Builder를 사용하는 경우 * builder.setDefaults (~ Notification.DEFAULT_SOUND); * 트릭을 수행합니다. 팁을위한 @karl에 감사드립니다. – Maragues

15

가 알림으로 사운드를 재생하려면

다음과 같이 당신이 Uri를 만들 필요가 귀하의 raw 리소스에 액세스하려면 :

Notification notification = new Notification(icon, tickerText, when); 

정상적인 알림 절차

는 알림과 기본 사운드를 재생하려면

notification.defaults |= Notification.DEFAULT_SOUND; 

가 알림으로 사용자 정의 사운드를 재생하려면 :

notification.sound = Uri.parse("file:///sdcard/notification/notification.mp3"); 

은 그럼 그냥 통지를 보내도록 알림 관리자를 사용합니다. 이 두 가지 문을 모두 사용하는 경우 응용 프로그램은 기본적으로 기본 소리를 사용합니다.

관련 문제