添加自定义应用内铃声
This commit is contained in:
parent
bff40d2a64
commit
986922c1fa
@ -1,8 +1,8 @@
|
|||||||
#Created by .winboll/winboll_app_build.gradle
|
#Created by .winboll/winboll_app_build.gradle
|
||||||
#Tue May 06 18:34:42 GMT 2025
|
#Tue May 06 18:56:20 GMT 2025
|
||||||
stageCount=7
|
stageCount=7
|
||||||
libraryProject=
|
libraryProject=
|
||||||
baseVersion=15.0
|
baseVersion=15.0
|
||||||
publishVersion=15.0.6
|
publishVersion=15.0.6
|
||||||
buildCount=7
|
buildCount=9
|
||||||
baseBetaVersion=15.0.7
|
baseBetaVersion=15.0.7
|
||||||
|
@ -0,0 +1,71 @@
|
|||||||
|
package cc.winboll.studio.timestamp.utils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author ZhanGSKen
|
||||||
|
* @Date 2025/05/07 02:38
|
||||||
|
* @Describe AudioPlayer
|
||||||
|
*/
|
||||||
|
import android.content.Context;
|
||||||
|
import android.media.MediaPlayer;
|
||||||
|
import android.net.Uri;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
public class AudioPlayerMP3Util {
|
||||||
|
|
||||||
|
public static final String TAG = "AudioPlayer";
|
||||||
|
|
||||||
|
private static MediaPlayer mediaPlayer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 播放指定的 MP3 文件
|
||||||
|
*
|
||||||
|
* @param context 上下文
|
||||||
|
* @param mp3FilePath MP3 文件的路径,例如:"/storage/emulated/0/Music/song.mp3"
|
||||||
|
*/
|
||||||
|
public static void playMp3(Context context, String mp3FilePath) {
|
||||||
|
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
|
||||||
|
mediaPlayer.stop();
|
||||||
|
mediaPlayer.release();
|
||||||
|
mediaPlayer = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
mediaPlayer = new MediaPlayer();
|
||||||
|
Uri uri = Uri.parse(mp3FilePath);
|
||||||
|
mediaPlayer.setDataSource(context, uri);
|
||||||
|
mediaPlayer.prepare();
|
||||||
|
mediaPlayer.start();
|
||||||
|
|
||||||
|
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
|
||||||
|
@Override
|
||||||
|
public void onCompletion(MediaPlayer mp) {
|
||||||
|
releaseMediaPlayer();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
|
||||||
|
@Override
|
||||||
|
public boolean onError(MediaPlayer mp, int what, int extra) {
|
||||||
|
Log.e("AudioPlayer", "播放音频时出错: what=" + what + ", extra=" + extra);
|
||||||
|
releaseMediaPlayer();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
releaseMediaPlayer();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 释放 MediaPlayer 资源
|
||||||
|
*/
|
||||||
|
private static void releaseMediaPlayer() {
|
||||||
|
if (mediaPlayer != null) {
|
||||||
|
mediaPlayer.stop();
|
||||||
|
mediaPlayer.release();
|
||||||
|
mediaPlayer = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -10,7 +10,7 @@ import android.media.MediaPlayer;
|
|||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
public class AudioPlayerUtil {
|
public class AudioPlayerUriUtil {
|
||||||
|
|
||||||
public static final String TAG = "AudioPlayerUtil";
|
public static final String TAG = "AudioPlayerUtil";
|
||||||
|
|
@ -128,8 +128,14 @@ public class NotificationHelper {
|
|||||||
|
|
||||||
service.startForeground(ID_MSG_SERVICE, mForegroundNotification);
|
service.startForeground(ID_MSG_SERVICE, mForegroundNotification);
|
||||||
|
|
||||||
|
// 播放默认短信铃声
|
||||||
Uri defaultSmsRingtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
|
Uri defaultSmsRingtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
|
||||||
AudioPlayerUtil.playAudio(service, defaultSmsRingtoneUri);
|
AudioPlayerUriUtil.playAudio(service, defaultSmsRingtoneUri);
|
||||||
|
|
||||||
|
// 播放应用铃声
|
||||||
|
// 获取MP3文件的Uri
|
||||||
|
Uri soundUri = Uri.parse("android.resource://" + service.getPackageName() + "/" + R.raw.diweiyi);
|
||||||
|
AudioPlayerUriUtil.playAudio(service, soundUri);
|
||||||
}
|
}
|
||||||
|
|
||||||
// public void sendSMSNotification(Context context, MessageNotificationBean messageNotificationBean) {
|
// public void sendSMSNotification(Context context, MessageNotificationBean messageNotificationBean) {
|
||||||
|
BIN
timestamp/src/main/res/raw/diweiyi.mp3
Normal file
BIN
timestamp/src/main/res/raw/diweiyi.mp3
Normal file
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user