对于用Adobe AIR编写的游戏,当我们在Android上显示本地或推送通知时,我们希望游戏中的声音之一成为通知声音。
目前,我们有以下解决方案,我们对此并不满意,因为它需要额外的Android权限,而我们确实认为这是不必要的。基本上,您传递 Assets 路径,例如位于根“assets”文件夹中的“audio / core / reward.mp3”,然后在Android端,将文件拉出到InputStream中,并将其写入文件在外部存储设备上,Android的MediaPlayer从那里开始播放。
显然,当您在Android上播放文件时,这一定不是AIR所做的,因为您不需要“WRITE_EXTERNAL_STORAGE”来播放声音,因此对于我们来说,当应用程序当前未在积极运行时,会出现推送通知在其中,我将如何播放在AS3方面打包和使用的文件?
推荐的方法是
MediaPlayer m = new MediaPlayer();
AssetFileDescriptor afd = context.getAssets().openFd(soundPath);
m.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
m.prepare();
m.setVolume(1f, 1f);
m.start();
playSound = false;
但这不适用于错误
java.io.FileNotFoundException: This file can not be opened as a file descriptor; it is probably compressed
所以我需要做一个不受欢迎的方法,
public static void HandleMessage(Context context, CharSequence title, CharSequence message, String soundPath)
{
log("HandleMessage (message: '" + message + "', title: '" + title +"', icon: " + Extension.getIcon(context) + ", class: " + Extension.getEntryClass(context));
long when = System.currentTimeMillis();
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(context, Extension.getEntryClass(context));
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
Notification notification = new NotificationCompat.Builder(context)
.setContentText(message)
.setContentIntent(contentIntent)
.setTicker(message)
.setSmallIcon(Extension.getIcon(context))
.setWhen(when)
.setAutoCancel(true)
.setVibrate(new long[] { 1000, 400 })
.build();
Boolean playSound = true;
if(null != soundPath && "" != soundPath)
{
try {
MediaPlayer m = new MediaPlayer();
String newPath = checkWriteFile(context, soundPath);
log(newPath);
if(null != newPath)
{
m.setDataSource(newPath);
m.prepare();
m.setVolume(1f, 1f);
m.start();
playSound = false;
}
} catch (IOException e) {
log(e.getLocalizedMessage());
e.printStackTrace();
}
}
if(playSound)
{
notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
}
nm.notify(NotifId, notification);
NotifId++;
}
public static String checkWriteFile(Context context, String relPath)
{
Boolean exists = false;
String newPath = null;
String fullPath = Environment.getExternalStorageDirectory().getPath() + "/" + context.getPackageName() + "/temp/" + relPath;
int index = fullPath.lastIndexOf("/") + 1;
String file = fullPath.substring(index);
String path = fullPath.substring(0, index);
log(relPath + ", " + fullPath + ", " + (new File(fullPath)).exists());
if (!(new File(fullPath)).exists()) {
try {
byte[] buffer = null;
InputStream fIn = context.getAssets().open(relPath);
log("InputStream: " + fIn);
int size=0;
try {
size = fIn.available();
buffer = new byte[size];
fIn.read(buffer);
fIn.close();
} catch (IOException e) {
e.printStackTrace();
}
exists = (new File(path)).exists();
if (!exists) {
new File(path).mkdirs();
}
FileOutputStream save;
try {
save = new FileOutputStream(path + file);
save.write(buffer);
save.flush();
save.close();
newPath = fullPath;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
catch (Exception e) {
log(e.getLocalizedMessage());
e.printStackTrace();
}
}
else {
newPath = fullPath;
}
return newPath;
}
最佳答案
如果要避免使用android.permission.WRITE_EXTERNAL_STORAGE
,可以将文件提取到内部存储中,而不是外部。
String fullPath = Environment.getExternalStorageDirectory() (...)
可以替换为
String fullPath = context.getFilesDir() (...)