Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Mobile / Android

Android: Play Media File in the Assets Folder

4.00/5 (1 vote)
17 Apr 2013CPOL 25.7K  
Play a media file (wav, MP3, etc) in the Assets folder.

Introduction

Play a media file (wav, MP3, etc) in the Assets folder.

Using the code

You can write an application using the code that plays a media file which is stored in your Assets folder.

Play a Media File in Assets Folder

Java
public void Play(String fileName)
{
    AssetFileDescriptor descriptor = _context.getAssets().openFd(fileName);
    long start = descriptor.getStartOffset();
    long end = descriptor.getLength();
    MediaPlayer mediaPlayer=new MediaPlayer();
    mediaPlayer.setDataSource(descriptor.getFileDescriptor(), start, end);
    mediaPlayer.prepare();
    mediaPlayer.start();     
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)