Android Media Player Error (1, -4) when playing sound from the Assets folder

I need your help. I tried to play the audio file stored in the Assets folder, but an error occurred.

Here is my code:

try{ if (player.isPlaying()) { player.stop(); player.release(); } }catch(Exception e){ Toast.makeText(this, "an exception occurred", Toast.LENGTH_LONG).show(); e.printStackTrace(); } try{ AssetFileDescriptor afd = BeeDailyConvo.this.getAssets().openFd("sounds/hello_kr.wma"); player.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength()); player.prepare(); player.start(); }catch(Exception e){ e.printStackTrace(); } 

And here is my logcat:

06-16 22: 39: 53.330: W / MediaPlayer (13490): information / warning (1, 26)
06-16 22: 39: 53.330: E / MediaPlayer (13490): error (1, -4)

Could you explain what is wrong with my code?

Thank you in advance

Hi,

Priska

+6
source share
5 answers

This issue has been solved.

The asset file descriptor must be closed before preparing the player. This is how I solved the problem:

 player = new MediaPlayer(); AssetFileDescriptor afd = BeeDailyConvo.this.getAssets() .openFd("sounds/"+file); player.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength()); afd.close();**//just added this line** player.prepare(); player.start(); 
+4
source

Here you can see all error codes Media player Error codes

A -4 error code indicates that you provided invalid arguments.

Put your code in a catch catch block.

Try using

  try { AssetFileDescriptor afd = CustomListViewActivity.this.getAssets() .openFd("sounds/hello_kr.wma"); player.setDataSource(afd.getFileDescriptor()); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } 
+3
source

Unfortunately, there is very little information about MediaPlayer error codes for some reason. However, I suggest you try putting your sound file in res / raw / instead of your assets.

EDIT:

Start here with the MediaPlayer section in the developer docs. This will show you how to properly set up and play sound.

EDIT 2:

which can do this from assets see this question: Play audio file from asset directory

+1
source

I do not think wma files are supported.

http://developer.android.com/guide/appendix/media-formats.html

I noticed that you did not specify audioStreamType

mediaPlayer.setAudioStreamType (AudioManager.STREAM_MISIC);

+1
source

use this method to solve your problem :)

  public void playBeep() { try { if (m.isPlaying()) { m.stop(); m.release(); m = new MediaPlayer(); } AssetFileDescriptor descriptor = getAssets().openFd("mp3 name.mp3"); m.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength()); descriptor.close(); m.prepare(); m.setVolume(1f, 1f); m.setLooping(true); m.start(); } catch (Exception e) { } } 
0
source

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


All Articles