Playing audio file from Sdcard

断了今生、忘了曾经 提交于 2019-11-28 01:22:12

Use the code below it worked for me.

MediaPlayer mp = new MediaPlayer();
mp.setDataSource("/mnt/sdcard/yourdirectory/youraudiofile.wav");
mp.prepare();
mp.start();

I have played the sound from sd card using following code and it works for me.

private SoundPool mSoundPool;
private int mSoundID;
private boolean isLoaded = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d(TAG, "Start Greeting View Activity...");

    setContentView(R.layout.greeting_view_activity);
    //mGiGiView = (GreetingWidget) findViewById(R.id.gigi_greet);
    //mGiGiView.setOnTouchListener(this);

    //Set default animation sound path.
    String soundAnimUrl = "/gigi/anim/evening.ogg";

    // get the Bundle out of the Intent.
    Bundle extras = getIntent().getExtras();
    if (extras != null) {

        // check to see if "soundAnimUrl" is in the bundle, if so then
        // assign it's value to animUrl if not, assign null to soundAnimUrl.
        soundAnimUrl = extras.containsKey("soundAnimUrl") ? extras
                .getString("soundAnimUrl") : null;
    }

    // Set the hardware buttons to control the music.
    this.setVolumeControlStream(AudioManager.STREAM_MUSIC);

    // Load the sound.
    mSoundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
    mSoundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
        @Override
        public void onLoadComplete(SoundPool soundPool, int sampleId,
                int status) {
            isLoaded = true;

            // Play the sound when loaded
            play();
        }
    });

    mSoundID = mSoundPool
            .load(getFile(Environment.DIRECTORY_MUSIC, soundAnimUrl)
                    .getPath(), 1);

    //Play sound from raw directory
    // soundID = soundPool.load(this, R.raw.greeting1, 1);      
}

private void play() {
    // Getting the user sound settings
    AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
    float actualVolume = (float) audioManager
            .getStreamVolume(AudioManager.STREAM_MUSIC);
    float maxVolume = (float) audioManager
            .getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    float volume = actualVolume / maxVolume;

    // Is the sound loaded already?
    if (isLoaded) {
        mSoundPool.play(mSoundID, volume, volume, 1, 0, 1f);
        Log.d(TAG, "Played sound");
    }
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        switch (v.getId()) {
        case R.id.gigi_greet:
            play();
            break;

        default:
            break;
        }
    }
    return false;
}   

/**
 * Get File instance from sd card path.
 * 
 * @param deviceFolderPath
 *            - Pictures, Music, etc
 * @param dbFilePath
 *            - path stored in db (/gigi/anim/morning.ogg)
 * @return
 */
public File getFile(final String deviceFolderPath, final String dbFilePath) {

    // Create full path
    String picturePath = deviceFolderPath.concat(File.separator).concat(
            dbFilePath);

    // Create file
    File mFile = getExternalFilesDir(picturePath);

    return mFile;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!