1

I want to play audio files which are stored on internal storage of my application from a user device: here below are the full code of extracting and showing list of those audio files=

//variables of retrieving audio file from internal storage
ListView listView_phone_calls;
ArrayList<File> phone_call_list;
ArrayAdapter<File> adapter;

//variables of playing an audio file
MediaPlayer player;

    listView_phone_calls=(ListView)findViewById(R.id.listView_phone_calls);
    phone_call_list=new ArrayList<>();

    File[] file=getApplicationContext().getFilesDir().listFiles();
    for (int i=0;i<file.length;i++){
        phone_call_list.add(file[i]);
    }

    adapter=new ArrayAdapter<>(PhoneCall_Player.this,android.R.layout.simple_list_item_activated_1,phone_call_list);
    listView_phone_calls.setAdapter(adapter);

    listView_phone_calls.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            //This is what should happen when a user clicks on a one item
            player=new MediaPlayer();
            File call_rec=(File) listView_phone_calls.getItemAtPosition(position);
            try {
                player.setDataSource(String.valueOf(call_rec));
            } catch (IOException e) {
                e.printStackTrace();
                Toast.makeText(PhoneCall_Player.this,"Sorry! File not found! "+e.getMessage(),Toast.LENGTH_SHORT).show();
            }catch (IllegalArgumentException e){
                e.printStackTrace();
                Toast.makeText(PhoneCall_Player.this,"Sorry! Something is wrong!, "+e.getMessage(),Toast.LENGTH_SHORT).show();
            }

            player.setAudioStreamType(AudioManager.STREAM_MUSIC);

            try {
                player.prepare();
            } catch (IOException e) {
                e.printStackTrace();
                Toast.makeText(PhoneCall_Player.this,"Player couldn't be prepared to play! "+e.getMessage(),Toast.LENGTH_SHORT).show();
            }
            player.start();

            player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    player.release();
                    player=null;
                }
            });
        }
    });

Whenever I click on an item, the toast of player couldn't be prepared to play keeps appearing; I think there is no problem with setDataSource(); so what is wrong with my codes. Is there any error that I am putting that I don't see? Or I am not calling MediaPlayer well?

Mugirase Emmanuel
  • 385
  • 1
  • 5
  • 14
  • You are throwing away valuable info. In every catch block you have a toast. Which is very good. But with every toast you should also display e.getMessage(). Add it and tell us what it tells. – greenapps May 20 '18 at 11:11
  • @greenaps I have just added and is saying: " Player couldn't be prepared to play! Prepare failed.:status=0x1 " – Mugirase Emmanuel May 20 '18 at 11:27
  • 1
    `player.setDataSource(String.valueOf(call_rec));`. Please tell us exactly which full path you indicate to the player to play from. – greenapps May 20 '18 at 11:31
  • @greenapps `String.valueOf(call_rec);` is returning /data/user/0/com.example.mypackagename/files/audio00001.mp3 that is the full path that it is getting on item1 – Mugirase Emmanuel May 20 '18 at 11:37
  • I wonder if the media player can play files from your apps private internal storage. Try with files in getExternalFilesDir() or getExternalStorageDirectory() to find out. – greenapps May 20 '18 at 11:42
  • @greenapps Those audiofiles are recorded by the same application, but I chose to save them in a private place which is accessed by the same application only. So I wonder how should I play them in the same application, if MediaPlayer can't really do that – Mugirase Emmanuel May 20 '18 at 11:52

1 Answers1

0

prefix your path with file:// so that your path becomes file:///data/user/0/com.example.mypackagename/files/audio00001.mp3

Sagar
  • 23,903
  • 4
  • 62
  • 62
  • can you try, `file.getAbsolutePath()` ? – Sagar May 20 '18 at 11:51
  • See how I did it: `File call_rec=(File) listView_phone_calls.getItemAtPosition(position); File file2=new File(call_rec.getAbsolutePath()); try { player.setDataSource("file//"+String.valueOf(file2)); } catch (IOException e) { e.printStackTrace(); Toast.makeText(PhoneCall_Player.this,"Sorry! File not found!"+e.getMessage(),Toast.LENGTH_SHORT).show();` and it keeps showing the same error with the same message the player couldn't be prepared – Mugirase Emmanuel May 20 '18 at 11:58
  • Thanks My brother; Now it is working successfully; I do really appreciate it; the problem was the file encodding in recording system. but also adding that `file://` helped me again. I do really appreciate your wonderfull answer – Mugirase Emmanuel May 20 '18 at 12:17
  • @De_Scholar great! Good to know the answer has helped you. Enjoy coding. – Sagar May 20 '18 at 12:19
  • Sorry. But prefixing with "file://" is not needed. Just a path will do. – greenapps May 20 '18 at 13:34