Question

I have kept in resources mp3 file, you need play it with the MediaPlayer

path in Resource Resources/Drawable/beep.mp3

i trying this code

var fileUri = Uri.Parse("android.resource://MyAppName/" + Resource.Drawable.beep);
_mediaPlayer.SetDataSource(Application.Context,fileUri);

but the path of the file is incorrect, how to get the correct path to the resource mp3 file

Was it helpful?

Solution

You're using MP3 from the "drawable" directory, which is wrong.

Use assets.

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

http://developer.android.com/tools/projects/index.html

OTHER TIPS

I actually store my MP3 files in Resources/Raw and use SoundPool instead of MediaPlayer for short clips like beeps, etc. See Soundpool or media player?

protected override void OnCreate(Bundle bundle)
{
    base.OnCreate(bundle);
    SetContentView(Resource.Layout.MyView);

    // Set the hardware buttons to control the music
    this.VolumeControlStream = Stream.Music;

    // Load the sound
    _soundPool = new SoundPool(10, Stream.Music, 0);
    _beepId = _soundPool.Load(this, Resource.Raw.beep, 1);
}

private void PlaySound(int soundId)
{
    var audioManager = (AudioManager)GetSystemService(AudioService);
    var actualVolume = (float)audioManager.GetStreamVolume(Stream.Music);
    var maxVolume = (float)audioManager.GetStreamMaxVolume(Stream.Music);
    var volume = actualVolume / maxVolume;

    _soundPool.Play(soundId, volume, volume, 1, 0, 1f);
}

You can then simply call PlaySound(_beepId) to play your sound.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top