play an asset file using MediaPlayer - Android App

Android examples for App:Assets File

Description

play an asset file using MediaPlayer

Demo Code


//package com.java2s;
import java.io.IOException;
import android.content.Context;
import android.content.res.AssetFileDescriptor;

import android.media.MediaPlayer;

public class Main {
    public static MediaPlayer mMediaPlayer = null;

    public static void playKaKhaGa(Context context, String file) {
        try {/*w ww  . j  a va2 s  .com*/
            AssetFileDescriptor afd = context.getAssets().openFd(file);
            mMediaPlayer.setDataSource(afd.getFileDescriptor(),
                    afd.getStartOffset(), afd.getLength());
            afd.close();
            mMediaPlayer.prepare();
            mMediaPlayer.start();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

Related Tutorials