문제

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

도움이 되었습니까?

해결책

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

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top