多张图片混编 android,Android:混合多个AudioTrack实例?

小编典典

我有4个音轨同时播放,它们似乎播放得很好。在HTC Desire 1.1ghz

OC上进行测试。我有时会在穿线时遇到小故障。有时,如果所有四个都在播放,则在我尝试加入该线程时不会停止。需要做更多的测试。这是我的课程,用于播放在给定路径下记录的WAV文件

package com.ron.audio.functions;

import java.io.BufferedInputStream;

import java.io.DataInputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.InputStream;

import android.media.AudioFormat;

import android.media.AudioManager;

import android.media.AudioTrack;

public class AudioPlayManager implements Runnable {

private File fileName;

private volatile boolean playing;

public AudioPlayManager() {

super();

setPlaying(false);

}

public void run(){

// Get the length of the audio stored in the file (16 bit so 2 bytes per short)

// and create a short array to store the recorded audio.

int musicLength = (int)(fileName.length()/2);

short[] music = new short[musicLength];

try {

// Create a DataInputStream to read the audio data back from the saved file.

InputStream is = new FileInputStream(fileName);

BufferedInputStream bis = new BufferedInputStream(is);

DataInputStream dis = new DataInputStream(bis);

// Read the file into the music array.

int i = 0;

while (dis.available() > 0) {

music[i] = dis.readShort();

i++;

}

// Close the input streams.

dis.close();

// Create a new AudioTrack object using the same parameters as the AudioRecord

// object used to create the file.

AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,

11025,

AudioFormat.CHANNEL_CONFIGURATION_MONO,

AudioFormat.ENCODING_PCM_16BIT,

musicLength,

AudioTrack.MODE_STREAM);

// Start playback

audioTrack.play();

// Write the music buffer to the AudioTrack object

while(playing){

audioTrack.write(music, 0, musicLength);

}

}

catch(Exception e){

e.printStackTrace();

}

}

public void setFileName(File fileName) {

this.fileName = fileName;

}

public File getFileName() {

return fileName;

}

public void setPlaying(boolean playing) {

this.playing = playing;

}

public boolean isPlaying() {

return playing;

}

}

2020-10-20

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值