Playing audio file from Sdcard

前端 未结 2 1456
花落未央
花落未央 2020-12-04 00:09

I would like to play an audio file from the sdcard. How can I read the audio file and play it? Below is my code to play audio file:

int sound1;
sound1 = mS         


        
相关标签:
2条回答
  • 2020-12-04 00:23

    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;
    }
    
    0 讨论(0)
  • 2020-12-04 00:31

    Use the code below it worked for me.

    MediaPlayer mp = new MediaPlayer();
    mp.setDataSource("/mnt/sdcard/yourdirectory/youraudiofile.wav");
    mp.prepare();
    mp.start();
    
    0 讨论(0)
提交回复
热议问题