Search code examples
androidandroid-mediaplayer

How to get currently running raw id in Mediaplayer


In android MediaPlayer we can create an Object like MediaPlayer mPlayer = MediaPlayer.create(Activity.this, R.raw.sound);

But my question is what if I want to get this raw id in somewhere else in my activity. Is it possible ? If so how?

I search for it but could not reach any solution.


Solution

  • from activity

    int sound_file_id =  getResources().getIdentifier("sound","raw" ,getPackageName());
    

    from fragment

    int sound_file_id =  getActivity().getResources().getIdentifier("sound","raw" ,getActivity().getPackageName());
    

    hope this answers your question