I just solved this problem yesterday
Instead of direct access to setDataSource, specify the path to the file
try { Log.d("SetDatasource path", playRecordPath); File filePath = new File(playRecordPath); if (!filePath.exists()) { filePath.createNewFile(); } FileInputStream is = new FileInputStream(filePath); mMediaRecordingPlayer.setDataSource(is.getFD()); mMediaRecordingPlayer.prepare(); is.close(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
And then in the onPrepared method
@Override public void onPrepared(MediaPlayer mp) { mMediaRecordingPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mMediaRecordingPlayer.setVolume(1.5f, 1.5f); mMediaRecordingPlayer.start(); duration = mMediaRecordingPlayer.getDuration(); mSeekBarPlayer.setMax(duration); mSeekBarPlayer.postDelayed(onEverySecond, 1000); }
when initializing the media player do this
mMediaRecordingPlayer = new MediaPlayer(); mMediaRecordingPlayer.setOnPreparedListener(this);
And in your work you must realize this
public class MainActivity extends Activity implements OnPreparedListener
source share