I am trying to develop an application which plays one of the predefined arrays of sounds continuously and also change the the tempo,pitch (individually) etc using soundpool class of android.
I know how to change the pitch of a sound.
But I do not know how to play the whole array and also the tempo of that whole array.
Please help!
Thank you in advance!
EDIT : I think I will have to use Handler in order to play the array of sounds, but i don't know how!
public class Sound {
private static int length = 22050 * 10; //10 seconds long
private static byte[] data = new byte[length];
static void fillRandom() {
new Random().nextBytes(data); //Create some random noise to listen to.
}
static void play() {
fillRandom();
final int TEST_SR = 22050; //This is from an example I found online.
final int TEST_CONF = AudioFormat.CHANNEL_OUT_MONO;
final int TEST_FORMAT = AudioFormat.ENCODING_PCM_16BIT;
final int TEST_MODE = AudioTrack.MODE_STATIC; //I need static mode.
final int TEST_STREAM_TYPE = AudioManager.STREAM_ALARM;
AudioTrack track = new AudioTrack(TEST_STREAM_TYPE, TEST_SR, TEST_CONF, TEST_FORMAT, length, TEST_MODE);
track.write(data, 0, length);
track.play();
}
}
This code play some random noises. All you have to do is fill the byte[]data array with the proper music files you intend to play...
Related
I'm working on a recorder app on iOS and a friend of mine he's doing it on Android, but when he want's to play my url sound which I send to server and which also works fine for me when I play it .. he hears a lot of noise and not my actual recorder.
func setupRecorder() {
let myID = userDefaults.value(forKey: "user_id") as! Int
let message_token = generateUniqueToken()
self.curentFileName = message_token
let currentFileName = "\(String(describing: self.curentFileName!))"+"."+"\(myID).m4a"
let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
self.soundFileURL = documentsDirectory.appendingPathComponent(currentFileName)
print("writing to soundfile url: '\(soundFileURL!)'")
if FileManager.default.fileExists(atPath: soundFileURL.absoluteString) {
print("soundfile \(soundFileURL.absoluteString) exists")
}
let recordSettings: [String : Any] = [AVFormatIDKey: kAudioFormatAppleLossless,
AVEncoderAudioQualityKey: AVAudioQuality.max.rawValue,
AVEncoderBitRateKey: 320000,
AVNumberOfChannelsKey: 2,
AVSampleRateKey: 44100.0]
do {
soundRecorder = try AVAudioRecorder.init(url: soundFileURL, settings: recordSettings)
soundRecorder.delegate = self
soundRecorder.isMeteringEnabled = true
soundRecorder.prepareToRecord()
}
catch {
soundRecorder = nil
print(error.localizedDescription)
}
soundRecorder.prepareToRecord()
}
This is my recorder settings....
What I have from him it s only
private static final int SAMPLE_PER_SEC = 8000;
private static final int CHANNEL_TYPE = AudioFormat.CHANNEL_IN_MONO;
private static final int AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;
private static int BUFFER_ELEMENT_TO_REC = 1024; // want to play 2048 (2K) since 2 bytes we use only 1024
private static int BYTES_PER_ELEMENT = 2;
ENCODING_PCM_16BIT
He said I m encoding my sound somehow and he s doing a raw record...I have no ideea where i m messing it since it s my first time with AvFoundation
Instead of using kAudioFormatAppleLossless change that to kAudioFormatLinearPCM. kAudioFormatAppleLossless format is a format only for Apple devices. It is better to use a common format for Android and iOS
devices, so that if any processing in the server for the audio, server doesn't have to handle the audio in two different way
I am using the new MIDI API in order to play some MIDI notes. However, I am unable to hear any sound, nor any exception is being thrown. The code for the same is as follows:
//initialising the MidiReceiver
private MidiReceiver midiReceiver;
midiReceiver = new MidiReceiver() {
#Override
public void onSend(byte[] msg, int offset,
int count, long timestamp) throws IOException {
}
};
/*Then in my loop containing note_on or note_off events*/
byte[] buffer = new byte[32];
int numBytes = 0;
int channel = 2; // MIDI channels 1-16 are encoded as 0-15.
// NOTE_STATUS is either 0x90 or 0x80
buffer[numBytes++] = (byte)(NOTE_STATUS + (channel - 1));
buffer[numBytes++] = (byte)noteValue; // the required MIDI pitch
buffer[numBytes++] = (byte)127; // max velocity
int offset = 0;
midiReceiver.send(buffer, offset, numBytes);
What am I doing wrong here? I think it must be because the onSend method is empty. How do I use it in order to make my app play back the note(s) within the Android device?
I couldn't find any indication in the documentation that the new MIDI API actually let you synthesize audio, so, it seems you need to generate the sound yourself.
Maybe this library might be useful.
Solved: I forgot the track.play(); at the end...
I want to play a sound on my Android Smartphone (4.0.4 Api level 15).
I tried to hear some random noise, but its not working:
public class Sound {
private static int length = 22050 * 10; //10 seconds long
private static byte[] data = new byte[length];
static void fillRandom() {
new Random().nextBytes(data); //Create some random noise to listen to.
}
static void play() {
fillRandom();
final int TEST_SR = 22050; //This is from an example I found online.
final int TEST_CONF = AudioFormat.CHANNEL_OUT_MONO;
final int TEST_FORMAT = AudioFormat.ENCODING_PCM_16BIT;
final int TEST_MODE = AudioTrack.MODE_STATIC; //I need static mode.
final int TEST_STREAM_TYPE = AudioManager.STREAM_ALARM;
AudioTrack track = new AudioTrack(TEST_STREAM_TYPE, TEST_SR, TEST_CONF, TEST_FORMAT, length, TEST_MODE);
track.write(data, 0, length);
}
}
I have played a little bit with the variabels, but could not get it to work.
All you have left to do is play it. Add this line to the end of your play() function:
track.play();
I tried to encode an mp3 sound in android device so I used lame encoder, it was successful but there are some settings and parameters for encoding like sample rate, bitrate and etc, I searched to find what they are and what choices is possible but I do not find something good, can anyone help me with them?
Here they are:
public static final int NUM_CHANNELS = 1;
public static final int SAMPLE_RATE = 16000;
public static final int BITRATE = 128;
public static final int MODE = 1;
public static final int QUALITY = 2;
To find out more about these parameters, take a look at http://en.wikipedia.org/wiki/MP3 first. You may need to check more specific details on mp3 codec documentation, too.
I am using the ToneGenarator in order to play a continuous beep.
The problem is that the volume is far too low!
We can't hear anything when the volume is lower than 8.
And with the maximum volume, it is not loud at all...
Is it a limitation of the DTMF sound or am I doing something wrong?
Here is the code:
private final int TONE_TYPE = ToneGenerator.TONE_DTMF_5;
private final int STREAM = AudioManager.STREAM_MUSIC;
private final int DOT_TIME = 3;
public SoundManager(Activity anActivity) {
audio = (AudioManager) anActivity
.getSystemService(Context.AUDIO_SERVICE);
generator = new ToneGenerator(STREAM,
audio.getStreamMaxVolume(STREAM));
}
private void playBeep() {
generator.startTone(TONE_TYPE, DOT_TIME);
}
Use this one, For using Current System Volume.
ToneGenerator toneG = new ToneGenerator(AudioManager.STREAM_SYSTEM, 100);
I found where the problem was:
ToneGenerator takes a volume between 0 and 100, while the stream volume is between 0 and 15.
Thus, audio.getStreamMaxVolume(STREAM) gives 15 out of 100, it is low...