Search code examples
javaandroidaudio

In android java the audio is not playing from "https://dictionaryapi.dev/"


I am creating a dictionary app in Android java using "https://dictionaryapi.dev/". I am fetching data by Retrofit, and using list Adapters to display my data. I am facing a problem in playing audio which is being fetched from mentioned API. Please help me to solve this problem, if the issue is with API so how can I solve it to play my audio? If I use an alternative link for audio how to link it to my other code?

At Debugging enter image description here

At Run: Onclicking at the audio icon this error appears on RUN: "E/MediaPlayerNative: error (1, -2147483648) E/MediaPlayer: Error (1,-2147483648)" enter image description here

The following appears in logcat E/AUDIO_ERROR: MediaPlayer exception: null E/MediaPlayerNative: error (1, -2147483648)

My relative code is as followed, where I tried adding different audio sources to check are other APIs are working or not. I came to conclude that YES! all files are working, and my audio is playing except "https://api.dictionaryapi.dev/media/pronunciations/en/{word}-us.mp3".

public class PhoneticAdapter extends RecyclerView.Adapter<PhoneticVH> {
    private final Context context;
    private List<Phonetic> phoneticList;
    private MediaPlayer player;

    public PhoneticAdapter(Context context, List<Phonetic> phoneticList) {
        this.context = context;
        this.phoneticList = phoneticList;
    }


    @NonNull
    @Override
    public PhoneticVH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new PhoneticVH(LayoutInflater.from(context).inflate(R.layout.phonetic_list_items, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull PhoneticVH holder, @SuppressLint("RecyclerView") int position) {
        holder.phonetic_text.setText(phoneticList.get(position).getText());
        holder.phonetic_audio.setOnClickListener(view -> {
//            String audioUrl = "https://" + phoneticList.get(position).getAudio();
            player = new MediaPlayer();
            try{
                AudioAttributes audioAttributes = new AudioAttributes.Builder()
                        .setUsage(AudioAttributes.USAGE_MEDIA)
                        .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
                        .build();

                player.setAudioAttributes(audioAttributes);
//                player.setDataSource("https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3");
//                player.setDataSource("https://www.learningcontainer.com/wp-content/uploads/2020/02/Kalimba.mp3");
//                player.setDataSource("https://www.learningcontainer.com/wp-content/uploads/2020/02/Kalimba-online-audio-converter.com_-1.wav");
//                player.setDataSource("https://www.learningcontainer.com/wp-content/uploads/2020/02/Sample-FLAC-File.flac");
//                player.setDataSource("https://www.learningcontainer.com/wp-content/uploads/2020/02/Sample-OGG-File.ogg");
                player.setDataSource("https://" + phoneticList.get(position).getAudio());
                player.setOnPreparedListener(mp -> {
                    mp.start();
                });
                player.setOnErrorListener((mediaPlayer, i, i1) -> {
                    Log.e("AUDIO_ERROR", "MediaPlayer error: " + i);
                    Toast.makeText(context, "Error: " + i, Toast.LENGTH_SHORT).show();
                    return false;
                });
                player.prepareAsync();
                player.prepare();
                player.start();
            }catch (Exception e){
                Log.e("AUDIO_ERROR", "MediaPlayer exception: " + e.getMessage());
                Toast.makeText(context, R.string.audio_fail, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public int getItemCount() {
        if (phoneticList == null) {
            return 0;
        }
        return phoneticList.size();
    }

    @SuppressLint("NotifyDataSetChanged")
    public void setData(List<Phonetic> phoneticList) {
        this.phoneticList = phoneticList;
        notifyDataSetChanged();
    }
}


Solution

  • Solution: There's no need to append 'https://' with the audio URL. On inspection of the variables, the audio variable already includes 'https://' in the URL.