Android get R.raw from variable

Related searches

i have some sounds in a raw folder on my app. And sometimes i need to play a sound, but i don't know exactly which is.

Example :

String actualSound = "hit"

playSound(mediaPlayer, R.Raw.actualSound));

I want to play the R.raw.hit but i don't know how to do that.

You can obtain the numeric ID of your raw resource using Resources.getIdentifier() and then use it in your play function:

Resources res = context.getResources();
int soundId = res.getIdentifier(actualSound, "raw", context.getPackageName());
playSound(mediaPlayer, soundId);

Note that generally, you shouldn't do this. It is more efficient to access resources by numeric identifier (i.e. R-qualified constant). This would especially matter if you were to do the above every time you want to play sound in a game. It is better to use a mapping of sound names (or better yet: enum values) to resource identifiers or even pre-loaded sound samples.

R.raw, Get the raw value associated with a resource with associated density. XmlResourceParser � getXml(int id). Return an XmlResourceParser through� Overview; Android Platform; AndroidX; AndroidX Test; AndroidX Constraint Layout; Architecture Components; Jetpack Compose UI; Android Automotive Library; Databinding

Quick answer:

public class WebsiteActivity extends AppCompatActivity {

    MediaPlayer mediaPlayer;

    protected void onCreate(Bundle savedInstanceState) {

        Resources res = getResources();
        int sound = res.getIdentifier(music, "raw", getPackageName());

        //Play music
        mediaPlayer = MediaPlayer.create(getApplicationContext(), sound);

    protected void onPause() {


android.content.res.Resources, There is not such thing as ArrayMap in standard Java. Therefore, I am just going to assume that you meant HashMap . I would choose neither of those options. This example demonstrates How to declare global variables in Android? Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml.

Simply use String actualSound = getString(R.Raw.actualSound).

Lookup map for converting Strings into Android `R.raw` integers , decodeResource(getResources(),R.drawable.f); Don't worry, first try to clean project and build project. And even if still doesn't show your resource� R.Java sometimes does not refresh itself in eclipse. This video will point out some possible causes and resolution.

Android studio- error:cannot find symbol variable raw, Adding a raw folder to your resource folder (/res/) should do the trick. Read more here: Android studio- error:cannot find symbol variable raw. I was trying to do� Android SDK environment variables; ANDROID_SDK_ROOT: Sets the path to the SDK installation directory. Once set, the value does not typically change, and can be shared by multiple users on the same machine. ANDROID_HOME, which also points to the SDK installation directory, is deprecated. If you continue to use it, the following rules apply:

How to add R.raw to project? - android, Initializes a string variable with the URL for the XML feed. If the user's settings and the network connection allow it, invokes new DownloadXmlTask().execute(url) . This instantiates a new DownloadXmlTask object ( AsyncTask subclass) and runs its execute() method, which downloads and parses the feed and returns a string result to be displayed

This can take a variable amount of time, depending on the network connectivity. To get an opaque key request byte array to send to a license server, call getKeyRequest(). To inform the DRM engine about the key response received from the license server, call provideKeyResponse(). The result depends on the type of key request: