[To Android Page]

Android examples how to play audio files

Android code examples to play audio asset or resource files.

Download
Source code audiodemo-src.zip
Android APK (min API 17, SDK 4.2) zip'd audiodemo-apk.zip
Android APK (min API 17, SDK 4.2) audiodemo.apk
WARNING - Chrome on 4.2 will get stuck trying to download APK, use a different browser like FireFox
GitHub source https://github.com/landenlabs/audiodemo

Simple applications with list of audio files. The audio files are stored in botht the res/raw directory and the asset directory. Best to stored audio files in mp3 encoding audio format which is supported by both old and new OS levels.

Android media formats

Image shows workspace layout with audio files (mp3) stored in both assets and res/raw directory.

dir-assets dir-res-raw


Sample of Audio Demo app screen:

audiodemo

Select and audio file then press the various play otions:

  • Notification - Play using status notification service
  • Raw - Play audio file stored in res/raw directory
  • Asset - Play audio file stored in asset directory

  • Play using Notification service

    Example of how to play Audio file in resource raw directory using the Status bar Notifcation service:

    
    private void notifySound(String assetName) {
        NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    
        // Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        String RESOURCE_PATH = ContentResolver.SCHEME_ANDROID_RESOURCE + "://";
    
        String path;
        if (false) {
            path = RESOURCE_PATH + getPackageName() + "/raw/" + assetName;
        } else {
            int resID = getResources().getIdentifier(assetName, "raw", getPackageName());
            path = RESOURCE_PATH + getPackageName() + File.separator + resID;
        }
        Uri soundUri = Uri.parse(path);
    
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext())
                .setContentTitle("Title")
                .setContentText("Message")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setSound(soundUri); //This sets the sound to play
    
        notificationManager.notify(10, mBuilder.build());
    }
    

    [To Top]


    Play audio file from resource raw directory

    Examples to load and play audio file stored in res/raw directory:
    
    private void playSound2(String assetName) {
        try {
            // Syntax  :  android.resource://[package]/[res type]/[res name]
            // Example : Uri.parse("android.resource://com.my.package/raw/sound1");
            //
            // Syntax  : android.resource://[package]/[resource_id]
            // Example : Uri.parse("android.resource://com.my.package/" + R.raw.sound1);
    
            String RESOURCE_PATH = ContentResolver.SCHEME_ANDROID_RESOURCE + "://";
    
            String path;
            if (false) {
                 path = RESOURCE_PATH + getPackageName() + "/raw/" + assetName;
            } else {
                int resID = getResources().getIdentifier(assetName, "raw", getPackageName());
                path = RESOURCE_PATH + getPackageName() + File.separator + resID;
            }
            Uri soundUri = Uri.parse(path);
            mSoundName.setText(path);
    
            mMediaPlayer = new MediaPlayer();
            if (true) {
                // Internal asset
                mMediaPlayer.setDataSource(getApplicationContext(), soundUri);
                mMediaPlayer.prepare();
            } else if (false) {
                // Load external audio files url:
                //  "http://www.bogotobogo.com/Audio/sample.mp3";
                mMediaPlayer.setDataSource(path);
                mMediaPlayer.prepare();
            } else {
                // Internal resource asset
                ContentResolver resolver = getContentResolver();
                AssetFileDescriptor afd = resolver.openAssetFileDescriptor(soundUri, "r");
                mMediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
                afd.close();
            }
    
            mMediaPlayer.start();
        } catch (Exception ex) {
            Toast.makeText(this, ex.getMessage(), Toast.LENGTH_LONG).show();
        }
    }
    
    

    [To Top]


    Play audio file from asset directory

    Example to play sound file stored in asset directory. Asset files are all merged together into a single file during the package build process. To play an asset file you need to provide its offset and length to the MediaPlayer.
    
    private void playSound3(String assetName) {
        try {
            AssetFileDescriptor afd =  getAssets().openFd("sounds/" + assetName + ".mp3");
            mMediaPlayer = new MediaPlayer();
            mMediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
            afd.close();
            mMediaPlayer.prepare();
            mMediaPlayer.start();
        } catch (Exception ex) {
            Toast.makeText(this, ex.getMessage(), Toast.LENGTH_LONG).show();
        }
    }
    
    

    [To Top]



    Syntax code highligter provided by https://highlightjs.org