how to write a simple audio player on android?
I have written a simple audio player on android(below). And trying to run on virtual device(AVD). But I am not getting the sound. Is it not possible to get sound using AVD ? Kindly help me.. Thanks
package com.example.helloplayer;
imp开发者_如何学运维ort android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
public class HelloPlayer extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MediaPlayer mp = new MediaPlayer();
mp.setDataSource("/home/usr/audio/file.mp3");
mp.prepare();
mp.start();
}
}
Hi Please your mp3 file put in your app Raw folder and then used below code
mMediaPlayer = new MediaPlayer();
mMediaPlayer = MediaPlayer.create(this, R.raw.sound1);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setLooping(true);
mMediaPlayer.start();
Try adding the mp3 file as a resource to your app. You can't access files on the device like that.
you have to set data source from devices internal or external memory. I think you set datasource path of your computer.
Please check this link.
Does the file exist on the internal or external memory of the virtual device? It looks like you're using a path on your computer.
See Using the External Storage on how to do this. Here is a tutorial on how to create an Emulator with external storage and how to copy a file onto the external storage.
精彩评论