Problems with Android when playing an audio file

I downloaded the sound files from the server and saved them using

/data/data/packagename/sounds/filename.mp3 

Then, if I play this sound with

 .MediaPlayer mp= new MediaPlayer(); try { mp.setDataSource(PATH+"/"+fileName); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } mp.start(); 

But he shows an error

 MediaPlayer:start called in state 2 MediaPlayer:Error(-38,0) MediaPlayer:Error(-38,0) 

What is the problem with this, I have searched a lot in this regard, but no solutions offer any solutions.

+4
source share
1 answer

Try this code,

 public void audioPlayer(String path, String fileName){ //set up MediaPlayer MediaPlayer mp = new MediaPlayer(); try { mp.setDataSource(path+"/"+filename.mp3); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { mp.prepare(); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } mp.start(); } 
+2
source

Source: https://habr.com/ru/post/1414711/


All Articles