Dynamically reference the R.raw.-content? - G1 Android Development

Is it possible to dynamically reference a specific resource in the R.raw.-folder?
For instance, if I have an mp3 in the raw-foler named "hello.mp3" I can reference it in the code with:
Code:
mp = MediaPlayer.create(getBaseContext(), R.raw.hello);
But how do I replace the last "hello" with the content of a variable, so that I can load different mp3's from R.raw with the same code?
Code:
// CODE NOT WORKING
String soundFile = "helloagain";
String mpPath = "R.raw." + soundFile;
mp = MediaPlayer.create(getBaseContext(), mpPath);

Related

MediaPlayer.setDataSource(url) not working

MediaPlayer.setDataSource(url) is not working in my app.
Im puttin the file "hello.mp3" in the assets-folder, and Im using the following
Code:
String soundUrl = "file:///android_asset/hello.mp3";
mp = new MediaPlayer();
try {
mp.setDataSource(soundUrl);
mp.prepare();
mp.start();
}
catch (IOException e) {}
catch (IllegalArgumentException e) {}
catch (IllegalStateException e) {}
This code works when I put the "hello.mp3" in the res/raw-folder:
Code:
mp = new MediaPlayer();
mp = MediaPlayer.create(getBaseContext(), R.raw.hello);
mp.start();
Problem with the last code is that I need to load the sound-files dynamically, and apparently you cant create a string or a uri with the correct sound-file at the end and insert that as the second parameter in the MP.create()-function.
Pseudocode - NOT WORKING
Code:
String mySound = "hello";
Uri myUri = "R.raw." + mySound;
...
mp = MediaPlayer.create(getBaseContext(), myUri);
Any ideas?
Amazing, several months later and still no one is able to realte how to play a local sound dynamically.
An issue with what you are trying to do is that there are multiple versions of MediaPlayer.create() and MediaPlayer.setDataSource() which take different types of parameters.
R.raw.hello is NOT a string. It is an int. Look in the gen directory to find the generated file R.java and in this file you will find raw which has a public static final int definition for R.raw.hello
R.raw.hello has to be passed to one of the routines which takes an int for the parameter and not a string.

[Q] choose array-elements with certain probability

hello,
I'm writing a Android App (java) - but I guess this question is pretty general, and isn't java-specific:
so I have an Array of Elements
and I let a random-number-generator pick one array-element randomly and hand it to me.
now I've also built in a "score"-field into each array Element
so what I want to do now, is that the random-number-generator takes the array's "score" in consideration, and gives me the array-elements with the higher/lower scores with a higher/lower probability
I dont want it to ALWAYS/NEVER give me the elements with the highest/lowest score - just with a higher/lower probability
I hope I could describe my problem in a proper way....
does anybody know how to achieve this?
(as I said, i use java, but i guess code in any language - or even pseudo-code would help me out)
*bump*
anybody?
Try this, it's in C# but it's pretty close to Java.
You cannot directly weight the random function so you have to use a different method.
By applying a weight to each item in the array, then using a random function to select using the weighting values, the results of the selection can be swayed.
The weighting values are relative to each other.
Using the values in the code, 'B' should turn up about three times more often than 'A'
The only object that may need some explanation is Random:
http://msdn.microsoft.com/en-us/library/system.random(v=VS.80).aspx
Code:
using System;
namespace RandomWeighting
{
class Program
{
static void Main(string[] args)
{
char[] Select = new char[10] {'A','B','C','D','E','F','G','H','I','J'};
int[] Weight = new int[10] {10,30,25,60,20,70,10,80,20,30};
int[] WeightSum = new int[10];
int i,j,k;
Random Rnd = new Random();
WeightSum[0]=Weight[0];
for (i = 1; i < 10; i++)
WeightSum[i] = WeightSum[i - 1] + Weight[i];
for (j = 0; j < 70; j++)
{
k = Rnd.Next(WeightSum[9]);
for (i = 0; k > WeightSum[i]; i++) ;
Console.Write(Select[i]);
}
Console.WriteLine();
}
}
}
Output:
Code:
HEFIBHHCCFBCAEFFDHACHBEJHHFDFIDFEDFFCHHDJBIDJEHHFHCJJJBHJGBDDGFDDFHHHB
Note the low density of A and G as opposed to H
It is just a sample at random, but 2 'A's and 7 'B's roughly matches the conjecture above, but over a million selections, the distribution is as follows:
Code:
A 30664
B 84187
C 70648
D 168481
E 56529
F 197311
G 28145
H 225764
I 56613
J 81658
If your code changes values in the Weight array then the WeightSum array must be recalculated.
wow, thats a lot!
The only thing I dont understand about the code is this line:
k = Rnd.Next(WeightSum[9]);
for (i = 0; k > WeightSum; i++) ;
what exactly happens here?
what kind of a for-loop is this?
why is there no body?
and strangely, while debugging this line, i noticed that the value of i jumps to some random number in this line - and I have no idea why and how
------
by the way: I've already tried it out, it works pretty good.
although I noticed that the first element always gets picked ALOT, no matter how low the weight.
I think thats a flaw in the algorithm
After the following code:
Code:
for (i = 1; i < 10; i++)
WeightSum[i] = WeightSum[i - 1] + Weight[i];
The WeightSum array contains the following values:
Code:
[0] 10
[1] 40
[2] 65
[3] 125
[4] 145
[5] 215
[6] 225
[7] 305
[8] 325
[9] 355
The Random.Next() function that takes a single integer as an argument is defined as:
-------- Courtesy of MS VS .NET Help ------
public virtual int Next(int maxValue)
Parameters maxValue Type: System.Int32
The exclusive upper bound of the random number to be generated. maxValue must be greater than or equal to zero.
Return Value Type: System.Int32
A 32-bit signed integer greater than or equal to zero, and less than maxValue; that is, the range of return values ordinarily includes zero but not maxValue. However, if maxValue equals zero, maxValue is returned.
-------------- End of Help ----------------
So, we are asking for a random value between 0 and 354,(element WeightSum[9] above). The following code then finds which is the lowest element of WeightSum which holds a value greater than the one we have been given. When this happens 'i' contains the index to use for the selection.
Code:
for (i = 0; k > WeightSum[i]; i++);
The standard construction of a for loop in C is
for(One or more initialisation statements; Do_While_True Condition; One or more iteration statements)
{
loop processing code;
}
If there is nothing to do in the loop body, you don't need it, and you can replace it with the ';' at the end of the for statement. In effect, it is identical to the following code:-
Code:
i=0;
while(k > WeightSum[i])
{
i++;
}
As regards the first element being picked more than the others, have a look at the distribution table in post #3. It is what you would expect for the values given. I assume the difference is either the Random function in Java or some different implementation between C# and Java.
You may have to change some of the code slightly, i.e. change the for() loop to the while() loop above and step though it in the Java debugger to get to the root of the problem.
You can't debug a for loop followed by an immediate ';' The entire for loop is executed to completion when you use debug and try and step through it. To debug it place a dummy statement in the for loop. A breakpoint may now be set on this line.
Code:
int i,z;
for (i = 0; k > WeightSum[i]; i++)
{
z=0;
}
ActionScript and Javascript versions ...
Thanks for the great write-up! In case anyone is interested, I've adapted this into a javascript and ActionScript class. If anyone is interested, I've attached the code. For a more in-depth post, check out blog.teamthinklabs.com.
Cheers!
Kipp Ashford
Art Director
Seven2 Interactive

Android ls and df parsing

Hey all together,
after a lot of searching and using Google for days with no really good results i created now my own reg-ex functions to parse some Android shell outputs. So i only want to share my functions coz i dun found something like that.
If u create a Android File-Explorer or something else like this u'll have to get the ls and df output in some situations, if u use C# like me this will be great for u.
here are my functions:
(command) ls -l
(command) busybox ls -aFl
Code:
^(\s{0,}(?<INODE>[0-9]{1,})\s{1,})?(?<TYPE>(-|b|c|d|l|s|p){1})(?<PERM>((-|r|s|t|w|x){9}|[0-9]{4}))\s{0,}(?<INCLUDES>([0-9]){1,}\s{1,})?(?<OWNER>.*?)\s{1,}(?<GROUP>.*?)\s{0,}(?<ID>([0-9]){1,},)?\s{1,}(?<SIZE>([0-9]){1,})?\s{1,}(?<DATE>.*?)\s{1,}(?<TIME>([0-9]{2}.[0-9]{2}(:[0-9]{2})?|[0-9]{4}))\s{1}(\e\[([0-9]{1,};)?[0-9]{1,}m){0,}(?<NAME>.*?)(\e\[([0-9]{1,};)?[0-9]{1,}m){0,}(\s{1,}->\s{1,}(\e\[([0-9]{1,};)?[0-9]{1,}m){0,}(?<SYMLINK>.*?)(\e\[([0-9]{1,};)?[0-9]{1,}m){0,})?(?<SUBTYPE>(\*|\/){1})?$
u can use it like
Code:
// INODE, TYPE, PERM, INCLUDES, OWNER, GROUP, ID, SIZE, DATE, TIME, NAME, SYMLINK, SUBTYPE
GroupCollection groups = Regex.Match("the ls -li or busybox -aFli output line by line", @"^(\s{0,}(?<INODE>[0-9]{1,})\s{1,})?(?<TYPE>(-|b|c|d|l|s|p){1})(?<PERM>((-|r|s|t|w|x){9}|[0-9]{4}))\s{0,}(?<INCLUDES>([0-9]){1,}\s{1,})?(?<OWNER>.*?)\s{1,}(?<GROUP>.*?)\s{0,}(?<ID>([0-9]){1,},)?\s{1,}(?<SIZE>([0-9]){1,})?\s{1,}(?<DATE>.*?)\s{1,}(?<TIME>([0-9]{2}.[0-9]{2}(:[0-9]{2})?|[0-9]{4}))\s{1}(\e\[([0-9]{1,};)?[0-9]{1,}m){0,}(?<NAME>.*?)(\e\[([0-9]{1,};)?[0-9]{1,}m){0,}(\s{1,}->\s{1,}(\e\[([0-9]{1,};)?[0-9]{1,}m){0,}(?<SYMLINK>.*?)(\e\[([0-9]{1,};)?[0-9]{1,}m){0,})?(?<SUBTYPE>(\*|\/){1})?$").Groups;
string INode = groups["INODE"].Value;
string Type = groups["TYPE"].Value;
string SubType = groups["SUBTYPE"].Value;
string Perms = groups["PERM"].Value;
string Includes = groups["INCLUDES"].Value;
string Owner = groups["OWNER"].Value;
string Group = groups["GROUP"].Value;
string Id = groups["ID"].Value;
string Size = groups["SIZE"].Value;
string Date = String.Join(" ", groups["DATE"].Value, groups["TIME"].Value);
string Name = groups["NAME"].Value;
string[] extTmp = Name.Split('.');
string FileExtension = extTmp[extTmp.Length - 1];
string SymPath = groups["SYMLINK"].Value;
(command) df
(command) busybox df -Pakh
Code:
^(df:\s{1,})?(?<NAME>(.*?))(:\s{1,}(.*?))?(\s{1,}(?<SIZE>([0-9]{1,}[a-zA-Z\.]?){1,}))?(\s{1,}(?<USED>([0-9]{1,}[a-zA-Z\.]?){1,}))?(\s{1,}(?<FREE>([0-9]{1,}[a-zA-Z\.]?){1,}))?(\s{1,}(?<BLOCKSIZE>([0-9]){1,}))?(\s{1,}(?<USE>([0-9]{1,})%))?(\s{1,}(?<PATH>(.*?)))?$
u can use it like
Code:
GroupCollection groups = Regex.Match("the df or df -Pakh output line by line", @"^(df:\s{1,})?(?<NAME>(.*?))(:\s{1,}(.*?))?(\s{1,}(?<SIZE>([0-9]{1,}[a-zA-Z\.]?){1,}))?(\s{1,}(?<USED>([0-9]{1,}[a-zA-Z\.]?){1,}))?(\s{1,}(?<FREE>([0-9]{1,}[a-zA-Z\.]?){1,}))?(\s{1,}(?<BLOCKSIZE>([0-9]){1,}))?(\s{1,}(?<USE>([0-9]{1,})%))?(\s{1,}(?<PATH>(.*?)))?$").Groups;
string Name = groups["NAME"].Value;
string Size = groups["SIZE"].Value;
string Used = groups["USED"].Value;
string Free = groups["FREE"].Value;
string BlockSize = groups["BLOCKSIZE"].Value;
string Use = groups["USE"].Value;
string Path = groups["PATH"].Value;
Hope this helps someone
Regards,
k1ll3r8e

Automates patcher tool

Hi,
I've been looking for a tool or script to replace hex sequence in binary file in Windows. Indeed, i found this script :
Code:
Imports System.Runtime.CompilerServices
Imports System.IO
Public Class FinduReplaceHex
Private Shared ReadOnly FindHex As Byte() = {&H75, &HF6, &HF3}
Private Shared ReadOnly ReplaceHex As Byte() = {&HA2, &HE3, &H4B}
<MethodImpl(MethodImplOptions.NoInlining)> Private Shared Function DP(sequence As Byte(), position As Integer) As Boolean
If position + FindHex.Length > sequence.Length Then
Return False
End If
For i As Integer = 0 To FindHex.Length - 1
If FindHex(i) <> sequence(position + i) Then
Return False
End If
Next
Return True
End Function
Private Sub Patch_Click(sender As Object, e As EventArgs) Handles Patch.Click
Dim DT As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
Dim FD As Byte() = File.ReadAllBytes(DT & "\App.dll")
For F As Integer = 0 To FD.Length - 1
If Not DP(FD, F) Then
Continue For
End If
For R As Integer = 0 To FindHex.Length - 1
FD(F + R) = ReplaceHex®
Next
Next
If System.IO.File.Exists(DT & "\App.dll") Then
System.IO.File.Move(DT & "\App.dll", DT & "\App.dll.backup")
File.WriteAllBytes(DT & "\App Method 3.dll", FD)
Else
'Write Other Codes
End If
End Sub
End Class
The only problem is that I want to be able to use wildcards like this :
Search for F4 B3 ?? 12 and replace F4 BE 65 EA
Thanks in advance

How and where my device store fingerprint results and matches with my next attempt ?

MAIN QUESTION IS AT BOTTOM
Where my android devices stores scanned fingerprint data and in what format and how it matches with new scanned.
I also know this: :the scan of fingertip is analysed for certain control points and generates a token which is like a password hash.
It generates hash via this:
Code:
KeyStore mKeyStore;
String KEY_NAME = UUID.randomUUID().toString();
Cipher mCipher;
mKeyStore = KeyStore.getInstance("AndroidKeyStore");
keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
keyGenerator.init(new
KeyGenParameterSpec.Builder(KEY_NAME,
KeyProperties.PURPOSE_ENCRYPT |
KeyProperties.PURPOSE_DECRYPT)
.setBlockModes(KeyProperties.BLOCK_MODE_CBC)
.setUserAuthenticationRequired(true)
.setEncryptionPaddings(
KeyProperties.ENCRYPTION_PADDING_PKCS7)
.build());
keyGenerator.generateKey();
mCipher = Cipher.getInstance(
KeyProperties.KEY_ALGORITHM_AES + "/"
+ KeyProperties.BLOCK_MODE_CBC + "/"
+ KeyProperties.ENCRYPTION_PADDING_PKCS7);
SecretKey key = (SecretKey) mKeyStore.getKey(KEY_NAME, null);
mCipher.init(Cipher.ENCRYPT_MODE, key);
Is editing/extracting or using this hash and storing somewhere else and try to match the newly generated hash with this while storing that security key of android(assuming same for all), is it possible OR ANY OTHERWAY ROUND?
ALSO
Code:
KeyStore ks = KeyStore.getInstance("AndroidKeyStore");
ks.load(null);
KeyStore.Entry entry = ks.getEntry(alias, null);
if (!(entry instanceof PrivateKeyEntry)) {
Log.w(TAG, "Not an instance of a PrivateKeyEntry");
return null;
}
Signature s = Signature.getInstance("SHA256withECDSA");
s.initSign(((PrivateKeyEntry) entry).getPrivateKey());
s.update(data);
byte[] signature = s.sign();
boolean valid = s.verify(signature);
I saw this, but can't say helpful or not

Categories

Resources