2014-11-09 16 views
5

Sto utilizzando il seguente codice per riprodurre suoni nella mia app. Tutto ha funzionato bene prima di ICS. Ma su ICS e versioni più recenti non si sente alcun suono, anche se non appare alcun errore.Suono non riproducibile nella versione Android superiore a 4.0

MODIFICA: Nota: il codice seguente è attivato da un ricevitore di serie. Il ricevitore BroadCast richiama un task asincrono. Nel metodo post process di asycn viene chiamato il metodo seguente.

Questo errore si verifica solo su specifici modelli di dispositivi mobili (ad esempio, nexus) e su alcuni modelli gli utenti sono in grado di riprodurre suoni forniti con sistemi operativi ma non i propri suoni che collocano nella cartella rigtones.

Io non sono in grado ottenere che cosa è il problematiche come avviene su specifici portatile

Che cosa potrebbe l'errore eventualmente essere?

public static void playSound(final Context context, final int volume, 
      Uri uri, final int stream, int maxTime, int tickTime) { 
     //stopPlaying(); 
     /* 
     if (stream < 0 || stream > 100) { 
      throw new IllegalArgumentException(
        "volume must be between 0 and 100 .Current volume " 
          + volume); 
     }*/ 

     final AudioManager mAudioManager = (AudioManager) context 
       .getSystemService(Context.AUDIO_SERVICE); 

     int deviceLocalVolume = getDeviceVolume(volume, 
       mAudioManager.getStreamMaxVolume(stream)); 

     Log.d(TAG, 
       "device max volume = " 
         + mAudioManager.getStreamMaxVolume(stream) 
         + " for streamType " + stream); 
     Log.d(TAG, "playing sound " + uri.toString() 
       + " with device local volume " + deviceLocalVolume); 

     final int oldVolume = mAudioManager.getStreamVolume(stream); 

     // set the volume to what we want it to be. In this case it's max volume 
     // for the alarm stream. 
     Log.d(Constants.APP_TAG, "setting device local volume to " + deviceLocalVolume); 
     mAudioManager.setStreamVolume(stream, deviceLocalVolume, 
       AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE); 

     final MediaPlayer mediaPlayer = new MediaPlayer(); 
     golbalMMediaPlayer = mediaPlayer; 

     try { 
      final OnPreparedListener OnPreparedListener = new OnPreparedListener() { 

       @Override 
       public void onPrepared(final MediaPlayer mp) { 
        Log.d(TAG, "onMediaPlayercompletion listener"); 
        mp.start(); 
        countDownTimer.start(); 
       } 
      }; 

      mediaPlayer.setDataSource(context.getApplicationContext(), uri); 
      mediaPlayer.setAudioStreamType(stream); 
      mediaPlayer.setLooping(false); 
      mediaPlayer.setOnPreparedListener(OnPreparedListener); 
      mediaPlayer.setOnCompletionListener(new OnCompletionListener() { 

       @Override 
       public void onCompletion(MediaPlayer mp) { 
        Log.d(Constants.APP_TAG, "Entered onCompletion listener of mediaplayer"); 
        mAudioManager.setStreamVolume(stream, oldVolume, 
          AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE); 
        try{ 
        if(mediaPlayer != null && mediaPlayer.isPlaying()){ 
         mediaPlayer.release(); 
        } 
        }catch(Exception ex){ 
         Log.e(Constants.APP_TAG, "error on oncompletion listener" ,ex); 
        } 
       } 

      }); 

      CountDownTimer timer = new CountDownTimer(maxTime*1000, tickTime*1000) { 

       @Override 
       public void onTick(long millisUntilFinished) { 
        Log.d(TAG, "tick while playing sound "); 
       } 

       @Override 
       public void onFinish() { 
        Log.d(TAG, "timer finished"); 
        stopPlaying(); 
       } 
      }; 

      countDownTimer = timer; 

      mediaPlayer.prepareAsync(); 

     } catch (Exception e) { 
      Log.e(TAG, "problem while playing sound", e); 
     } finally { 

     } 
    } 

TRONCHI

:07-01 00:00:00.030: D/beephourly(9500): device max volume = 7 for streamType 5 
07-01 00:00:00.030: D/beephourly(9500): playing sound content://media/internal/audio/media/166 with device local volume 7 
07-01 00:00:00.030: D/beephourly(9500): setting device local volume to 7 
07-01 00:00:00.080: D/beephourly(9500): vibrating with pattern = [[email protected] 
07-01 00:00:00.090: D/beephourly(9500): will show normal notification 
07-01 00:00:00.100: D/beephourly(9500): notification is enabled 
07-01 00:00:00.100: D/usersettings(9500): hr = 0 
07-01 00:00:00.110: D/beephourly(9500): onMediaPlayercompletion listener 
07-01 00:00:00.451: D/beephourly(9500): tick while playing sound 
07-01 00:00:20.460: D/beephourly(9500): timer finished 
07-01 00:00:20.460: D/beephourly(9500): got request to stop playing 
07-01 00:00:20.460: D/beephourly(9500): cancelling countdowntimer 
07-01 00:00:20.460: D/beephourly(9500): releasing mediaplayer now 

Questa domanda è una domanda di follow-up a mettere in discussione che ho chiesto in precedenza: sound not playing in android > icecream sandwich

+0

Sei in grado di identificare un suono che fa giocare e uno che non e fammi sapere il bitrate e il formato dell'audio che stai cercando di riprodurre e su quale dispositivo? –

+0

uno dei formati che non possono essere riprodotti è mp3 – user93796

+0

Non c'è nulla di evidentemente sbagliato nel codice: un'app di esempio può essere riprodotta nello stream NOTIFICATION sul mio Nexus. Ti suggerisco di aggiungere un listener di errori e di rimuovere il filtro del log che potresti avere - i log interessanti verranno da MediaPlayer e NuPlayer. – rds

risposta

2

si dovrebbe essere la richiesta di messa a fuoco audio, anche per le notifiche. Nel tuo caso sarebbe simile a questa:

AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); 

//request a transient lock on the notification stream 
int result = audioManager.requestAudioFocus(this, AudioManager.STREAM_NOTIFICATION, 
    AudioManager.AUDIOFOCUS_GAIN_TRANSIENT); 

if (result != AudioManager.AUDIOFOCUS_REQUEST_GRANTED) { 
    // could not get audio focus. 
} 

Ho il sospetto che ci sia qualche altra applicazione (s) in esecuzione sui dispositivi che si stanno dando problemi che le richiedono attenzione audio. Se altre app richiedono il focus audio e non lo fai, è possibile che l'app non venga riprodotta nello stream finale. Richiedere la messa a fuoco autonomamente garantirà che nessun altro processo interferisca con il suono della notifica. Sebbene questo sia stato introdotto qualche tempo fa, le versioni più recenti di Android sono molto più sensibili alle app che usano questo meccanismo, specialmente dallo sfondo.

Per essere corretta, quando hai finito con la notifica, è possibile rilasciare il focus:

audioManager.abandonAudioFocus(this); 
Problemi correlati