2013-01-03 3 views
2

Android에서 새로 생겼고 알림이 표시된 상태에서 사운드를 재생하려고했으나 시뮬레이터에서 작동하지 않습니다. 도와주세요.안드로이드에서 알림 소리가 들리는 이유는 무엇입니까?

public class OneShotAlarm extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     Toast.makeText(context, R.string.one_shot_received, Toast.LENGTH_SHORT).show(); 

     buildNotification(context); 

    } 


    @SuppressLint("NewApi") 
    private void buildNotification(Context context){ 
      NotificationManager notificationManager 
      = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); 
      Notification.Builder builder = new Notification.Builder(context); 

      Intent intent = new Intent(context, MainActivity.class); 
      PendingIntent pendingIntent 
      = PendingIntent.getActivity(context, 0, intent, 0); 

      builder 
      .setSmallIcon(R.drawable.ic_launcher) 
      .setContentTitle("ContentTitle") 
      .setContentText("ContentText") 
      .setContentInfo("ContentInfo") 
      .setTicker("Ticker") 
      .setLights(0xFFFF0000, 500, 500) //setLights (int argb, int onMs, int offMs) 
      .setContentIntent(pendingIntent) 
      .setSound(Uri.parse("android.resource://com.vatshal.VSAlarm/" + R.raw.satinder)) 
      .setAutoCancel(true); 

      Notification notification = builder.build(); 
      //notification.sound = Uri.parse("android.resource://com.vatshal.VSAlarm/" + R.raw.satinder); 
      //notification.defaults |= Notification.DEFAULT_SOUND; 

      notificationManager.notify(R.drawable.notification_warning, notification); 
     } 
} 
+0

시뮬레이터에서 안드로이드 알림 소리 작동하지 ..하지만 Mobiles에서 자동으로 작동합니다 .. 소리를 얻을 것이다 그것은 변화에 따라 달라집니다. – itsrajesh4uguys

+0

감사합니다 Rajesh, 소리에 대한 경로를 설정했습니다 : - .setSound (Uri.parse ("android.resource : //com.vatshal.VSAlarm/"+ R.raw.satinder)) 괜찮습니까 ?? – VATSHAL

+0

다음 줄을 사용하면 모바일 알림에서 기본 알림 소리가납니다 .defaults | = Notification.DEFAULT_SOUND; – itsrajesh4uguys

답변

2

당신이 당신의 자원

try { 
     MediaPlayer mediaPlayer = MediaPlayer.create(context,     R.raw.sound); 
     mediaPlayer.start(); 
    } catch (Exception ex) { 

    } 
+0

빠른 응답을 주셔서 감사합니다. 이전 예에서는 mediaPlayer를 사용했지만이 경우 여러 사운드 문제가 있습니다. 예를 들어 반복 알람을 사용하고 MediaPlayer를 사용하면 다음 사운드가 현재 재생중인 사운드와 동시에 재생됩니다. – VATSHAL

0
내가

.setSound (Uri.parse을 사용

에 소리를 가지고 있는지 ("android.resource합니다 //com.vatshal.VSAlarm/를 "+ R.raw.satinder))

"satinder "라는 이름의"raw "폴더 안에 오디오 경로를 설정하는 올바른 구문은 무엇입니까 ??

관련 문제