开发者

Android MediaPlayer not playing sound on some platforms

开发者 https://www.devze.com 2023-02-07 06:37 出处:网络
I\'m currently developing an Android application that plays a sound to the user at various intervals. I have working code (included below) which functions exactly as expected on my Hero (running 2.2)

I'm currently developing an Android application that plays a sound to the user at various intervals. I have working code (included below) which functions exactly as expected on my Hero (running 2.2) and on the 1.6 emulator. However, it doesn't work on my friends Xperia x8 -- no sound is played. He has a notification tone set, which plays fine when he receives a text and so on.

private void prepareAudio(){
    alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
    mp = new MediaPlayer();
    if(alert != null){
        try {
            mp.setDataSource(ctx, alert);
        } catch (IllegalArgumentException e) {
            Log.i("Prepare Audio", e.getMessage());
        } catch (SecurityException e) {
            Log.i("Prepare Audio", e.getMessage());
        } catch (IllegalStateException e) {
            Log.i("Prepare Audio", e.getMessage());
        } catch (IOException e) {
            Log.i("Prepare Audio", e.getMessage());
        }
    }
    am = (AudioManager)ctx.getSystemService(Context.AUDIO_SERVICE);
}

private void notifyUser(){
    if(alert != null){
        if (am.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
            mp.setAudioStreamType(AudioManager.STREAM_ALARM);
            mp.setLooping(false);
            try {
                mp.prepare();
            } catch (IllegalStateException e) {
                Log.i("Notify User", e.getMessage());
            } catch (IOException e) {
                Log.i("Notify User", e.getMessage());
            }
            mp.start();
        } else {
            Log.i("Notify User", "Alarm volume zero");
        }
    } else {
        Log.i("Notify User", "Uri is null");
    }
    long[] pattern = {0,200,300,600}; 
    v.vibrate(pattern, -1);
}

PrepareAudio is called on initialising the class, and notifyUser every time we want to play the sound. The phone always vibrat开发者_如何学Pythones when it should, so notifyUser is definitely being called.

The version my friend has installed uses e.printStackTrace rather than Log.i, so there's nothing being spat out in logcat. I'm going to try and get hold of his phone to update it to a version with Log.i, but in the meantime is there anything obviously wrong with the code that could cause such an intermittent problem?

Thanks,


So it turned out that updating my exception handling as listed in the original post appears to have fixed the problem.

If you're using the above code for anything, it's worth noting that sometimes e.getMessage() returns null, so rather than

Log.i("Notify User", e.getMessage());

use

Log.i("Notify User", e.getMessage() + "");

To avoid any nasty FCs when an exception is thrown.

Sorry for the incidentally unanswerable question!


Not sure, but older versions of Android had problems playing WAV files. Are these WAV files or MP3 files that you're trying to play?

0

精彩评论

暂无评论...
验证码 换一张
取 消