2

我尝试使用 MediaPlayer 类播放设备铃声一次(我必须使用 MediaPlayer,因为我在我的应用程序中将铃声与其他音频源一起使用)所以我尝试以下播放随机铃声但我无法阻止它循环播放,它一直在播放。是否可以使用 MediaPlayer 只播放一次铃声。

谢谢

try {
    RingtoneManager mRing = new RingtoneManager(RingtoneActivity.this);
    int mNumberOfRingtones = mRing.getCursor().getCount();
    Uri mRingToneUri = mRing.getRingtoneUri((int) (Math.random() * mNumberOfRingtones));
    mMediaPlayer = new MediaPlayer();
    mMediaPlayer.setDataSource(RingtoneActivity.this, mRingToneUri);
    mMediaPlayer.prepare();
    mMediaPlayer.setLooping(false);
    mMediaPlayer.start();

} catch (Exception ignore) {
}

更新:

我刚刚发布了一个与此问题相关的新问题,因为我能够找到问题的具体原因。

4

2 回答 2

9

我找到了使用事件的简单解决方案 - 确认答案,因为它解决了您的问题:)

mediaPlayer.setOnSeekCompleteListener(new MediaPlayer.OnSeekCompleteListener() {
                    @Override
                    public void onSeekComplete(MediaPlayer mediaPlayer) {
                        mediaPlayer.stop();
                    }
                });

http://developer.android.com/reference/android/media/MediaPlayer.html

记住release()。

            mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mediaPlayer) {
                    mediaPlayer.release();
                }
            });

您想要使用的完整代码需要将铃声设置为您想要的东西。

        AudioManager audioManager = (AudioManager)getSystemService(AUDIO_SERVICE);
        int volume = audioManager.getStreamVolume(AudioManager.STREAM_NOTIFICATION);
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(view.getContext());
        Uri notificationSoundUri = Uri.parse(prefs.getString("notification_sound", "DEFAULT_SOUND"));

        MediaPlayer mediaPlayer = new MediaPlayer();
        try {
            mediaPlayer.setDataSource(view.getContext(), notificationSoundUri);
        } catch (Exception e1) {
            e1.printStackTrace();
            mediaPlayer.release();
            return;
        }
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                mediaPlayer.release();
            }
        });
        try {
            mediaPlayer.prepare();
        } catch (Exception e1) {
            e1.printStackTrace();
            mediaPlayer.release();
            return;
        }
        mediaPlayer.setOnSeekCompleteListener(new MediaPlayer.OnSeekCompleteListener() {

            @Override
            public void onSeekComplete(MediaPlayer mediaPlayer) {
                // TODO Auto-generated method stub
                mediaPlayer.stop();
                mediaPlayer.start();
            }
        });
        mediaPlayer.setVolume(volume, volume);
        mediaPlayer.start();
于 2013-01-26T20:58:18.173 回答
0

使用通知

   NotificationManager nManager = 
           (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);    
   NotificationCompat.Builder notifBuilder = 
           new NotificationCompat.Builder(context);
   notifBuilder.setSound(sound);
   notification = notifBuilder.build();

   nManager.notify(SOME_INT_CONSTANT, notification);
于 2013-12-03T01:17:41.627 回答