I have a Sound loaded in the onCreateResources method.
try {
shield = MusicFactory.createMusicFromAsset(this.getMusicManager(), this, "shield.ogg");
} catch (IOException e) {
e.printStackTrace();
}
shield.setVolume(1.0f);
shield.setLooping(true);
When i play the sound the first time it works fine, however, the following times i play it is seems random if it works or not.
i use shield.play(); to play the sound and shield.stop(); to stop it again.
When it doesnt work i get the following in the log.
AudioFlinger could not create track, status: -12
Error creating AudioTrack
Using Music when you play background music, it will be control long play, repeat..
Using Sound when you play a short audio, example when press button, or shoot..etc
Here the my same code using both of them:
public static void controlBgAudio(boolean play, boolean repeat) {
Music ms = ResourceManager.getInstance().audioBgMusic;
if (play) {
if (ms.isReleased()) {
ms.resume();
} else {
ms.play();
}
} else {
if (ms.isPlaying()) {
ms.pause();
} else {
ms.stop();
}
}
if (repeat) {
ms.setLooping(true);
} else {
ms.setLooping(false);
}
}
public static void controlShootAudio(boolean play, boolean repeat) {
Sound ms = ResourceManager.getInstance().audioShoot;
if (play) {
if (ms.isReleased()) {
ms.resume();
} else {
ms.play();
}
} else {
if (ms.isLoaded()) {
ms.pause();
} else {
ms.stop();
}
}
if (repeat) {
ms.setLooping(true);
} else {
ms.setLooping(false);
}
}
You don't use the MusicFactory for sound effects. It is strictly for music.
You are supposed to use the SoundFactory.
Here is an example:
Sound sound = SoundFactory.createSoundFromAsset(getSoundManager(), this, "sound.wav");
Related
I am building a video chatting app via the Linphone SDK.
There is an issue that when someone "receives" a video call, the loud speaker is off by default, so users need to use the phone speaker, the one used for phone call, rather than the loud speaker. However, at the same time, the one who gives the call has the loud speaker on by default.
LinphoneManager.getInstance().routeAudioToSpeaker();
I thought this is the code for which Linphone turns on loud speaker, but actually it's not.
How do I turn on loud speaker when users receive video calls by default?
LinphoneCore has two handy method for that:
enableSpeaker(boolean)
muteMic(boolean)
Just create helper functions inside LinphoneManager:
public void enableVoice() {
getLc().muteMic(false);
getLc().enableSpeaker(true);
}
public void disableVoice() {
getLc().muteMic(true);
getLc().enableSpeaker(false);
}
If you don't have an access to LinphoneManager, then the functions above should call:
LinphoneManager.getLc().{method_call};
private AudioManager mAudioManager;
...
public LinphoneMiniManager(Context c) {
mContext = c;
mAudioManager = ((AudioManager) c.getSystemService(Context.AUDIO_SERVICE));
mAudioManager.setSpeakerphoneOn(true);
...
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
....
btnSpeaker.setOnClickListener {
mIsSpeakerEnabled = !mIsSpeakerEnabled
it.isSelected = mIsSpeakerEnabled
toggleSpeaker()
}
....
}
private fun toggleSpeaker() {
val currentAudioDevice = core.currentCall?.outputAudioDevice
val speakerEnabled = currentAudioDevice?.type == AudioDevice.Type.Speaker
for (audioDevice in core.audioDevices) {
if (speakerEnabled && audioDevice.type == AudioDevice.Type.Earpiece) {
core.currentCall?.outputAudioDevice = audioDevice
return
} else if (!speakerEnabled && audioDevice.type == AudioDevice.Type.Speaker) {
core.currentCall?.outputAudioDevice = audioDevice
return
}/* If we wanted to route the audio to a bluetooth headset
else if (audioDevice.type == AudioDevice.Type.Bluetooth) {
core.currentCall?.outputAudioDevice = audioDevice
}*/
}
}
I am attempting to write a RemotePlaybackClient sample app, in part because the one published by Google crashes aapt.
I can get RemotePlaybackClient to support play(), and it plays back a video on a Chromecast.
However, when I call stop(), to stop playback of the video, while the Chromecast does stop playback (showing a black screen with a cast icon centered), the SessionActionCallback that I pass into the stop() call does not get called with onResult():
private void stop() {
logToTranscript(getActivity().getString(R.string.stop_requested));
SessionActionCallback stopCB=new SessionActionCallback() {
#Override
public void onResult(Bundle data, String sessionId,
MediaSessionStatus sessionStatus) {
logToTranscript(getActivity().getString(R.string.stopped));
isPlaying=false;
isPaused=false;
getActivity().supportInvalidateOptionsMenu();
endSession();
}
};
client.stop(null, stopCB);
}
The same thing happens if I try pause() -- the SessionActionCallback passed to pause() is not invoked.
The sample code published by Google shows that these callbacks should be invoked, but, again, I can't get that to compile successfully.
Does anyone know under what circumstances the SessionActionCallback would not work, while the ItemActionCallback used with play() would work?
UPDATE
I have filed issue 66996 and issue 67032, the latter of which is specifically the problem I am seeing here, as I run into this same problem with the official sample app.
I beleive all Answer reside on how you make connection.
Because in google code ,code says that client which you had made shold not leave session and should not be null.
if (!mClient.hasSession()) {
// ignore if no session
return;
}
/*********Rest of the code would be unreachable***********/
#Override
public void pause() {
if (!mClient.hasSession()) {
// ignore if no session
return;
}
if (DEBUG) {
Log.d(TAG, "pause");
}
mClient.pause(null, new SessionActionCallback() {
#Override
public void onResult(Bundle data, String sessionId, MediaSessionStatus sessionStatus) {
logStatus("pause: succeeded", sessionId, sessionStatus, null, null);
if (mCallback != null) {
mCallback.onPlaylistChanged();
}
}
#Override
public void onError(String error, int code, Bundle data) {
logError("pause: failed", error, code);
}
});
}
Well I'm working with ball game , Every thing is working fine , Now i want to add Sound when the two balls collide each other .
I tried the following code but the sound is being repeated.I want it will play only once when collision start & not at collision maintained.
In onCreateResources:
SoundFactory.setAssetBasePath("sfx/");
try {
mSound = SoundFactory.createSoundFromAsset(getSoundManager(), this, "coll2.m4a");
} catch (IOException e) {
e.printStackTrace();
}
And added below code in onAccelerationChanged(AccelerationData pAccelerationData)
if (face.collidesWith(face1) || face.collidesWith(face2))
{
mSound.play();
}
You could have a boolean to check if the sprites have already collided. The code would look something like this.-
if (face.collidesWith(face1) || face.collidesWith(face2)) {
if (!colliding) {
colliding = true;
mSound.play();
}
} else {
colliding = false;
}
I'm testing playing online video using chromecast.
After onRouteSelected(), I create the ApplicationSession and attach a MediaProtocalMessageStream;
Then I called mSession.startSession(); with no APP_ID, so I assume the build-in app inside chromecast play the video for me. This code works perfect and I can play online mp4 videos without writing my own receiver.
But, When I try to leave the video play app, I can't go back anymore, there is always an error message comes from onSessionStartFailed() which says
StartSessionTask failed with error: failed to start application: no
application is running
I don't remember how the first time I got into the video play app, which I don't leave for few day.
But I do know how I leave it, Here is what I did before I can never startSession again:
open Youtube app, get a deviced connected
play some youtube videos
disconnected from a chormecast, then the chromecast return to the starting page
So, doesn't anybody know what's going on here? How to open the build-in video app again?
By the way, My chromecast get a system update just after I return to the starting page, I don't know if google update something cause startSession() fail.
Below is the code I startSession and attach a mediaStream.
mSession = new ApplicationSession(mCastContext, mSelectedDevice);
ApplicationSession.Listener listener = new ApplicationSession.Listener() {
#Override
public void onSessionStarted(ApplicationMetadata appMetadata) {
mChannel = mSession.getChannel();
mStream = new MediaProtocolMessageStream();
mChannel.attachMessageStream(mStream);
if (mStream.getPlayerState() == null) {
ContentMetadata metaData = new ContentMetadata();
metaData.setTitle("Test Video");
String url = "http://www.auby.no/files/video_tests/h264_720p_hp_5.1_6mbps_ac3_planet.mp4";
try {
mCommand = mStream.loadMedia(url, metaData, true);
mCommand.setListener(new MediaProtocolCommand.Listener() {
#Override
public void onCompleted(MediaProtocolCommand arg0) {
onSetVolume(0.5);
}
#Override
public void onCancelled(MediaProtocolCommand arg0) {
}
});
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
#Override
public void onSessionStartFailed(SessionError error) {
Log.d("TEST", "Session Started failed");
}
#Override
public void onSessionEnded(SessionError error) {
Log.d("TEST", "Session Started end");
}
};
mSession.setListener(listener);
try {
mSession.startSession();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
You will have to use your own app id and own receiver. Google's default receiver doesn't play video streams anymore (it used to). It only handles Chrome tab mirroring now.
I wrote a code to record audio of call conversation using MediaRecorder.
how can i know whether a MediaRecorder is in running state or not, to stop the recording.
like
boolean running;
MediaRecorder mr;
//what should i assign to running?
if(running){
mr.stop()
}
Above code is just an example.. If you do not understand my question, please tell me.. i will explain clearly with actual code..
What all i want to know is "In which state the MediaRecorder is?" -> recording/released/prepared/initial/etc..
You cannot get the state directly, see the open enhancement request at http://code.google.com/p/android/issues/detail?id=800
You need to set a variable manually in the listeners when the mediaplayer reaches a certain state in order to remember the current state.
Also this this discussion:
http://www.mail-archive.com/android-developers#googlegroups.com/msg35320.html
String flag = "0";
public void audioRecordStart(){
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
try {
if (flag.equals("1")){
// It means recorder is already on recording state.
}
else{
myAudioRecorder.prepare();
myAudioRecorder.start();
flag = "1";
}
} catch (IllegalStateException ise) {
// make something ...
} catch (IOException ioe) {
// make something
}
Toast.makeText(getApplicationContext(), "Recording started", Toast.LENGTH_LONG).show();
}
else {
getAudioPermission();
}
}
public void audioRecordStop() {
if (flag.equals("0")){
// It means recorder is already stopped state.
}
else {
myAudioRecorder.stop();
myAudioRecorder.release();
myAudioRecorder = null;
flag = "0";
Toast.makeText(getApplicationContext(), "Audio Recorder successfully", Toast.LENGTH_LONG).show();
}
}
const mediaRecorder = new MediaRecorder(...)
mediaRecorder.start();
console.log(mediaRecorder.state);
// Will return "recording"
// Other possible states are "inactive" and "paused"
if (mediaRecorder.state != "inactive") {
mediaRecorder.stop();
}
Reference:
https://developer.mozilla.org/en-US/docs/Web/API/MediaRecorder/state