package com.example.kankanla.start;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(this, "AudioTrack.demo", Toast.LENGTH_LONG).show();
new Thread(new Runnable() {
@Override
public void run() {
try {
AP();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
protected void AP() throws IOException {
int sampleRateInHz = 44100;
int buferSize = AudioTrack.getMinBufferSize(
sampleRateInHz,
AudioFormat.CHANNEL_IN_STEREO,
AudioFormat.ENCODING_PCM_16BIT);
AudioTrack audioTrack = new AudioTrack(
AudioManager.STREAM_MUSIC,
sampleRateInHz,
AudioFormat.CHANNEL_IN_STEREO,
AudioFormat.ENCODING_PCM_16BIT,
buferSize, AudioTrack.MODE_STREAM);
FileInputStream fileInputStream = new FileInputStream(new File(Environment.getExternalStorageDirectory().toString(), "PCM-D50_441kHz16bit.wav"));
byte[] buffer = new byte[buferSize];
int lens = 0;
audioTrack.play();
while ((lens = fileInputStream.read(buffer)) != -1) {
audioTrack.write(buffer, 0, lens);
}
System.out.println(audioTrack.getPlayState());
audioTrack.stop();
System.out.println(audioTrack.getPlayState());
System.out.println(Thread.currentThread().getName());
// public static final int PLAYSTATE_STOPPED = 1; // matches SL_PLAYSTATE_STOPPED
// /** indicates AudioTrack state is paused */
// public static final int PLAYSTATE_PAUSED = 2; // matches SL_PLAYSTATE_PAUSED
// /** indicates AudioTrack state is playing */
// public static final int PLAYSTATE_PLAYING = 3; // matches SL_PLAYSTATE_PLAYING
}
}
0 件のコメント:
コメントを投稿