Play audio button is not working

android, audio, button, media-player

Solution

you need to concatenate the String `mFileName`

mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
mFileName += "/rec" + recR  + ".mp3"; 

Problem

I have a layout in which there's a 'Listen' button, so that if I press it, it will play certain audios by random (they have been assigned ids, but let's not worry about that) in my phone. However, when I click on the button, nothing plays, although the toast shows up with the correct filename. Can someone please take a look and tell me what's wrong with my code? Thanks! >_< ``` Button btnListen= (Button) findViewById(R.id.buttonListen); String recR = getIntent().getStringExtra("dataR"); //importing random filename mFileName = Environment.getExternalStorageDirectory().getAbsolutePath(); mFileName = "/rec" + recR + ".mp3"; //filename starts with rec, so itll usually be like recwoof.mp3 or rectest.mp3 etc btnListen.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { MediaPlayer mPlayer = new MediaPlayer(); Toast.makeText(getApplicationContext(), "show "+ mFileName + " now", Toast.LENGTH_LONG).show(); try { mPlayer.setDataSource(mFileName); mPlayer.prepare(); mPlayer.start(); } catch (IOException e) { Log.e("meh log", "prepare() failed"); } } ```

Original source