对于我们用Adobe AIR编写的游戏,当我们在Android上显示本地或推送通知时,我们希望游戏中的一种声音成为通知声音。
目前我们有一个解决方案,我们并不高兴,因为它需要额外的Android权限,我们真的觉得这是不必要的。基本上你传递资产路径,说“audio / core / reward.mp3”,它位于根“assets”文件夹中,然后在Android端,它将该文件拉出到InputStream中,将其写入文件在外部存储上,Android的MediaPlayer从那里播放它。
显然,这不是AIR在Android上播放文件时所做的事情,因为你不需要“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;
}
答案 0 :(得分:1)
如果您想避开android.permission.WRITE_EXTERNAL_STORAGE
,可以将文件提取到内部存储而不是外部存储。
String fullPath = Environment.getExternalStorageDirectory() (...)
可以替换为
String fullPath = context.getFilesDir() (...)