Playing Stream Audio with MPMoviePlayerController in Background Modes - c#

my first question here...
I have been searching how solve this issue i start a app using monotouch creating my MPMoviePlayerController like this:
this.mp = new MPMoviePlayerController(new NSURL("http://stream3.dyndns.org:1935/iphone/xeco.stream/playlist.m3u8"));
this.mp.useApplicationAudioSession=false //also try with true
this.mp.PrepareToPlay();
this.mp.Play();
Everything works great, i hear the audio
Notes:
I already search how fix this and on my plist file add UIBackgroundModes and string audio as many members advice. (Using the plist editor provided in monotouch). Also add this code to set the category of my audio session:
AudioSession.Initialize();
AudioSession.Interrupted += delegate{
Console.Writeline("interrupted");
}
AudioSession.Category = AudioSessionCategory.MediaPlayback; //also try ambientsound and others
The problem is:
How can i keep the audio playing when the app goes background, when the device blocks or the home button is pushed?
The stream always seems get muted when press home or block the device, when i return to the app the audio starts very quickly wich make me think that the app is streaming all the time but only doesnt hear.
I am testing only on the Iphone Simulator, i am starting to think that maybe this is caused only on simulator. any advice?
thanks in advance.
UPDATE:
I receive my licenses of monotouch and my register on the iOS Dev Program, so i test my app on the device. It works, the important part is on the AudioSession part:
AudioSessionCategory.MediaPlayback;
and when use the player on:
this.mp.useApplicationAudioSession=false

I receive my licenses of monotouch and my register on the iOS Dev Program, so i test my app on the device. It works, the important part is on the AudioSession part:
AudioSessionCategory.MediaPlayback;
and when use the player on:
this.mp.useApplicationAudioSession=false
The app works great on the device. Monotouch rules!

Related

Keep Unity program working in background after device goes to sleep?

I'm working on a Unity project that uses the device camera for AR purposes (I'm using AR Foundation). I currently have it set so that the device won't go to sleep (because I want it to operate for long-periods without user-interaction). Specifically, Screen.sleepTImeout = SleepTimeout.NeverSleep. If I didn't have this, then the device would go to sleep, and the program would stop functioning. Does anyone know if there is a way to keep the program working in the background after the device goes to sleep...similar to how music apps keep playing music? I've come up empty handed after a few hours of searching for a solution. Any help would be greatly appreciated!!!
Some people aren't finding this in more recent versions of Unity. But it's still there: In Player settings, under the section "Resolution and Presentation", you find "Resolution" and below that, there's "Run In Background".
This is only available for Web players and standalones (Anything Except IOS And Android Pretty Much), though (you won't find it when you have the iOS, Android or Flash tab selected.

C# Windows Form App - choose output audio device

I'm trying to do some basic app, with System.Speech.Synthesis in c#
It's just a basic form with a text input and after pressing enter it reads the text using ss.SpeakAsync();
The goal is to play that sound on a exact audio output device on Windows but I can't achieve that.
I've tried do it by windows settings, App volume and device preferences, but it doesn't work, not sure why.
I'm setting the correct device, but it plays on the "default" anyways, no matter what I do. It works with any other application correctly (like browser for example) bit with mine it doesn't. It works when I change all system audio to that device, but the main goal is that i want only this app to use "cable input".
I've tried to use some 3rd party software to do this (https://github.com/a-sync/audio-router) but it doesn't work either. I set everything, and when I click "play" in my app, then my app crashes with an exception: „[17132] TrayReader.exe” exited with code -1073741819 (0xc0000005) 'Access violation'.
So maybe I'm able to set in the code the correct audio output device? Or how do I fix those previous errors? maybe I need to add some privileges or something? I'm mainly web developer, this is my first time with windows app.
//edit -> it works, when i change all my system audio to cable input, play some sound, and then go back to my main device. It seems like a windows bug?
you may use a 3rd lib Naudio; the example below shows how to return to current position in case the player was playing a song. The key code is player.DeviceNumber = deviceNum; in which deviceNum is 0, 1, 2... depending how many speakers are available in your PC.
A disadvantage of Naudio is that it loads completely the song before playing, and quite slow.
var playback = player.PlaybackState;
if (playback == PlaybackState.Stopped) // change and exit
{
player.DeviceNumber = deviceNum;
return;
}
var currentTime = reader.CurrentTime;
player.Stop(); // must stop to change output
player.DeviceNumber = deviceNum;
player.Init(reader);
reader.CurrentTime = currentTime;
if (playback == PlaybackState.Playing)
player.Play();

C# Windows Phone 8 - Catastrophic Failure E_Unexpected when playing media

This is driving me bonkers! When trying to play a sound or any media element in a windows phone 8 app I get "Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))"
And that is all it says. If I look at the stack trace it shows: at Windows.UI.Xaml.Controls.MediaElement.Play()
Example code is:
XAML:
MediaElement x:Name="sndClick" Source="Assets/Sounds/Click.wav" Volume="10" AutoPlay="False"
Code:
sndClick.play();
I am trying to port a Windows 8 app to Windows Phone 8. The Windows 8 app works perfectly with the exact same code. This must be some issue with the Windows Phone. Everything else works, it just crashes when the phone trys to play media.
Not all the elements that play sounds crash the program, unless you click them a few times. Some elements always crash on the first click. No sounds ever play, even if the program doesn't crash with the exception it never plays any sounds.
ONE time (and only one time) it played the first couple frames of one of the animation files (.wmv) and then crashed.
It is a weird problem. All the code is copied from the working Windows 8 program and everything works except for the media. If I comment out the sound.play()'s and I disable the media player the program works fine.
At first I thought it might be that the resources weren't copying so I set the "Copy to Output Directory" to "Always Copy". No effect.
I tried the simulators and I tried it on physical hardware, it is the same on everything.
It isn't a resource issue, the simulators and the hardware have more than enough system resources, the whole app is only 22mb's.
It's not a codec issue, the animation is a .wmv file and the sounds are all .wav. Both should be natively supported on any windows device.
I have searched around and seen others with similar problems but I haven't seen any solutions. Does anybody know what is causing this issue and how to fix it? I would very much appreciate it.
I am pulling my hair out on this.
Thanks,
-RW
OK I fix it.
Windows Phone 8.1 simply hates it when there is more than one MediaElement in the program... Even just playing one at a time throws the error and wont play any sounds.
It is annoying. It is further annoying that it doesn't seem that XNA works in 8.1 (at least I couldn't get it to work)
The solution was to use SharpDX.
Special Thanks to this post: Multiple audio stream in Universal App(Runtime API), XNA SoundEffect replacement
I removed all but one MediaElement and added installed SharpDX and SharpDX Xaudio2 from the package installer thing. Then:
using SharpDX;
using SharpDX.XAudio2;
using SharpDX.IO;
using SharpDX.Multimedia;
private void PlaySound(string strPath)
{
XAudio2 xAudio = new XAudio2();
var masteringVoice = new MasteringVoice(xAudio);
var nativeFileStream = new NativeFileStream(strPath, NativeFileMode.Open, NativeFileAccess.Read, NativeFileShare.Read);
SoundStream stream = new SoundStream(nativeFileStream);
var waveFormat = stream.Format;
AudioBuffer buffer = new AudioBuffer
{
Stream = stream.ToDataStream(),
AudioBytes = (int)stream.Length,
Flags = BufferFlags.EndOfStream
};
var sourceVoice = new SourceVoice(xAudio, waveFormat, true);
sourceVoice.SubmitSourceBuffer(buffer, stream.DecodedPacketsInfo);
sourceVoice.Start();
}
And then just:
PlaySound("Assets/Sounds/Click.wav");
It works quite well! And the one remaining MediaElement that I need for an animation works now. My hair is even starting to grow back (though it is growing back grey).
If anyone else is having this issue, this is the fix. I hope it helps someone.

Windows Phone 8 - 2 Background Audios Clash and both of the App terminates

around a week ago, I submitted an online Background Radio Streaming app for the Windows Phone store. The app was quite good (as I used the Emulator to test it, it was good on all the possible sectors) but when I submitted it for certification, it failed.
According the the error log, if someone is already playing a Music from Music + Video hub and then tries to open this app, both of the apps Crash and stop unexpectedly.
So far I understood, it is because the Music of Music + Video hub is also Background Music and for playing 2 Background Musics at the same time, the apps are Crashing. It can be some other reason but the described one seemed more logical to me.
So, is there anyone who can tell me how to change the state of the app of Music + Video hub? I want to pause or stop the app of Music + Video hub for the time being so that both of the states of the app are not same. In that way, the apps won't clash with each other in the background.
Can anyone help me in this regard?
Use gameHasControl to check for other BAP using music:
bool gameHasControl = Microsoft.Xna.Framework.Media.MediaPlayer.GameHasControl;
if (!gameHasControl)
{
MessageBox.Show("stopping other player"); // normally you should ask if to do that
BackgroundAudioPlayer.Instance.Stop();
}
Once it is Stopped, when you start your BAP, then old instance invokes Shutdown(), and your BAP will be new Instance, which you can normally use. The same is when your BAP is in memory and you start to play from Music+Video Hub.
Only watch out, because when you use XNA, you sometimes need to do:
FrameworkDispatcher.Update();
Otherwise your App will sometimes crash. Good luck.
EDIT - after comment
To make it work you need to add a reference to Microsoft.Phone.BackgroundAudio or use like this:
Microsoft.Phone.BackgroundAudio.BackgroundAudioPlayer.Instance.Stop();
BackgroundAudioPlayer is a Singleton which you are allowed to use - in this case - Stop it playing (of course give the choice to the User).

Control WP7 and WP8 app with bluetooth headset

I have developed a music player app for Windows Phone 7.5 and Windows Phone 8. Now I want to listen to the music with various bluetooth devices. I can connect it to my phone and listen to music, so that's working just fine. But it acts weird in certain scenarios. If the music is paused it will resume on the speakers if I disconnect the bluetooth. I can see in my log that it receives a user action to UserAction.SEEK to the position it was when I turned off bluetooth. At this point, the player.Position variable has somehow gotten to be 0, so it goes to 0 and back to where I was. And I don't do anything that would cause that action.
What I am asking: Is there a way to take over control or to get any control in the AudioPlaybackAgent that I use of what happens when I do something with my bluetooth devices. Is there any event that occurs, or is all this handled by the OS? If so, why does my application behave differently than the stock app? Also, why am I receiving a UserAction.SEEK when I turn of my bluetooth device?
If my question is unclear, please don`t hesitate to ask!
This is a known issue with the platform, but is fixed in WP8 GDR2. You can workaround the issue by detecting an OnUserAction sequence of Pause Seek Play. Once detected, you can pause on the next call to OnPlayerStateChanged. A few things to be aware of:
Consecutive calls to OnUserAction aren't guaranteed to be called on the same instance of AudioPlayer, so any state should be stored in static state.
The track will play from the speaker for a brief moment, so you might want to set the volume to 0 (after making note of it's previous value, of course)
I've posted a Gist to GitHub with a full implementation. Example usage and a more detailed rundown of the problem is available on my blog.

Categories