android studio emulator volume stuck on low - android-studio

I was trying to use MediaPlayer and AudioManager to change the volume of an audio file using app buttons. I seem to have messed up my emulator. The volume setting of the emulator and of my speakers is maxed but I can barely hear the sound file. Audio from other applications are working normally. I tried clearing the data from the emulator but that had no effect. Is there some way to reset the volume settings? I was using the following suggestions from other posts.
AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
int currVolume= am.getStreamVolume(AudioManager.STREAM_MUSIC);
currVolume++; // to change the volume
int maxVolume = 50;
float log1=(float)(Math.log(maxVolume-currVolume)/Math.log(maxVolume));
yourMediaPlayer.setVolume(1f-log1, 1f-log1);
Every time I executed this code on the emulator, the volume got lower and I can't bring it back up, no matter what I put in .setVolume()

The following has brought the volume back up. The currentVolume was 3 whereas the maxVolume is 15. I'm still not sure how mediaPlayer.setVolume() works.
AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int currentVolume = audio.getStreamVolume(AudioManager.STREAM_MUSIC);
int maxVolume = audio.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
audio.setStreamVolume(AudioManager.STREAM_MUSIC, maxVolume, 0);

Related

how to programmatically increase/decrease volume using speech in Android

The goal here is to say "volume up" and for the volume to increase by 20% at a time by speaking that command for media playback. Not just to set it to max volume when the app starts. and conversely volume down. This is what I tried and it does not work.
AudioManager audioManager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
// Without volume control UI
To increase media player volume
audioManager.adjustVolume(AudioManager.ADJUST_RAISE,AudioManager.FLAG_PLAY_SOUND)
//To decrease media player volume
audioManager.adjustVolume(AudioManager.ADJUST_LOWER,AudioManager.FLAG_PLAY_SOUND)
My app uses Speech commands too launch apps I want to add the functionality of volume control and I'm a lil stuck, most of the code uses intents like similar to this format for basic commands like time, for instance, works as follows
if (command.indexOf("time") != -1) {
Date now = new Date();
String time = formatDateTime(this, now.getTime(),
FORMAT_SHOW_TIME);
speak("the time is " + time);
I would like it to raise or lower by 20% so I used the format of the following and can't get it to work Any help is appreciated..
else if (command.indexOf("Volume Up") !=-1) {
intent = new Intent(Intent.ACTION_VIEW,
intent.audioManager.adjustVolume(AudioManager.ADJUST_RAISE,AudioManager.FLAG_PLAY_SOUND));
}

D3D9 CreateDevice() fails on Windows 10 when creating a multihead device

When calling IDirect3D9::CreateDevice with the BehaviorFlags D3DCREATE_ADAPTERGROUP_DEVICE in order to create a fullscreen multihead device (with 2 or more monitors attached) the function returns D3DERR_INVALIDCALL, when running the application on Windows 10 (build 1511, or build 10240).
The same code works fine on Windows 7 (on a multitude of different machines), and also on Windows 8.1 (with the latest updates). Also creating individual D3D9 devices (fullscreen) for each attached monitor to the graphics adapter works fine on Windows 10.
D3D9Ex by the way shows exactly the same behavior. Can anyone point me to a working D3D9 multihead example that works on Windows 10? Thanks!
I observed the exact same behaviour with "CreateDevice".
But when you use "CreateDeviceEx" it works ... well ... almost :-(.
You may now create the device and use it, but under some circumstances (especially if you use the same resolutions as the desktop already had) you wont see anything and "Present" will continuously return "S_PRESENT_MODE_CHANGED". But if you now switch the second monitor to some other resolution via ResetEx end then switch back to desktop resolution - voila it works. I put that on on a key I can press after initialization:
const int idx = 1;
int OldWidth = D3DPresPar[idx].BackBufferWidth;
int OldHeight = D3DPresPar[idx].BackBufferHeight;
D3DPresPar[idx].BackBufferWidth = 1280;
D3DPresPar[idx].BackBufferHeight = 720;
D3DDispMode[idx].Width = 1280;
D3DDispMode[idx].Height = 720;
FailCheck(pD3DDevice->ResetEx(D3DPresPar, D3DDispMode), "ResetEX");
D3DPresPar[idx].BackBufferWidth = OldWidth;
D3DPresPar[idx].BackBufferHeight = OldHeight;
D3DDispMode[idx].Width = OldWidth;
D3DDispMode[idx].Height = OldHeight;
FailCheck(pD3DDevice->ResetEx(D3DPresPar, D3DDispMode), "ResetEX");
And after pressing the key it suddenly works. Weird, eh?
I confirmed this behaviour on multiple computers with nvidia, amd and intel grafics adapters. So the bug seems to be on microsoft side.
Conclusion: Theoretically it should work but there is some annyoing bug in windows 10 multihead initialization.
With some weird tricks you can achieve what you want, but these tricks are just too weird to use in production.

Windows Phone Background Audio current track is resetting

I am developing an WP8 app witch uses background audio agent. i have taken the background audio players sample. i have added the following method to audioplayer.cs
public static void playTrackAtIndex(int index)
{
currentTrackNumber = index;
BackgroundAudioPlayer.Instance.Track = _playList[currentTrackNumber];
}
after it is called the song at the specified index (let's say 5) will play, but when i pres skipnext in my ap or in the UVC currentTrackNumber is 0!. Please, any help is apreciated
it turns out you don't have any control over the lifecycle of the background audio agent, so at any time it may be killed and then instanced by the foreground app or the Background audio player. So the only whey to make the agent work is to design it as it will always be killed and instanced (use sql lite or files with a lock, or always check what backgoundaudioplayer is playing when your agent is called, so your agent will "remember" where it was before getting killed

Best approach for playing a single tone audio file?

Can anyone tell me the best approach to playing single-tone, audio (.mp3) files in a Windows Phone 8 app? Think of a piano app, where each key would represent a button, and each button would play a different tone.
I'm looking for the most efficient way to go about this - I've got 8 different buttons that need to play a different tone when tapped.
I tried using the MediaElement:
MediaElement me;
public MainPage()
{
InitializeComponent();
me = new MediaElement();
me.AutoPlay = false;
me.Source = new Uri("/Sounds/Sound1.mp3", UriKind.Relative);
btnPlay.Click += btnPlay_Click;
}
private void btnPlay_Click(object sender, EventArgs e)
{
me.Play();
}
But nothing happens, either in the emulator or on a device (testing w/ a Lumia 822). Am I doing something wrong here? It seems like it should be pretty simple. Or would using MediaElement even be the best thing to use for my scenario?
Would this fall under the Background Audio category? I've read through this example but it seems overkill for what I want to do.
I've also read about using XNA's SoundEffect to do the job, but then I'd have to convert my .mp3 files to .wav (which isn't necessarily a problem, but I'd rather not go through that if I don't need to).
Can anyone tell me either what I'm doing wrong in my example above or guide me to a better solution for playing quick <1s audio tones?
I had this problem before with MediaElement not playing audio files. After many attempts I found out that it only plays if it defined in the xaml and AutoPlay is set to true.
Try defining it in the xaml or you can just add it to your LayoutRoot.
var me = new MediaElement();
LayoutRoot.Children.Add(me);
me.AutoPlay = true;
me.Source = new Uri("Sound/1.mp3", UriKind.Relative);
I have had good luck just doing this piece of code in my app. But it may not work as well in your context, give it a whirl though.
mediaElement.Source = new Uri("/Audio/" + songID.ToString() + ".mp3", UriKind.Relative);
mediaElement.Play();

IMFMediaPlayer hangs during SetSourceFromByteStream

Background: I'm coding a metro-styled app for Win8. I need to be able to play music-file. Because of quality and space requirements we're using encoded audio (mp3/ogg).
I'm using XAudio2 to play sound effects (.wav files), but since I couldn't figure out a way to play encoded audio with it, I decided to play the music files with Media Foundation (IMFMediaPlayer interface).
I downloaded metro apps sample, and found out that the Media Engine Native C++ video playback sample was closest to what I needed.
Now that my app has MediaPlayer playing musics, I ran into a problem. If the device running the app is slow enough, MediaPlayer hangs. When I'm running the release-version of the app on my device, it's fine and I can hear the music just fine. But when I attach the debugger or run it on a slower device, it hangs when I'm setting bytestream for the MediaPlayer to play.
Here's some code, you'll find it pretty similiar to the sample:
StorageFolder^ installedLocation = Windows::ApplicationModel::Package::Current->InstalledLocation;
m_pickFileTask = Concurrency::task<StorageFile^>(installedLocation->GetFileAsync(filename)), m_tcs.get_token());
auto player = this;
m_pickFileTask.then([player](StorageFile^ fileHandle)
{
player->SetURL(fileHandle->Path);
Concurrency::task<IRandomAccessStream^> fOpenStreamTask = Concurrency::task<IRandomAccessStream^> (fileHandle->OpenAsync(Windows::Storage::FileAccessMode::Read));
fOpenStreamTask.then([player](IRandomAccessStream^ streamHandle)
{
MEDIA::ThrowIfFailed(
player->m_spMediaEngine->Pause()
);
MEDIA::GetMediaError(player->m_spMediaEngine);
player->SetBytestream(streamHandle);
if (player->m_spMediaEngine)
{
MEDIA::ThrowIfFailed(
player->m_spEngineEx->Play()
);
MEDIA::GetMediaError(player->m_spMediaEngine);
}
}
);
}
);
And here's the SetBytestream method:
SetBytestream(IRandomAccessStream^ streamHandle)
{
if(m_spMFByteStream != nullptr)
{
m_spMFByteStream->Close();
m_spMFByteStream = nullptr;
}
MEDIA::ThrowIfFailed(
MFCreateMFByteStreamOnStreamEx((IUnknown*)streamHandle, &m_spMFByteStream)
);
MEDIA::ThrowIfFailed(
m_spEngineEx->SetSourceFromByteStream(m_spMFByteStream.Get(), m_bstrURL)
);
MEDIA::GetMediaError(m_spEngineEx);
return;
}
The line where it hangs is:
m_spEngineEx->SetSourceFromByteStream(m_spMFByteStream.Get(), m_bstrURL)
When I'm debugging the app, I can press pause and see the stack. Well, not much of it, but atleast I can see it that it's indefinitely at
ntdll.dll!77b7f4dc()
Any ideas why my app would hang in such a way?
(OPTIONAL: If you know a better way to play mp3/ogg in a c++ metro-styled app, let me know)
Could not figure out why this is happening, but I managed to code a work-a-round:
IMFSourceReader can be used to decode MP3s and feed bytes into XAudio2SourceVoice.
XAudio2 audio stream effect sample contains good example how to do this.

Resources