Android Visualizer class throwing runtime exception - android

I am using AndroidFX Visualizer class in my demo app to read FFT but when i try to create object of that class its throwing Runtime exception (java.lang.RuntimeException: Cannot initialize Visualizer engine, error: -1). Player class is my custom class for playback control and using same Player class i have implemented equalizer class and that's working fine. Do i need to add any permission in manifest file?
Player mediaPlayer = Player.GetInstance();
mediaPlayer.LoadFile("song.mp3");
mediaPlayer.Play();
try{
visual = new Visualizer(mediaPlayer.GetAudioSessionID()); // this line causing Exception
visual.setEnabled(true);
visual.setCaptureSize(Visualizer.getCaptureSizeRange()[1]);
}
catch(Exception ex)
{
Log.e("Visual Ex", ex.getMessage());
}

That was due to my foolish mistake, that feature requires <uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission> permission. thanks

I know this is a very late answer but I also struggled with this problem and I want to share my experiences.
First, as the answer above mentioned, the permissions
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
and, if audio source 0 is used (Visualizer(0); //system mix),
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
are needed. After adding the permissions to my app and installing the (new compiled) app again, my app still crashed. I found out, that the device has to be restarted, to use the Visualizer without any exception (for whatever reason). So if you develop an app and get this exception, a restart could be required after adding the permissions to the app .

Related

Using java.awt.robot on Android app service

I'm trying to use java.awt.Robot on android application with this code
Robot robot = null;
try {
robot = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
// Simulate a key press
robot.keyPress(KeyEvent.KEYCODE_A);
robot.keyRelease(KeyEvent.KEYCODE_A);
By default, It seems the
import java.awt.Robot;
is not available on Android SDK. So to do that I tried to import the .jar downloaded from http://www.java2s.com/Code/Jar/j/Downloadjavartjarstubs150jar.htm called
java-rt-jar-stubs-1.5.0.jar
But when I run the application on that line of code
robot = new Robot();
I get this Exception
java.lang.VerifyError: Verifier rejected class java.awt.Robot due to bad method void java.awt.Robot.<init>(java.awt.GraphicsDevice) (declaration of 'java.awt.Robot' appears in /data/app/com.example.myapp-1/split_lib_dependencies_apk.apk)
So I'm starting to think that maybe I can't use Java.awt.Robot even if I import the jar as a dependence of application.
As you can see from code, I need from my Application (in particular from my intent) to emulate keyboard input and write in every input text.
So I need to send text to input focused as emulated keyboard press.
Any ideas?
P.S.: I also tried to use this
Instrumentation m_Instrumentation = new Instrumentation();
m_Instrumentation.sendKeyDownUpSync( KeyEvent.KEYCODE_B );
whit permission
<permission android:name="android.permission.INJECT_EVENTS" />
but apparently it can not be used from third party application but only from verified Google Apps because I get this exception
java.lang.SecurityException: Injecting to another application requires INJECT_EVENTS permission

MediaRecorder Error: setAudioSource in invalid state(4)

I've been trying to get a custom camera screen to work, but for some reason the following code does not seem to work. I end up with a RuntimeException, caused by an error that says: setAudioSource called in an invalid state(4).
The following is the code in question:
Preview.getRecorderInstance().setVideoSource(MediaRecorder.VideoSource.CAMERA);
Preview.getRecorderInstance().setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
Preview.getRecorderInstance().setAudioSource(MediaRecorder.AudioSource.MIC);
Preview.getRecorderInstance().setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
Preview.getRecorderInstance().setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
Preview.getRecorderInstance().setOutputFile(Environment.getExternalStorageDirectory().getAbsolutePath()
+ "/test" + System.currentTimeMillis() + ".mp4"
);
Preview.getRecorderInstance().prepare();
Preview.getRecorderInstance().start();
Preview.getRecorderInstance() gets the singleton media recorder tied to the Preview class (which is a subclass of SurfaceView designed to display the camera preview).
My permissions:
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
I'd appreciate any help with this, as I'm not getting anywhere in this endeavor, and I've looked at similar questions on stackoverflow. I wasn't able to fix the problem after reading the responses.
Just follow MediaRecord document offcial. Your code maybe is not follow the instruction. The order of functions call must be right.
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mediaRecorder.setOutputFile(_path);
The required ordering of the statements to configure the MediaRecorder is tricky. The documentation states that setAudioSource() must be called before setOutputFormat(). Flip the order of the statements to be like this:
Preview.getRecorderInstance().setAudioSource(MediaRecorder.AudioSource.MIC);
Preview.getRecorderInstance().setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);

Android Permission denial in Widget RemoteViewsFactory for Content

I have a widget which I am trying to use to display information from my app's local database inside of a listview.
I'm using the RemoteViewsService.RemoteViewsFactory interface to load my list's contents. If I run the block of code which reloads the list in the onDataSetChanged method. the app crashes with the following message:
11-01 16:40:39.540: E/ACRA(27175): DataDisplay fatal error : Permission Denial: reading com.datadisplay.content.ContentProviderAdapter uri content://com.datadisplay.provider.internalDB/events from pid=573, uid=10029 requires the provider be exported, or grantUriPermission()
However, this same code run in the class's constructor works just fine. Of course, I need to have this also work in the onDataSetChanged method for updating and stuff.
Here is my provider's entry in the manifest:
<provider android:name="com.datadisplay.content.ContentProviderAdapter"
android:authorities="com.datadisplay.provider.internalDB"
android:exported="true"
android:enabled="true"
android:grantUriPermissions="true">
<grant-uri-permission android:pathPattern="/events/"/>
</provider>
I am both exporting it AND granting Uri permissions like the error message requests, but it still fails. I found this question, where the guy had an issue but eventually removes his custom permissions and it worked. I don't have any custom permissions like that, but still no luck:
Widget with content provider; impossible to use ReadPermission?
If anyone has insight I'd be really grateful, this is getting incredibly frustrating, haha.
This is happening because RemoteViewsFactory is being called from a remote process, and that context is being used for permission enforcement. (The remote caller doesn't have permission to use your provider, so it throws a SecurityException.)
To solve this, you can clear the identity of the remote process, so that permission enforcement is checked against your app instead of against the remote caller. Here's a common pattern you'll find across the platform:
final long token = Binder.clearCallingIdentity();
try {
[perform your query, etc]
} finally {
Binder.restoreCallingIdentity(token);
}
Put this in your onDataSetChanged() method:
Thread thread = new Thread() {
public void run() {
query();
}
};
thread.start();
try {
thread.join();
} catch (InterruptedException e) {
}
Fetch data from the database inside query() method. I do not know why fetching data in a separate thread helps get around this problem, but it works! I got this from one of the Android examples.
If this only happens for 4.2 and not the rest, you need to set the android:exported="true", because the default is changed:
http://developer.android.com/about/versions/android-4.2.html
Content providers are no longer exported by default. That is, the default value for the android:exported attribute is now “false". If it’s important that other apps be able to access your content provider, you must now explicitly set android:exported="true".

Activity Access Restriction Implementation in a Monodroid Application

I have a requirement where I need to restrict access to an activity of a Monodroid Application. Hence i tried a spike where the application IntentSayHello would have an access restricted Activity called SayHelloActivity. As the first step i defined the permission tag in the AndroidManifest.xml of the application as below:
...
...
</application>
<permission
android:name="intentsayhello.permission.SAYHELLO"
android:protectionLevel="signature" android:label="#string/permlbl_restricted"
android:description="#string/permdesc_restricted">
</permission>
</manifest>
Please note that i'm using protectionLevel = signature which means that any other application signed with the same certificate as IntentSayHello can only access the restricted activity.
Now i coded the SayHelloActivity as below:
[Activity(Label = "SayHelloActivity", MainLauncher = true, Icon = "#drawable/icon", Permission = "intentsayhello.permission.SAYHELLO")]
[IntentFilter(new string[] { "companyXYZ.intent.sayhello.MAIN" },Categories = new string[]{Intent.CategoryDefault},
DataMimeType = "vnd.companyXYZ.say.hello/vnd.companyXYZ.activity")]
public class SayHelloActivity : Activity
{
.....
.....
}
After this i tested with a client application by invoking SayHelloActivity of IntentSayHello through an implicit intent and i got SecurityException as expected.
Permission Denial: starting Intent { act=companyXYZ.intent.sayhello.MAIN typ=vnd.companyXYZ.say.hello/vnd.companyXYZ.activity cmp=IntentSayHello.IntentSayHello/intentsayhello.SayHelloActivity }
from ProcessRecord{4094f850 9126:DiffKeyHello.DiffKeyHello/10097} (pid=9126, uid=10097) requires intentsayhello.permission.SAYHELLO
Now if i want my client Application to be given access to the SayHelloActivity of the restricted application, i'm supposed sign my client application with the same keystore (certificate) and also mention in the AndroidManifest.xml of the client application as below:
...
<uses-sdk android:minSdkVersion="4" />
<uses-permission android:name="intentsayhello.permission.SAYHELLO" />
</manifest>
But when i did both of this, the client application still could not invoke the SayHelloActivity and same SecurityException is thrown.
I would like to know the directions/solution to this issue.
Thanks
After Googling around, hit upon this page: http://lists.ximian.com/pipermail/mono-bugs/2011-January/108218.html
I took the cue from this page that the need to be put into the Client's AssemblyInfo.cs. The syntax would be:
// Add some common permissions, these can be removed if not needed
[assembly: UsesPermission("intentsayhello.permission.SAYHELLO")]
[assembly: UsesPermission(Android.Manifest.Permission.Internet)]
It worked from then on.
Further search in mono android forum, i found that another way of doing this is to explicitly add the androidmanifest.xml in the .csproj file as below:
<PropertyGroup>
<AndroidManifest>Properties\AndroidManifest.xml</AndroidManifest>
</PropertyGroup>
Either of the above solutions works, only i do not know which/both are correct practice.

"unable to open the database file" error in Flex for Android

Using Flex 4.5 for Android development, this is the script that should create the database:
private var db:File = File.userDirectory.resolvePath("events.db");
private var conn:SQLConnection;
public function MyDB() {
conn = new SQLConnection();
conn.addEventListener(SQLEvent.OPEN, openHandler);
conn.addEventListener(SQLErrorEvent.ERROR, errorHandler);
conn.open(db, );
}
and I have added this permission:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
but I get this error:
SQLError: 'Error #3125: Unable to open the database file.', details:'Connection closed.', operation:'open', detailID:'1001'
at flash.data::SQLConnection/internalOpen()
at flash.data::SQLConnection/open()
at com.galleons.util::MyDB()[/Users/luca/Documents/Adobe Flash Builder 4.5/Galleons/src/com/galleons/util/MyDB.as:24]
I know it's an old question, but anyway I was facing the same error and found the cause. If any of the parent directories of File which you pass to SQLConnection.open() does not exist, Flash Player throws an Error with detailID=1001. Simply call dbFile.parent.createDirectory() and the error should be gone.
Similar answer was given on Adobe Forums: SQLError #3125
Have you checked the 'usual suspects'?
file exists
not locked by some other app / stale version of your app
path is correct
At least part of the problem is due to mixing the SQLConnection class's open() method – which is synchronous – with events that are only supposed to be used when opening an asynchronous connection. You would open an asynchronous connection by using the openAsync() method instead of the open() method.
The docs are contradictory in this matter because it is, in fact, possible to listen for SQLEvent.OPEN when opening a synchronous connection. However, notice that the SQLErrorEvent.ERROR listener is not being triggered in your code and you are instead getting a runtime error. The docs make no mention of SQLErrorEvent.ERROR working with a synchronous connection; that does appear to be the case.
It's possible this is an AIR bug, but I suspect mixing synchronous methods with asynchronous event listeners is just a gray area. It's also likely that the problem could be solved if you instead wrap the open() call in a try/catch block, which is the recommended way to catch synchronous errors:
try
{
conn.open(db);
trace("Hey, is that a database?", (db.exists));
}
catch (err:SQLError)
{
trace("Error, database not created:", err.message);
trace("Error details:", err.details);
}

Categories

Resources