-
大小: 3KB文件类型: .java金币: 1下载: 0 次发布日期: 2021-05-05
- 语言: Java
- 标签: AudioTrack wav
资源简介
读取wav文件,用static模式播放.注意需要添加权限,
代码片段和文件信息
package com.example.gentone;
import java.io.FileInputStream;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.media.AudioFormat;
import android.media.AudioTrack;
import android.media.ToneGenerator;
import android.media.AudioManager;
import android.widget.Button;
public class MainActivity extends Activity {
private Button btn_start;
private Button btn_stop;
private FileInputStream mFile;
private byte mBuffer[];
private short mLen;
AudioTrack mTrack;
ToneGenerator gen = new ToneGenerator(AudioManager.STREAM_MUSIC 50);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
mFile = new FileInputStream(“/sdcard/raw.wav“);
mBuffer = new byte[256 * 1024];
mLen = (short) mFile.read(mBuffer);
} catch (Exception e) {
Log.e(“Tom“ “Can‘t open /sdcard/raw.wav“ + “ “ + e.toString());
}
setContentView(R.layout.activity_main);
btn_start = (Button) findViewById(R.id.btn_start);
btn_stop = (Button) findViewById(R.id.btn_stop);
btn_start.setonclickListener(
new View.onclickListener() {
public void onclick(View v) {
//gen.startTone(ToneGenerator.TONE_DTMF_6);
if (mTrack == null && mFile != null) {
try {
Log.i(“Tom“ “file lenth is “ + mLen);
评论
共有 条评论