0

I want to play music tracks with AudioTrack class in android, using stereo with PCM 16 bit channel. Here's my code for MusicListFragment.java

import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

public class MusicListFragment extends Fragment implements AdapterView.OnItemClickListener {

    private AudioTrack mAudioTrack;

    public MusicListFragment() {

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_music_list, container, false);

        ListView musicListView = (ListView) view.findViewById(R.id.music_list_view);
        musicListView.setAdapter(new MusicListAdapter(getActivity()));

        int minBufferSize = AudioTrack.getMinBufferSize(
                22000,
                AudioFormat.CHANNEL_OUT_STEREO, AudioFormat.ENCODING_PCM_16BIT);
        mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, 22000
                , AudioFormat.CHANNEL_OUT_STEREO, AudioFormat.ENCODING_PCM_16BIT, minBufferSize,
                AudioTrack.MODE_STREAM);

        musicListView.setOnItemClickListener(this);

        return view;
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Music music = (Music) adapterView.getItemAtPosition(i);
        InputStream inputStream = null;
        byte[] bytes = new byte[512];

        mAudioTrack.play();

        try {
            File file = new File(music.getPath());
            inputStream = new FileInputStream(file);
            int k;
            while ((k = inputStream.read(bytes)) != -1) {
                mAudioTrack.write(bytes, 0, k);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

The adapter works fine, since I tested it using the MediaPlayer class. (I can provide my Adapter and other classes too, if you want. But I doubt they are the issue.)
My list view shows the title, artist and the album of musics and also stores each music's path.
I could play musics easily with MediaPlayer, but I'm having problem using the AudioTrack. The code makes device play static sounds, like the old TVs when they have no signal! :)
As you can see in adapter's on click listener, I'm
1. getting the music that is selected.
2. reading the music file into an InputStream
3. and finally writing the bytes to the audio track instance. (I've also put the line: mAudioTrack.play() after the try, catch statement, no luck)
What am I doing wrong here?

Iman Irt
  • 145
  • 3
  • 12

2 Answers2

1

Playing the binary contents of a compressed audio file to the AudioTrack, perhaps? This won't work unless your music files are stored in raw, uncompressed format. AudioTracks use PCM format. Even the header on a .wav file would sound like static, until you reached the raw samples.

greeble31
  • 4,894
  • 2
  • 16
  • 30
1

Thanks to @greeble31 answer, I understand my issue now, I searched about how to decode .mp3 and .wav files to PCM format and I found some useful answers here and here , in case anyone needs it.

Community
  • 1
  • 1
Iman Irt
  • 145
  • 3
  • 12