Search code examples
c#androidandroid-activityandroid-mediaplayer

invalid resource directory name: "assets" for running an mp3 file in the second activity


I wrote this code in VS 2012 into C#

    MediaPlayer mPlayer;
    mPlayer = MediaPlayer.Create(this, Resource.Raw.house);
    mPlayer.Start();

This code is run in MainActivity but it is not run in the second activity. I have this error:

invalid resource directory name: "assets"

Solution

  • To get your files from assets you have to do

    AssetFileDescriptor afd = getAssets().openFd("AudioFile.mp3");
    player = new MediaPlayer();
    player.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength();
    player.prepare();
    player.start();
    

    Let me know if it is not working