0

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

Artem Polishchuk
  • 505
  • 5
  • 17
  • mp3 file path is assets/beep.mp3 and use the following code AssetFileDescriptor afd = getBaseContext().getAssets().openFd("beep.mp3"); – Shini Apr 23 '14 at 11:53

2 Answers2

0

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

shkschneider
  • 17,833
  • 13
  • 59
  • 112
0

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.

Community
  • 1
  • 1
Kiliman
  • 18,460
  • 3
  • 39
  • 38
  • What is `GetSystemService` a part of? – muttley91 Jul 11 '16 at 20:42
  • It's part of the `Context` class, or in this case from `Activity`. – Kiliman Jul 11 '16 at 20:45
  • What about AudioService? It claims to be a static constant on mine, but you're using it in a non-static context. – muttley91 Jul 11 '16 at 20:56
  • That's correct. `AudioService` is static string constant from `Context` class. But since `Activity` is a subclass of `Context`, it also inherits this constant. And you can reference your own static members without the class qualifier. You could easily use `Context.AudioService` or even `Activity.AudioService` if it'll make it clearer, even if it is redundant. It's like using `this` to reference instance members. It's not required (except to disambiguate). – Kiliman Jul 11 '16 at 21:22