Using MediaPlayer with a Timer - audio

I currently have a timer that I want to play a sound file when the timer ends. However it's only playing a fraction of audio file I have stored. I have nothing in the code that tells the file to stop playing, so I don't know why this is happening.
I start out with this:
public class DrinkinzActivity extends Activity {
/** Called when the activity is first created. */
Timer myTimer;
public MediaPlayer mMediaPlayer;
Then in the onCreate method I have this:
mMediaPlayer = new MediaPlayer();
mMediaPlayer = MediaPlayer.create(getBaseContext(), R.raw.soundfile);
Then when the timer runs out, I have this:
mMediaPlayer.start();
I haven't even gotten to the part where the user can pick which sound file to play, or use a ringtone instead. Is there something I'm missing?

Apparently I answered my own question. The audio file I was using was in wma format, and android (being Linux) didn't really like that. I reformatted it to mp3 and it works fine as of right now. It had nothing to do with the timer.

Related

Pre Spatializer Effect - Unity and Google VR Audio

I am using GVR Audio within unity to provide HRTF's for my audio sources, my project involves modelling the acoustics of the virtual environment which needs to happen before the HRTF filters.
On a default unity audio source there is an option to spatialise post effects, meaning I can insert my own effect there. However on the GVR Audio source there is no such option, what is the recomended way to spatialize post effects with GVR?
GvrAudioSource uses Unity's AudioSource under the hood. This means, it is possible to apply pre-spatialization processing using the OnAudioFilterRead method - as you'd normally write for audio sources in your script.
Alternatively, for other audio effect components that would require the spatializePostEffects option, you could simply enable the option through the script by adding the corresponding line below to Awake() function in GvrAudioSource.cs:
void Awake () {
...
audioSource.spatialBlend = 1.0f;
audioSource.spatializePostEffects = true; // Add this line.
OnValidate();
...
}
Please also note that, this unfortunately does not currently allow you to add Unity's stock AudioEffect components (e.g. AudioLowPassFilter) in the Editor, as it'd complain about the lack of an AudioSource component in that game object. This is, however, only a UI limitation, i.e., adding a component with such restrictions in run time should still work as expected.
Hope this answers your question.
Cheers

Play sound when other object(ball) hit cube

using UnityEngine;
using System.Collections;
public class audio : MonoBehaviour
{
public AudioClip hitsound;
void Update ()
{
}
void OnTriggerEnter2D (Collider2D other)
{
if (other.tag == "Ball")
{
GetComponent.<AudioSource>().PlayOneShot (hitsound);
}
}
}
I assign .mp3 file to inspector, and also, I added Audio Source component but I can't hear hit sound. Cubes which need to be destroyed is moving during game. I added that script and audio source component on parts which are not moving and when ball hit that non-moving parts, sound is playing (every time).
I hope that someone can help me with this.
Thanks and kind regards
YOU HAVE A TYPO
GetComponent.<
SHOULD BE
GetComponent<
Doesn't the . after GetComponent give you an error?
Anyway, make sure your colliders are set as triggers (checkbox on component).
Also I think Unity recommend using CompareTag() instead of ==.
It's worth putting a Debug.Log into the OnTriggerEnter2D to see if that is even firing.
Finally, make sure your colliders are the 2D versions, not just the regular colliders.

Muting 'Sound' instead of 'Music' in libgdx

I'm developing a simple game using eclipse and libgdx.
Currently, I'm using 'Music' instead of 'Sound' for sound effects for my game.
I made a button for muting all of the sound fx but having a problem when it comes to 'sound' instead of music.
Here's my current code:
public static Music jump;
public static void load() {
jump = Gdx.audio.newMusic(Gdx.files.internal("data/jump.wav"));
}
public static void muteFX() {
lesgo.setVolume(0);
public static void normalizeFX() {
jump.setVolume(1f);
//'muteFX' and 'normalizeFX' to be called on different class
I wanted to change it to 'Sound' (the reason is I wanted it to be more responsive on fast clicks,) So it could be like this:
public static Sound jump;
public static void load() {
jump = Gdx.audio.newSound(Gdx.files.internal("data/jump.wav"));
}
/* here comes my problem, didn't know how to set mute and normal
volume for the jump sound. I know there is also a set volume method
to 'Sound' but really confused on the terms (long soundID, float volume)
Can someone make this clear to me on how to implement the long and soundID?
*/
I'm really new to libgdx, as well as in java. I researched many forums regarding this and still can't find a more clearer explanation.
Any help will be greatly appreciated.
Thanks a lot in advance! =)
A quick suggestion would be to use a global variable for sound
public static VOLUME = 1.0f;
The sound api allows you to play a sound at a certain volume, so what you could do is have all the sounds in your game play at that global value when needed.
jump.play(VOLUME);
this way, all your switch will do is change the float value of volume.
public static void muteFX(){
VOLUME = 0.0f;
}
public static void normalizeFX(){
VOLUME = 1.0f;
}
It would be in your best interest to not use the Music class for sound effects due to memory constraints.
I hope this helps
In the documentation it states that the id is returned when play() or loop() methods succeed. This might not be convenient depending what are you trying to achieve. Basicly you can get the id and use it with something similar to this:
long id = sound.play(1.0f); // play new sound and keep handle for further manipulation
sound.stop(id); // stops the sound instance immediately
sound.setPitch(id, 2); // increases the pitch to 2x the original pitch
When I wanted to have a button Volume OFF and Volume ON I had a global variable which I would check before I play the sound somethign similar to this:
In my main Game Class:
public static boolean soundEnabled;
Whenever I wanted to play sound.
if (MyMainClass.soundEnabled)
sound.play();
In case you want any other control of the sound then inevetably you need to get the sound's id.

requestCastDeviceForRoute in onRouteAdded

I'm developing app for Chromecast. I'v tried Android Sample from https://github.com/googlecast/cast-android-sample, and he works fine - I may click on Cast icon, select device and start streaming. So, MediaRouter.Callback looks like this:
private class MyMediaRouterCallback extends MediaRouter.Callback {
#Override
public void onRouteSelected(MediaRouter router, RouteInfo route) {
MediaRouteHelper.requestCastDeviceForRoute(route);
}
}
And it works. But I want to start streaming on my device without stream button. So I'v changed this callback to:
private class MyMediaRouterCallback extends MediaRouter.Callback {
#Override
public void onRouteAdded (MediaRouter router, RouteInfo route)
MediaRouteHelper.requestCastDeviceForRoute(route);
}
}
But this doesn't work. This callback fires, requestCastDeviceForRoute returns true with required device (I see it by route.getName()), but onDeviceAvailable from CastSampleActivity never called. I'v tried delayed call of requestCastDeviceForRoute using Handler.postDelayed, and still nothing.
So, how to use requestCastDeviceForRoute properly to stream on Chromecast device without Media Button, right after he will be detected in MediaRouter.Callback?
Try calling
mediaRouter.selectRoute(route);
instead of using the MediaRouterHelper. This has worked for me when calling from within onRouteAdded()
Just to make sure I understand your question, are you trying to start streaming automatically as soon as the system "discovers" a cast device, without user clicking on the cast icon and selecting the device? Or are you trying to start streaming of the media immediately after user clicks on the cast icon and selects a target (i.e. removing the need to click on the Play button)?
The recommended flow is to let user choose the device manually, so what you are trying to do breaks the flow completely. What would user expect to happen if there is more than one chromecast device on the network? So it is strongly recommended not to go down that route.

How do I play a sound file in Java ME on Samsung mobile phones?

File formats I would like to play include .wav, .mp3, .midi.
I have tried using the Wireless Toolkit classes with no success. I have also tried using the AudioClip class that is part of the Samsung SDK; again with
If this device supports audio/mpeg you should be able to play mp3 use this code inside your midlet...
This works on my nokia symbian phones
// Code starts here put this into midlet run() method
public void run()
{
try
{
InputStream is = getClass().getResourceAsStream("your_audio_file.mp3");
player = Manager.createPlayer(is,"audio/mpeg");
// if "audio/mpeg" doesn't work try "audio/mp3"
player.realize();
player.prefetch();
player.start();
}
catch(Exception e)
{}
}
As for emulators my nokia experience is that I couldn't make it emulate mp3 player but when I put application on phone it works...
Without source code to review, I would suggest using the wireles toolkit (from http://java.sun.com)first.
It contains the standard J2ME emulator for windows and example code that will allow you to play a wav file.
assuming that works OK for you, try the same code on your Samsung device (of course, the location of the wav file will probably change so you have a tiny modification to make in the example code).
Assuming that works, compare your code that doesn't with the example code.

Resources