Move files from /raw to /res, fix for IllegalState on MediaPlayer once released.

This commit is contained in:
downthecrop 2022-02-15 05:51:36 -08:00
parent 09484d0f24
commit 0b21c2e1d7
7 changed files with 39 additions and 51 deletions

View file

@ -29,7 +29,7 @@ public class SoundService extends Service {
public static void setMusicVolume(float v) {
musicVolume = v;
if(musicPlayer.isPlaying()){
if(isPlayingMusic()){
musicPlayer.setVolume(v,v);
}
}
@ -47,11 +47,23 @@ public class SoundService extends Service {
}
public static boolean isPlayingMusic(){
return musicPlayer.isPlaying();
try{
return musicPlayer.isPlaying();
} catch (IllegalStateException e){
System.out.println("musicPlayer was released. Resetting.");
}
musicPlayer = new MediaPlayer();
return false;
}
public static boolean isPlayingSFX(){
return sfxPlayer.isPlaying();
try{
return sfxPlayer.isPlaying();
} catch (IllegalStateException e){
System.out.println("musicPlayer was released. Resetting.");
}
sfxPlayer = new MediaPlayer();
return false;
}
public static void setSFXTrack(int t) {
@ -61,7 +73,7 @@ public class SoundService extends Service {
public static void setSFXVolume(float v) {
sfxVolume = v;
if(sfxPlayer.isPlaying()){
if(isPlayingSFX()){
sfxPlayer.setVolume(v,v);
}
}

View file

@ -1,5 +1,7 @@
package net.kdt.pojavlaunch;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import net.kdt.SoundService;
@ -35,9 +37,7 @@ public class JAudioManager {
}
public static void muteSound(){
if (SoundService.isPlayingMusic())
SoundService.setMusicVolume(0);
if (SoundService.isPlayingSFX())
SoundService.setSFXVolume(0);
}

View file

@ -231,12 +231,6 @@ public class JavaGUILauncherActivity extends BaseActivity implements View.OnTou
placeMouseAt(CallbackBridge.physicalWidth / 2f, CallbackBridge.physicalHeight / 2f);
Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.miniclient);
final File miniclient = new File(getCacheDir(), getFileName(this, uri));
final File config = new File(getFilesDir(), "config.json");
final String javaArgs = getIntent().getExtras().getString("javaArgs");
mTextureView = findViewById(R.id.installmod_surfaceview);
mTextureView.setOnTouchListener((v, event) -> {
scaleGestureDetector.onTouchEvent(event);
@ -258,6 +252,11 @@ public class JavaGUILauncherActivity extends BaseActivity implements View.OnTou
}
return true;
});
final File miniclient = new File(Tools.DIR_DATA, "miniclient.jar");
final File config = new File(Tools.DIR_DATA, "config.json");
final String javaArgs = getIntent().getExtras().getString("javaArgs");
new Thread(() -> {
try {
launchJavaRuntime(miniclient, javaArgs, config);
@ -270,12 +269,12 @@ public class JavaGUILauncherActivity extends BaseActivity implements View.OnTou
}
// Start the audio service is it's not already running, otherwise it crashes.
if(!isMyServiceRunning()){
if(!isSoundServiceRunning()){
JAudioManager.init();
}
}
private boolean isMyServiceRunning() {
private boolean isSoundServiceRunning() {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (SoundService.class.getName().equals(service.service.getClassName())) {
@ -414,16 +413,13 @@ public class JavaGUILauncherActivity extends BaseActivity implements View.OnTou
public void onResume() {
super.onResume();
isFocused = true;
JAudioManager.resumeSound();
if(isSoundServiceRunning())
JAudioManager.resumeSound();
final int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
final View decorView = getWindow().getDecorView();
decorView.setSystemUiVisibility(uiOptions);
}
public static boolean getFocusState(){
return isFocused;
}
float[] initScaleFactors(){
return initScaleFactors(true);
}

View file

@ -143,21 +143,13 @@ public class PojavLoginActivity extends BaseActivity {
}
private void uiInit() {
setContentView(R.layout.launcher_main_v4);
Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.miniclient);
final File miniclient = new File(Tools.DIR_DATA, "miniclient.jar");
Thread t = new Thread(()->{
try {
final File miniclient = new File(getCacheDir(), getFileName(this, uri));
FileOutputStream fos = new FileOutputStream(miniclient);
IOUtils.copy(getContentResolver().openInputStream(uri), fos);
fos.close();
PojavLoginActivity.this.runOnUiThread(() -> {
Intent intent = new Intent(PojavLoginActivity.this, JavaGUILauncherActivity.class);
intent.putExtra("miniclient", miniclient);
startActivity(intent);
});
}catch(IOException e) {
//Tools.showError(PojavLoginActivity.this,e);
}
PojavLoginActivity.this.runOnUiThread(() -> {
Intent intent = new Intent(PojavLoginActivity.this, JavaGUILauncherActivity.class);
intent.putExtra("miniclient", miniclient);
startActivity(intent);
});
});
t.start();
}
@ -247,6 +239,10 @@ public class PojavLoginActivity extends BaseActivity {
(resid, vararg) -> runOnUiThread(()->{if(startupTextView!=null)startupTextView.setText(getString(resid,vararg));}));
MultiRTUtils.postPrepare(PojavLoginActivity.this,"Internal");
Tools.copyAssetFile(this,"miniclient.jar",Tools.DIR_DATA, true);
Tools.copyAssetFile(this,"config.json",Tools.DIR_DATA, true);
// Extract predumped sounds
try{
// Unpack Music
@ -270,20 +266,6 @@ public class PojavLoginActivity extends BaseActivity {
// Copy config.json to writable storage
// https://stackoverflow.com/questions/38590996/copy-xml-from-raw-folder-to-internal-storage-and-use-it-in-android
File file = new File(getFilesDir(), "raw/config.json");
try {
Context context = getApplicationContext();
InputStream inputStream = context.getResources().openRawResource(R.raw.config);
FileOutputStream fileOutputStream = new FileOutputStream(file);
byte buf[]=new byte[1024];
int len;
while((len=inputStream.read(buf))>0) {
fileOutputStream.write(buf,0,len);
}
fileOutputStream.close();
inputStream.close();
System.out.println("Write to Local");
} catch (IOException e1) {}
return true;
}catch (IOException e) {
Log.e("JREAuto", "Internal JRE unpack failed", e);

View file

@ -52,7 +52,7 @@ public class SettingsMenu extends Activity {
if (requestCode == FILE_SELECT_CODE) {
if (resultCode == RESULT_OK) {
Uri uri = data.getData();
File config = new File(getFilesDir(), "raw/config.json");
File config = new File(Tools.DIR_DATA, "config.json");
try {
Log.d("TAG", "Starting copy: " + uri.getPath());
InputStream inputStream = getContentResolver().openInputStream(uri);
@ -64,8 +64,6 @@ public class SettingsMenu extends Activity {
}
fileOutputStream.close();
inputStream.close();
Toast.makeText(this, "Config loaded. Please restart the app.",
Toast.LENGTH_SHORT).show();
} catch (IOException e1) {
Log.d("error", "Error with file " + e1);
}
@ -94,7 +92,7 @@ public class SettingsMenu extends Activity {
loadConfig.setOnTouchListener((v, event) -> {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
showFileChooser();
return true; // if you want to handle the touch event
return true;
}
return false;
});