问题
I am working on an Android application that record the audio and play it with different frequency and voice properties. my code working:
It records,Stop and play the audio but not in desired format and values
just like Voice Changer With Effects
Basically this app record voice and play it with different properties like Ghost,Radio,Girl etc etc. I am using list view to achieve this. I want to change the properties of sound just like this application.
Here is code for main activity
public class MainActivity extends AppCompatActivity {
ArrayList<Model> datamodels;
ListView listView;
MyAdapter myAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView= (ListView) findViewById(R.id.listview);
datamodels= new ArrayList<>();
datamodels.add(new Model(R.drawable.ic_color_lens_black_24dp,"Normal",R.drawable.ic_play_arrow_black_24dp,11025,0,R.drawable.ic_more_vert_black_24dp,false));
datamodels.add(new Model(R.drawable.ic_spa_black_24dp,"Ghost",R.drawable.ic_play_arrow_black_24dp,16000, 128,R.drawable.ic_more_vert_black_24dp,false));
datamodels.add(new Model(R.drawable.ic_local_phone_black_24dp,"Telephone",R.drawable.ic_play_arrow_black_24dp,22050, 8000 ,R.drawable.ic_more_vert_black_24dp,false));
datamodels.add(new Model(R.drawable.ic_menu_camera,"Girl",R.drawable.ic_play_arrow_black_24dp,44100, 200,R.drawable.ic_more_vert_black_24dp,false));
datamodels.add(new Model(R.drawable.ic_android_black_24dp,"Cave",R.drawable.ic_play_arrow_black_24dp,200, -10453621,R.drawable.ic_more_vert_black_24dp,false));
datamodels.add(new Model(R.drawable.ic_menu_gallery,"Speaker",R.drawable.ic_play_arrow_black_24dp,-9079435, 4300,R.drawable.ic_more_vert_black_24dp,false));
datamodels.add(new Model(R.drawable.ic_menu_manage,"Anger",R.drawable.ic_play_arrow_black_24dp,-16121, 3100,R.drawable.ic_more_vert_black_24dp,false));
datamodels.add(new Model(R.drawable.ic_menu_send,"Under Water",R.drawable.ic_play_arrow_black_24dp,-12627531, 500, R.drawable.ic_more_vert_black_24dp,false));
datamodels.add(new Model(R.drawable.ic_menu_share,"Space",R.drawable.ic_play_arrow_black_24dp,-14575885, 1000,R.drawable.ic_more_vert_black_24dp,false));
datamodels.add(new Model(R.drawable.ic_menu_slideshow,"Robot",R.drawable.ic_play_arrow_black_24dp,-16743537, 2000,R.drawable.ic_more_vert_black_24dp,false));
datamodels.add(new Model(R.drawable.ic_mood_bad_black_24dp,"Optomus Prime",R.drawable.ic_play_arrow_black_24dp,-8825528, 400,R.drawable.ic_more_vert_black_24dp,false));
datamodels.add(new Model(R.drawable.ic_people_black_24dp,"Spinning",R.drawable.ic_play_arrow_black_24dp,-26624, 1400,R.drawable.ic_more_vert_black_24dp,false));
datamodels.add(new Model(R.drawable.ic_people_black_24dp,"Alien",R.drawable.ic_play_arrow_black_24dp,-141259, 4400,R.drawable.ic_more_vert_black_24dp,false));
datamodels.add(new Model(R.drawable.ic_people_black_24dp,"Small Alien",R.drawable.ic_play_arrow_black_24dp,-4142541, 3800,R.drawable.ic_more_vert_black_24dp,false));
datamodels.add(new Model(R.drawable.ic_spa_black_24dp,"Fan",R.drawable.ic_play_arrow_black_24dp,-16689253, 3900,R.drawable.ic_more_vert_black_24dp,false));
datamodels.add(new Model(R.drawable.ic_local_phone_black_24dp,"Old Radio",R.drawable.ic_play_arrow_black_24dp,-7461718, 3600,R.drawable.ic_more_vert_black_24dp,false));
datamodels.add(new Model(R.drawable.ic_local_phone_black_24dp,"Wolf",R.drawable.ic_play_arrow_black_24dp,-1754827, 3200,R.drawable.ic_more_vert_black_24dp,false));
datamodels.add(new Model(R.drawable.ic_local_phone_black_24dp,"Android",R.drawable.ic_play_arrow_black_24dp,-16746133, 3300,R.drawable.ic_more_vert_black_24dp,false));
datamodels.add(new Model(R.drawable.ic_local_phone_black_24dp,"Warning",R.drawable.ic_play_arrow_black_24dp,-1499549, 1500,R.drawable.ic_more_vert_black_24dp,false));
myAdapter = new MyAdapter(datamodels, this);
listView.setAdapter(myAdapter);
}
}
Code for model class
public class Model {
int image1;
String imagename1;
int ply;
int optn;
double freqancy;
double bit_rate;
boolean ischange;
public Model(int image1, String imagename1, int ply,double freqancy,int bit_rate, int optn,boolean ischange){
this.image1=image1;
this.imagename1= imagename1;
this.ply=ply;
this.optn=optn;
this.freqancy=freqancy;
this.bit_rate=bit_rate;
this.ischange=ischange;
}
public int getImage1() {
return image1;
}
public double getBit_rate() {
return bit_rate;
}
public double getFreqancy() {
return freqancy;
}
public boolean setIschange(boolean ischange) {
this.ischange = ischange;
return ischange;
}
public String getName() {
return imagename1;
}
}
Code for playing with different values
void playRecord(int freq, double bit_rate) throws IOException {
String folder_main = "MyVoiceChanger";
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + folder_main + "/Recording.mp3");
int shortSizeInBytes = Short.SIZE/Byte.SIZE;
int bufferSizeInBytes = (int)(file.length()/shortSizeInBytes);
short[] audioData = new short[bufferSizeInBytes];
try {
InputStream inputStream = new FileInputStream(file);
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
DataInputStream dataInputStream = new DataInputStream(bufferedInputStream);
int i = 0;
try {
while (dataInputStream.available() > 0) {
audioData[i] = dataInputStream.readShort();
i++;
}
}catch (java.io.EOFException e){
e.printStackTrace();
}
dataInputStream.close();
int sampleFreq = freq;
double b_rate=bit_rate;
AudioTrack audioTrack = new AudioTrack(
AudioManager.STREAM_MUSIC,sampleFreq,
AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_8BIT,
bufferSizeInBytes, AudioTrack.MODE_STREAM);
Toast.makeText(mContext, "Usm", Toast.LENGTH_SHORT).show();
mediaPlayer.prepare();
audioTrack.write(audioData, 0, bufferSizeInBytes);
audioTrack.play();
mediaPlayer.start();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
回答1:
The variety of sound/voice effects you have mentioned, can not be generated simply by modifying frequency/bitrate parameters. That would require deeper knowledge of audio signal processing.
Apart from that, the source you have written for playback seems incorrect. You are using both MediaPlayer
and AudioTrack
. Generally, for playback of a single audio file, one needs either one of them.
AudioTrack
gives you audio sample level access and control, and therefore great power. But, with great power comes great responsibility.
MediaPlayer
APIs are comparatively easier to implement, and I would recommend using it for playback. You may have a look at this source, which explains using media player for playback of audios which are displayed in the form of list (using RecyclerView
)
来源:https://stackoverflow.com/questions/49187801/unable-to-play-sound-with-different-values