I am trying to access the methods of the PlaybackParams class in my code but it gives me this error:
Call requires API level 23 (current min is
15): android.media.PlaybackParams#PlaybackParams
The song is playing (not smoothly it kind buffers like in youtube videos) but the set speed isn't increasing the tempo of the song.
Also, the seekTo() methods works properly but the setSpeed() method doesn't.
I've been trying to figure out the bug in the code but haven't found my way through yet.
Also, if I delete the whole implementation and the access of PlaybackParams' methods then only the song plays otherwise (as with the provided code) it throws many (-38,0) errors and this error also:
Attempt to perform seekTo in wrong state: mPlayer=0xad7a2460, mCurrentState=0
I know state =0 error means it's trying to start the song before it's loaded but I don't know how to make everything fit in place properly.
Here's my code:
MediaPlayer mySong;
PlaybackParams params = new PlaybackParams();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mySong = MediaPlayer.create(this, R.raw.fixyou);
}
public void musicPlayer (View view) {
mySong.seekTo(20000);
params.setSpeed(0.75f);
mySong.setPlaybackParams(params);
mySong.start();
}
Run the program on an emulator or a real device with API 23 because PlaybackParams class was newly added for API 23 and doesn't exist for lower API's.
Related
I have a Unity Scene built with Cardboard SDK and exported as a library for Android. The library is used to play videos in cardboard mode on the android app. it's not the whole app, but a part in it. The rest of the android app is built with Kotlin and Java.
I have implemented that and all the functions work as expected, but, exiting the scene crashes the android.
We tried various ways to clear player prefs and even clear memory before closing the scene. But on android it always crashes. I have two android phones with android 9 and 10 for testing.
In the android app, I have made it such that as soon as the app crashes, I try to recover. My crash is that some lateinit var variables are destroyed. Their value becomes null and recovering the previous activity crashes it. So right after I exit the unity scene, I load the dereferenced variables back into memory and everything works again.
Note: I have tried using Application.Quit(); in unity, but it just closes the whole app. On the other hand, I only want to close the running scene
In unity [I call a function goBack in android part to close the app]:
public void GoToHome()
{
Pause();
Stop();
Resources.UnloadUnusedAssets();
PlayerPrefs.DeleteAll();
AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
jo.Call("goBack");
}
In App:
public void goBack()
{
UnityPlayer.currentActivity.finish();
finish();
loadDerereferencedVars();
}
This goes perfectly on android 9. On the other phone with android 10, after I close the scene, the app continues to function, but, there comes a message
When I click close app, the app continues to work.
I have checked the logs and there is a null pointer dereference cause for the crash in Unity Main >...
If you'd like to see the Unity Crash Log from LogCat in Android Studio
So, since the app is still running, I thought, it would be better to just hide the crash report and just let the user not know about this crash, but still report it.
I tried enclosing my app in Application and added a method to catch uncaughtException.
here is my application class:
public class MyApp extends Application {
private static final String TAG = "MyAPP";
#Override
public void onCreate() {
super.onCreate();
Thread.setDefaultUncaughtExceptionHandler(
new Thread.UncaughtExceptionHandler() {
#Override
public void uncaughtException (Thread thread, Throwable e) {
handleUncaughtException (thread, e);
}
});
}
/**
* Handles Uncaught Exceptions
*/
private void handleUncaughtException (Thread thread, Throwable e) {
// The following shows what I'd like, though it won't work like this.
Toast.makeText(getApplicationContext(), "Looks like I am having a bad day!", Toast.LENGTH_LONG).show();
Log.e("UncaughtException", "I found an exception!");
// Add some code logic if needed based on your requirement
}
}
Again, this works perfectly in Android 9 and I also got the error reported. However in the phone with android 10, I just get the crash report like the image above and no error is reported.
I want to know why the crash handling is not working and how can I fix it?
I would not finish the Activity you came from, instead just open a new intent (on UnityActivity). When you end this intent the app will come back to the last active Activity.
I will give you my script as an example:
public void sendJobToUnity(String fileName, boolean isNewJob){
//creates a new job. It exists inside the JobSelector Activity
isUnityLoaded = true;
//this is what you are looking for part1
Intent i = new Intent(JobSelector.this, MainUnityActivity.class); //same as (CurrentActivity.this, UnityActivity.this)
//those are how I send some data across the app. just ignore it
//i.putExtra("jobName", fileName);
//i.putExtra("isNewJob",isNewJob);
//i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivityForResult(i, 1); //this is what you are looking for part2
}
For closing it, in the MainUnityActivity Activity I have an override that Unity sends to Android in order to Unload the activity (not quit it completely cause you cannot load it again if you do it) like this:
#Override
protected void receiveJobAndUnloadUnity(String data){
saveCurrentJob(data); //saves the job it receives from Unity
mUnityPlayer.unload(); //this is what you are looking for part3
}
If you want to unload Unity from android you can put "mUnityPlayer.unload();" wherever you want, provided you have started the Activity the way I've shown you.
Note that "mUnityPlayer" is a default Unity variable and cannot be renamed
I would like to know how can I force volume buttons to only control media while in my application.
I know this is an android specific question and ios does this by default.
There are two related questions giving android solutions:
Volume Control in android application
How can I manage audio volumes sanely in my Android app?
They both suggest adding setVolumeControlStream(AudioManager.STREAM_MUSIC); to android but I'm a react-native developer and not familiar with java.
There is also this react-native related question:
How to control media volume?
Which gives me no idea on where onCreate() is.
Any Specific suggestions on where to add this line in java part of react-native?
So I did some research and thanks to #VolkanSahin45 for his comment, I figured it out.
Adding setVolumeControlStream(AudioManager.STREAM_MUSIC); was right.
What I had to do was to Override the onCreate() function inside MainActivity.java.
public class MainActivity extends ReactActivity {
/* Any previous code you had here */
[...]
/* Override the onCreate function here */
#Override
protected void onCreate(Bundle savedInstanceState) {
/* Add this line to keep the original behavior of onCreate() */
super.onCreate(savedInstanceState);
/* This one does the trick */
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
}
}
My IDE shows me the error even is empty project. As you can see I create the new project with empty activity. App is launched in mobile successfully and work fine. but logcat isn't working properly or any thing else I'm stuck help me out. And when I connect my mobile even I didn't build the project logcat is full of instruction/logs.
UPDATE:
First, gralloc stands for the low-level graphics buffer allocator.
The gralloc is part of the HAL (Hardware Abstraction Layer) which
means that the implementation is platform-specific. You can find the
interface definitions in
hardware/libhardware/include/hardware/gralloc.h. As expected from a
HAL component, the interface is divided into a module interface
(gralloc_module_t) and a device interface (alloc_device_t).
The error is very low level and related with OpenGLRenderer. Your device is trying to open some 64-bit library files, but if fails. Then the error occurs.
I did very deep search about this but nothing useful came up.
Try running apps on another devices and see the error occurs again.
Continue developing if the error is not causing crash or other important issue. If you find a solution, you will try again.
in my case, I tried to use Application class static context for calling new Activity and I changed it to local static Activity and problem solved.
Notice that this problem doesn't happen in all devices it just occur in some cases.
private static MainActivity u_static;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_users);
u_static = this;
}
public static void new(int id, String name){
Intent i = new Intent(u_static, NewActivity.class);
u_static.startActivity(i);
}
And something I notice that in some devices when an application stops working instead of showing error, this error happens. Trace and debug your codes.
About the crazy E/HAL: load: id=gralloc != hmi->id=gralloc
Reason for error: Generally, it is caused by the fact that the control object is not fetched, the click event of the control is executed, and the null value is related to the initialization control.
As
private Button bt;
bt.setOnclickListener(new OnClickLisenter(){....});
Is there a problem? Yes!
xml layout control
Check if the findViewById() is missing in the java code!
The above leaked bt = (Button) findViewById(R.id...);
java dynamic new control / custom control error
Check if you forgot to create the object!
The above leaked bt = new Button();
So the correct way to change the above code is:
private Button bt;
bt=(Button)findViewById(R.id...);
I encountered this issue while using runnable and passing an unassigned view.
This is caused by an empty view object where findViewById returns a null value.
Instead of passing the view object passing the value the view holds resolved the issue.
When on Android there is not Facebook native app, then in order to share a post from another Android app a web dialog is being opened. Here on step 4 there is a description how to open that dialog (see publishFeedDialog function).
My problems is that all the other code described in this tutorial I have wrote in AppActivity which is a subclass of Activity. Hence, getActivity() method is not defined as far as it is defined for Fragments. To solve this problem I have defined a private variable like this:
private Activity activity;
Assigned a value in onCreate method:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activity = this;
// .......
}
and used in all places where there was a call of getActivity(). As I not a Java neither an Android expert, I would like to understand whether I have done it correctly.
My program is working absolutely fine from android 2.1 to android 2.3
But its not working on higher version of android.
It does not give any exception when the app is executed on a higher version than 2.3.
When run on 2.3 or lower version the number is correctly dialed and imei or other query is executed properly but when the app is run on higher version only dialpad with the code opens but the number/code is not dialed
I have used in my manifest file
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="18" />
And my MainActivity Class
public class Motorola extends Activity implements OnClickListener{
TextView tvm1,tvm2,tvm3;
Intent myIntent;
String m=Uri.encode("#");
String str;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.motorola);
tvm1=(TextView) findViewById(R.id.tvmo1);
tvm2=(TextView) findViewById(R.id.tvmo3);
tvm3=(TextView) findViewById(R.id.tvmo2);
AdView adv3=(AdView) findViewById(R.id.ad4);
adv3.loadAd(new AdRequest());
tvm1.setOnClickListener(this);
tvm2.setOnClickListener(this);
tvm3.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
/* *#06#:DISPLSYS IMEI NUMBER.
*#*#4636#*#*:DISPLAY INFORMATION ABOUT PHONE, BATTERY AND USAGE STATISTICS.
##7764726:MOTOROLA DROID HIDDEN SERVICE MENU. THE DEFAULT SPC PASSWORD IS SIX ZEROES (000000).*/
switch(v.getId())
{
case R.id.tvmo1:
str="*"+m+"06"+m;
myIntent=new Intent(Intent.ACTION_DIAL);
myIntent.setData(Uri.parse("tel:"+str));
startActivityForResult(myIntent,1);
break;
case R.id.tvmo2:
str="*"+m+"*"+m+"4636"+m+"*"+m+"*";
myIntent=new Intent(Intent.ACTION_DIAL);
myIntent.setData(Uri.parse("tel:"+str));
startActivityForResult(myIntent,1);
break;
case R.id.tvmo3:
str=m+m+7764726;
myIntent=new Intent(Intent.ACTION_DIAL);
myIntent.setData(Uri.parse("tel:"+str));
startActivityForResult(myIntent,1);
break;
}
}
}
For some codes, you can try using Intent.ACTION_CALL instead of Intent.ACTION_DIAL
But for most codes Im afraid you wont be able of doing that.
You can take a look at this answer
... in the beginning, it was possible to directly call USSD
codes from an app (using the intent Intent.ACTION_DIAL) [...]
But that was actually considered a
vulnerability of the system, since somebody could write malicious
software, or even more, insert malicious code in a website that could
even wipe your phone or block the sim card[...]
At this point it will be hard to find any device
still vulnerable.
Go to the answer to find out more
You shouldn't be using the dialer for these tasks. Instead, use the appropriate Android APIs to get this information. Here are two questions to get you started quickly:
How to get the device's IMEI/ESN programmatically in android?
Get battery level before broadcast receiver responds for Intent.ACTION_BATTERY_CHANGED
For more details, see the TelephonyManager reference documentation and the Android training article Monitoring the Battery Level and Charging State.