Yanshof
Yanshof

Reputation: 9926

fail to use MediaPlayer object to play some mp3 file

i do same as api guides -

I just have some file in and the file path - and i want to play it.

The exception is : /storage/emulated/0/111.mp3 (Permission denied)

I already add all permission

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

The code:

public class PlayingModule {

    private MediaPlayer _mediaPlayer;
    private Activity _content;

    public PlayingModule(Activity content) {

 //        this._mediaPlayer = new MediaPlayer();
        this._content = content;
}

    public void PlayMediaFile(URI mediaFilePath) throws IOException {


    Uri auri = Uri.parse(mediaFilePath.toString());

    _mediaPlayer = MediaPlayer.create( _content , auri);

    _mediaPlayer.stop();
    _mediaPlayer.reset();
    _mediaPlayer.release();

    _mediaPlayer = MediaPlayer.create( _content , auri);

    _mediaPlayer.start();
}

}

Call this class and play method :

  public void play() throws IOException {
    File path = Environment.getExternalStorageDirectory();
    File file = new File(path + "/111.mp3");

    PlayingModule pm = new PlayingModule(this);
    pm.PlayMediaFile(file.toURI());

Can't understand why its not working

I also change the code to this code - ... and still does not work ..

    Uri uri = Uri.parse(mediaFilePath.toString());

    MediaPlayer mediaPlayer = new MediaPlayer();
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    mediaPlayer.setDataSource(_content.getApplicationContext(), uri);
    mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

        @Override
        public void onPrepared(MediaPlayer mp) {
            mp.start();
        }
    });
    mediaPlayer.prepareAsync();

Upvotes: 0

Views: 167

Answers (1)

Swati
Swati

Reputation: 1179

You may check if MediaPlayer is prepared.

You need to set the OnPreparedListener. Once MediaPlayer is prepared, you will get call in OnPrepared() where you must call mediaplayer.start()

If start() api is called in any invalid state(), it will give an exception.

In order to check for any error occurred, you may also register for onErrorListener.

Adding Runtime permission -

public void checkPermissionGranted() {
    if (Build.VERSION.SDK_INT >= 23) {
        if (!(checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)) {
            requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 0);
        }
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (!(grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
        Log.d(TAG, "Storage Permission not granted");   
    } else {
        // Permission granted - resume
    }
}

Upvotes: 1

Related Questions