我想播放具有单声道和立体声效果的mp3。目前我正在制作播放立体声效果我已阅读所有文件,但我只得到白噪声。 我的代码是:
public class AudioTest extends Activity
{ byte[] b;
public void onCreate(Bundle savedInstanceState)
{
AndroidAudioDevice device = new AndroidAudioDevice( );
super.onCreate(savedInstanceState);
File f=new File("/sdcard/lepord.mp3");
try {
FileInputStream in=new FileInputStream(f);
int size=in.available();
b=new byte[size];
in.read(b);
in.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
while( true )
{
device.writeSamples(b);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
我的AndroidAudioDevice类是:
public class AndroidAudioDevice
{
AudioTrack track;
byte[] buffer = new byte[158616];
@SuppressWarnings("deprecation")
public AndroidAudioDevice( )
{
int minSize =AudioTrack.getMinBufferSize( 44100, AudioFormat.CHANNEL_OUT_STEREO, AudioFormat.ENCODING_PCM_16BIT );
track = new AudioTrack( AudioManager.STREAM_MUSIC, 44100,
AudioFormat.CHANNEL_OUT_STEREO, AudioFormat.ENCODING_PCM_16BIT,
158616, AudioTrack.MODE_STREAM);
Log.e(""," sizewe are using for track buffer is 158616");
track.setStereoVolume(.6f,.6f);
track.play();
}
public void writeSamples(byte[] b) {
// TODO Auto-generated method stub
Log.e("","bytes to be write in track is "+b.length );
fillBuffer(b);
track.write( buffer, 0, b.length );
}
private void fillBuffer( byte[] samples )
{
Log.e("","track buffer length="+buffer.length+" samle length"+samples.length );
if( buffer.length < samples.length )
buffer = new byte[samples.length];
for( int i = 0; i < samples.length; i++ )
buffer[i] = (byte)(samples[i] * Byte.MAX_VALUE);;
}
}
首先我没有声音而不是白噪声,我只想通过AudioTrack播放声音然后我会处理单声道和立体声音效 请帮我 在此先感谢。*