[Q] Dynamically edit Uri for playing audio-files - Android Q&A, Help & Troubleshooting

I'm trying to play an mp3-file located in res/raw.
Problem is that I have a lot of audio that needs to be played dynamically.
This code does work for "mySound.mp3".
Code:
mPlayer = MediaPlayer.create(main.this, R.raw.mySound);
mPlayer.start();
Whereas this code does NOT work:
Code:
String soundName = "mySound";
Uri uri_soundName = Uri.parse("R.raw." + soundName);
mPlayer = MediaPlayer.create(main.this, uri_soundName);
mPlayer.start();
I've tried various other syntaxes to get the uri right, for instance:
Code:
uri_soundName = Uri.parse("android.resource://com.myProject/myApp/raw/" + soundName);
...but to no avail.
Does anybody have an idea what I'm doing wrong?
Thanks,
Dan

Android Software Development
Questions or Problems Should Not Be Posted in the Development Forum
Please Post in the Correct Forums
Moving to Q&A

Related

Help with App and shell script integration

I have an app I have written with the help of another user on the forum here. It currently executes shell scripts that are located in the /system/bin directory.
Is there a way to add these scripts to an assets directory within the app and call on them from list view strings?
flappjaxxx said:
I have an app I have written with the help of another user on the forum here. It currently executes shell scripts that are located in the /system/bin directory.
Is there a way to add these scripts to an assets directory within the app and call on them from list view strings?
Click to expand...
Click to collapse
You can add anything to the "assets" directory in your project and it will be stored as-is in the apk; however, you'll need to extract it and place it somewhere to be able to use it. "res/raw" is another place you can add raw resources. Your main activity should check to see if you're scripts exist and if not, extract and copy them to a directory owned by your apk (like "/data/data/com.example.myprog/shell-scripts"). Create a function in your class and call it from the main activity in OnCreate(). Add a function something like this:
Code:
protected void CheckAndCopyScript() {
// check if it's already there
File myscript = new File("/data/data/com.example.myapp/scripts/script.sh");
if (myscript.exists()) {
// already there, nothing to do.
return;
}
//create the directory
Process p1 = Runtime.getRuntime().exec("sh");
DataOutputStream os1 = new DataOutputStream(p1.getOutputStream());
os1.writeBytes("mkdir /data/data/com.example.myapp/scripts/\n");
os1.writeBytes("exit\n");
os1.flush();
//open the raw resource
InputStream scriptStream = getResources().openRawResource(R.raw.myscript);
try {
byte[] bytes = new byte[scriptStream .available()];
DataInputStream dis = new DataInputStream(scriptStream );
dis.readFully(bytes);
//create the new script file
FileOutputStream scriptOutStream = new FileOutputStream(
"/data/data/com.example.myapp/scripts/script.sh");
scriptOutStream .write(bytes);
scriptOutStream .close();
// set executable permissions on the script
Process p2 = Runtime.getRuntime().exec("sh");
DataOutputStream os2 = new DataOutputStream(p2.getOutputStream());
os2.writeBytes("chmod 755 /data/data/com.example.myapp/scripts/script.sh\n");
os2.writeBytes("exit\n");
os2.flush();
} catch (Exception e) {
//show the exception
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
return;
}
}
Thanks for the info. I will try this soon when I am jot so sick and hopefully you won't mind if I pock your brain a bit if I run into issues.
Sent from A Van Down By The River!
Questions or Problems Should Not Be Posted in the Development Forum
Please Post in the Correct Forums
Moving to Q&A
lufc said:
Questions or Problems Should Not Be Posted in the Development Forum
Please Post in the Correct Forums
Moving to Q&A
Click to expand...
Click to collapse
Sorry about that total brain cramp apparently on my part

[Q] How to get a Reference ID to accept a variable input?

Hi, I'm trying to create an application, and i require to use a variable input for a reference ID to display different arrays here is what i am using at the moment
Code:
public void verbTenses() {
Spinner verbs = (Spinner) findViewById(R.id.verbs);
Spinner tenses = (Spinner) findViewById(R.id.verb_tenses);
[COLOR="RoyalBlue"]ListAdapter adapter = R.array.(verbs.getSelectedItem() + "_" + tenses.getSelectedItem());[/COLOR]
ListView list = (ListView) findViewById(R.id.ListView01);
list.setAdapter(adapter);
}
the error area is highlighted
Thank you
Questions or Problems Should Not Be Posted in the Development Forum
Please Post in the Correct Forums
Moving to Q&A

Help with XML NodeList

Im a noob to Android programming but have some experience with xml in other languages, here is what i have....
This is my XML
Code:
<myrooms>
<room type="private" lastupdate="1347500059" roomname="03EBFDF4AB76E9599A979AB1FED4" roomalias="Ryan,,,chronic Chris">
<entry timestamp="1347493468" handle="Ryan" sender="599A979AB1FED4">Entry one</entry>
<entry timestamp="1347499372" handle="Ryan" sender="599A979AB1FED4">entry two</entry>
<entry timestamp="1347500059" handle="Ryan" sender="599A979AB1FED4">entry three</entry>
</room>
</myrooms>
Code:
Document doc_roomlist;
NodeList nodelist_roomlist;
doc_roomlist = WebRequest.XMLfromString(newxml); //newxml contains the above xml
nodelist_roomlist = doc_roomlist.getElementsByTagName("room");
int size = nodelist_roomlist.getLength();
for (int z = 0; z < size; z++) {
mNode=nodelist_roomlist.item(z);
String roomname = mNode.getAttributes()
.getNamedItem("roomname").getNodeValue();
/// so the above is okay i can get all the attributes of the room nodes
/// but i'm having problems getting the entry nodes
NodeList entries=mNode.getElementsByTagName??????? <<< thats what i cant figure out how to get the entries into another nodelist
}
I cannot seem to get the entry nodes into another nodelist any help would really be appreciated.
when i do
NodeList nl = mNode.getChildNodes();
instead of returning the 3 entry nodes
it returns 6 nodes
one called #text and then my node
then #text and my other entry
so why is getChildNodes making two nodes for each <entry> and what is the #text?
Questions or Problems Should Not Be Posted in the Development Forum
Please Post in the Correct Forums & Read the Forum Rules
Thanks ✟
Moving to Q&A
really nobody can explain this nodelist thing to me? why there are 2 nodes for every one ??

[Q] What the correct way to read from thumb_image column on messages table (Whatsapp)

Hi all,
I build a software for myself that read from Whatsapp db (that stored on my android device)..
I trying to read from thumb_image column but cannot find the correct way ...
I write this software on c# language... and the code for read its column as follows:
//thumbImage is the value that stored in thumb_image column.
byte[] encodedbytes = System.Text.Encoding.Unicode.GetBytes(thumbImage);
string encoded = System.Convert.ToBase64String(encodedbytes);
byte[] utf8Decoded = System.Text.Encoding.UTF8.GetBytes(encoded);
File.WriteAllBytes("Images\\" + mdeiaName, utf8Decoded);
but it's not working, so i trying to use IronPython as following:
string pythonScript = @"import base64
class DecryptString(object):
def Decrypt(self, str):
return base64.b64encode(str).decode(""utf-8"")";
ScriptEngine engine = Python.CreateEngine();
ScriptSource source = engine.CreateScriptSourceFromString(pythonScript);
ScriptScope scope = engine.CreateScope();
source.Execute(scope);
dynamic DecryptString = scope.GetVariable("DecryptString");
dynamic decryptString = DecryptString();
var result = decryptString.Decrypt(thumbImage);
File.WriteAllText("Images\\" + mdeiaName, result);
but without success...
Can somebody help me?

Question regarding Vue-native

hello,
I'm working on a Vue-native project. I'm asking the question here because there is a limited community of developers using vue-native.
i'm working on a vue-native project where i need to create a hexadecimal to decimal and decimal back to hexadecimal converter. i have absolutely no clue about this numbering system. Is there any way I can create a converter in javascript or any other language?
Yes, pretty easily actually
JavaScript:
const base = 16;
let decNumber = 1234
let hexNumber = decNumber.toString(base)
console.log(hexNumber)
//"4d2"
typeof hexNumber
//"string"
and the other way around:
JavaScript:
const base = 16;
let hexNumber = "4d2"
let decNumber = parseInt(hexNumber, base)
console.log(decNumber)
//1234
typeof decNumber
//"number"
By changing "base" you can convert to other types:
JavaScript:
base = 2 //binary
base = 8 //octadecimal
base = 16 //hexadecimal
Thanks sir. This piece actually worked. I really appreciate it!..
denzelvieta said:
Thanks sir. This piece actually worked. I really appreciate it!..
Click to expand...
Click to collapse
istir said:
Yes, pretty easily actually
JavaScript:
const base = 16;
let decNumber = 1234
let hexNumber = decNumber.toString(base)
console.log(hexNumber)
//"4d2"
typeof hexNumber
//"string"
and the other way around:
JavaScript:
const base = 16;
let hexNumber = "4d2"
let decNumber = parseInt(hexNumber, base)
console.log(decNumber)
//1234
typeof decNumber
//"number"
By changing "base" you can convert to other types:
JavaScript:
base = 2 //binary
base = 8 //octadecimal
base = 16 //hexadecimal
Click to expand...
Click to collapse
Hello sir,
I just showed the answer to my client and this code does not return the correct answer for the large digits. For example, if i insert this number "1111111111111111" then this code returns "37450626705270707" which the actual answer is "1229782938247303441". My client gave me reference of this tool: https://binarytotext.net/hexadecimal-to-decimal/. Is there any way I can get the correct answer for the larger numbers?
There's nothing wrong with the code. Use Bignumber.js, it is used to handle big numbers in Javascript!!!
denzelvieta said:
Hello sir,
I just showed the answer to my client and this code does not return the correct answer for the large digits. For example, if i insert this number "1111111111111111" then this code returns "37450626705270707" which the actual answer is "1229782938247303441". My client gave me reference of this tool: https://binarytotext.net/hexadecimal-to-decimal/. Is there any way I can get the correct answer for the larger numbers?
Click to expand...
Click to collapse
I'm pretty sure this isn't an error in the code but rather "bug" in javascript.
As per this stackoverflow answer javascript uses IEEE-754 for storing numbers (which is mostly used for storing doubles, not integers). If you want to see what happens when you type in a really big number check out this tool (that's why you don't want to store eg. phone number as an integer).
This stackoverflow answer should probably give you a good idea what to do. This answer works for "1111111111111111".

Categories

Resources