Play audio file from Resources – Android

playSample(R.raw.defaultmusic);

private void playSample(int resid)
	{
	    AssetFileDescriptor afd = getApplicationContext().getResources().openRawResourceFd(resid);

	    try
	    {   
	        mp.reset();
	        mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getDeclaredLength());
	        mp.prepare();
	        mp.start();
	        afd.close();
	    }
	    catch (IllegalArgumentException e)
	    {
	        Log.e(TAG, "Unable to play audio queue do to exception: " + e.getMessage(), e);
	    }
	    catch (IllegalStateException e)
	    {
	        Log.e(TAG, "Unable to play audio queue do to exception: " + e.getMessage(), e);
	    }
	    catch (IOException e)
	    {
	        Log.e(TAG, "Unable to play audio queue do to exception: " + e.getMessage(), e);
	    }
	}

Leave a Reply

Your email address will not be published. Required fields are marked *