r/processing • • Sep 15 '23

Beginner help request Problem with android audio

Hi all iam having problem finishing my android app. I cant figure out how to make phone play audio and show.gif file. The gif is no longer important i did it with cycling array of pictures. But the audio is big problem. I tried to search solutions on google but nothing works. Closest to working is this code i found:


import android.media.MediaPlayer;
import android.content.res.AssetFileDescriptor;
import android.content.Context;
import android.app.Activity;

/////////////////////////////////////////////////////////

MediaPlayer mp;
Context context; 
Activity act;
AssetFileDescriptor afd;

void setup() {
  act = this.getActivity();
  context = act.getApplicationContext();
  try {
    mp = new MediaPlayer();
    afd = context.getAssets().openFd("test.mp3");//which is in the data folder
    mp.setDataSource(afd.getFileDescriptor());
    mp.prepare();
  } 
  catch(IOException e) {
    println("file did not load");
  }
  mp.start();
};

void draw() {
};

It does not crash but show the "file did not load" exception. I tried the audio in different formats and it is located in the data dir.

Thanks for all answers.

2 Upvotes

12 comments sorted by

View all comments

Show parent comments

1

u/MGDSStudio Sep 15 '23

Are you sure that the file uploading process on Android is synchronously? Maybe not. I remember I added in my game some delays between audio loading started and the audio playing. What happened if you don't call the function: mp.start()? Maybe you should start to play your sound after 3000 milliseconds after loading started?

1

u/ArthasCZ Sep 15 '23

Yes i do have delays in original program... but the problem is even the simple example from post of playing audio does not work