android mediaplayer - 播放铃声一次

时间:2012-10-11 05:13:52

标签: android android-mediaplayer ringtone

我尝试使用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) {
}

更新

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

2 个答案:

答案 0 :(得分:9)

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

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

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

记得发布()。

            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();

答案 1 :(得分: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);