Windows phone 8 on emulator - why can't I play audio files? - mp3

I'm converting an app that was written in Silverlight, and so far I've succeeded in solving all of the problems, except for one:
For some reason, the emulator refuses to play any audio files of the app, and it doesn't even throw an exception. I've checked, and in the ringtone category it can make sounds.
The original code was :
<Grid x:Name="sharedFullScreenFilePathContainer"
Tag="{Binding StringFormat=\{0\},Converter={StaticResource fullScreenImageConverter}}">
<Image x:Name="fullScreenImage" Stretch="Fill"
Source="{Binding ElementName=sharedFullScreenFilePathContainer,Path=Tag, StringFormat=../Assets/images/\{0\}.jpg}"
ImageFailed="onFullScreenImageFailedToLoad" MouseLeftButtonDown="onPressedOnFullScreenImage" />
<MediaElement x:Name="mediaPlayer" AutoPlay="True"
Source="{Binding ElementName=sharedFullScreenFilePathContainer,Path=Tag, StringFormat=../Assets/sounds/\{0\}.wma}" />
</Grid>
so, the image that I set to this item's context is really shown, but the sound that really exists on the path I set to it doesn't play (I've checked in the "Bin" folder).
I've tried to use code instead of xaml, but I still have the same problem.
I've tried this (though it's usually used for background music):
AudioTrack audioTrack = new AudioTrack(new Uri("../Assets/sounds/" + fileToOpen, UriKind.Relative), "", "", "", null);
BackgroundAudioPlayer player = BackgroundAudioPlayer.Instance;
player.Track = audioTrack;
player.Play();
It didn't play anything, and also didn't throw any exception.
I've also tried the next code, but it throws an exception (file not found exception) probably because I don't call it right:
Stream stream = TitleContainer.OpenStream("#Assets/sounds/" + fileToOpen);
SoundEffect effect = SoundEffect.FromStream(stream);
FrameworkDispatcher.Update();
effect.Play();
I've also tried using wma files but it also didn't work.
I also tried to play with the "copy to output directory" parameter of the mp3 files (to "always" and "only if new" ) and with the "build action" parameter (to "none" and "content" ). Nothing helps.
Can anyone please help me? I didn't develop for Silverlight/WP for a very long time and I can't find out how to fix it .
Btw, since later I need to know when the sound has finished playing (and also to be able to stop it), I would like to use code anyway. I would be happy if you could also tell me how to do it too (I can ask it on a new post if needed).
EDIT:
ok , i've found out the problem : i kept getting a weird exception when using the MediaPlayer.Play() method , and after checking out about the exception , i've found out that it's a known issue , and that i need to call FrameworkDispatcher.Update(); right before i call the Play() method .
so the solution would be to do something like this:
Song song = Song.FromUri(...);
MediaPlayer.Stop();
FrameworkDispatcher.Update();
MediaPlayer.Play(song);
the exception is:
'System.InvalidOperationException' occurred in System.Windows.ni.dll"
i've found the solution here .
Now the question is why , and how come I didn't find anything related to it in the demos of windows phone ? Also , i would like to know what this function does .
ok , since nobody gave me an answer to both questions , and I still wish to give the bounty , I will ask another question :
If there is really no solution other than using the MediaPlayer class for windows phone , how do i capture the event of finishing playing an audio file ? Even getting the audio file duration doesn't work (keeps returning 0 length , no matter which class i've tried to use) ...

The BackgroundAudioPlayer can play files only from isolated storage or from a remote URI, that is why you can here anything!
If you have your file as resources in your app, you must first copy them to the isolated store, and then make a reference to the file in the isolated store to your BackgroundAudioPlayer.
private void CopyToIsolatedStorage()
{
using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
{
string[] files = new string[]
{ "Kalimba.mp3",
"Maid with the Flaxen Hair.mp3",
"Sleep Away.mp3" };
foreach (var _fileName in files)
{
if (!storage.FileExists(_fileName))
{
string _filePath = "Audio/" + _fileName;
StreamResourceInfo resource = Application.GetResourceStream(new Uri(_filePath, UriKind.Relative));
using (IsolatedStorageFileStream file = storage.CreateFile(_fileName))
{
int chunkSize = 4096;
byte[] bytes = new byte[chunkSize];
int byteCount;
while ((byteCount = resource.Stream.Read(bytes, 0, chunkSize)) > 0)
{
file.Write(bytes, 0, byteCount);
}
}
}
}
}
}
And then you can make a list of your songs
private static List<AudioTrack> _playList = new List<AudioTrack>
{
new AudioTrack(new Uri("Kalimba.mp3", UriKind.Relative),
"Kalimba",
"Mr. Scruff",
"Ninja Tuna",
null),
new AudioTrack(new Uri("Maid with the Flaxen Hair.mp3", UriKind.Relative),
"Maid with the Flaxen Hair",
"Richard Stoltzman",
"Fine Music, Vol. 1",
null),
new AudioTrack(new Uri("Sleep Away.mp3", UriKind.Relative),
"Sleep Away",
"Bob Acri",
"Bob Acri",
null),
// A remote URI
new AudioTrack(new Uri("http://traffic.libsyn.com/wpradio/WPRadio_29.mp3", UriKind.Absolute),
"Episode 29",
"Windows Phone Radio",
"Windows Phone Radio Podcast",
null)
};
And play your tracks!
private void PlayNextTrack(BackgroundAudioPlayer player)
{
if (++currentTrackNumber >= _playList.Count)
{
currentTrackNumber = 0;
}
PlayTrack(player);
}
private void PlayPreviousTrack(BackgroundAudioPlayer player)
{
if (--currentTrackNumber < 0)
{
currentTrackNumber = _playList.Count - 1;
}
PlayTrack(player);
}
private void PlayTrack(BackgroundAudioPlayer player)
{
// Sets the track to play. When the TrackReady state is received,
// playback begins from the OnPlayStateChanged handler.
player.Track = _playList[currentTrackNumber];
}

If you want the MediaElement to work from your code you can do something like this!
MediaElement me = new MediaElement();
// Must add the MediaElement to some UI container on
// your page or some UI-control, otherwise it will not play!
this.LayoutRoot.Children.Add(me);
me.Source = new Uri("Assets/AwesomeMusic.mp3", UriKind.RelativeOrAbsolute);
me.Play();

Use the MediaElement instead, this can play media files stored in your application, but stops playing when the application stops (you can make some advance chance to your app so it keep running, but it will not work very well)
In your XAML:
<Button x:Name="PlayFile"
Click="PlayFile_Click_1"
Content="Play mp3" />
In your Code behind:
MediaElement MyMedia = new MediaElement();
// Constructor
public MainPage()
{
InitializeComponent();
this.LayoutRoot.Children.Add(MyMedia);
MyMedia.CurrentStateChanged += MyMedia_CurrentStateChanged;
MyMedia.MediaEnded += MyMedia_MediaEnded;
}
void MyMedia_MediaEnded(object sender, RoutedEventArgs e)
{
System.Diagnostics.Debug.WriteLine("Ended event " + MyMedia.CurrentState.ToString());
// Set the source to null, force a Close event in current state
MyMedia.Source = null;
}
void MyMedia_CurrentStateChanged(object sender, RoutedEventArgs e)
{
switch (MyMedia.CurrentState)
{
case System.Windows.Media.MediaElementState.AcquiringLicense:
break;
case System.Windows.Media.MediaElementState.Buffering:
break;
case System.Windows.Media.MediaElementState.Closed:
break;
case System.Windows.Media.MediaElementState.Individualizing:
break;
case System.Windows.Media.MediaElementState.Opening:
break;
case System.Windows.Media.MediaElementState.Paused:
break;
case System.Windows.Media.MediaElementState.Playing:
break;
case System.Windows.Media.MediaElementState.Stopped:
break;
default:
break;
}
System.Diagnostics.Debug.WriteLine("CurrentState event " + MyMedia.CurrentState.ToString());
}
private void PlayFile_Click_1(object sender, RoutedEventArgs e)
{
// Play Awesome music file, stored as content in the Assets folder in your app
MyMedia.Source = new Uri("Assets/AwesomeMusic.mp3", UriKind.RelativeOrAbsolute);
MyMedia.Play();
}

Audio track plays audio files stored in isolated storage or streamed over the internet.

Playing audio files stored within the application package works fine for me. I got files named like "Alarm01.wma" in project folder Resources\Alarms. I then play these sounds like this:
using Microsoft.Xna.Framework.Media;
...
Song s = Song.FromUri("alarm", new Uri(#"Resources/Alarms/Alarm01.wma", UriKind.Relative));
MediaPlayer.Play(s);
Also don't forget to reference Microsoft.Xna.Framework library.
I guess it should work fine for mp3 files and files stored in IsolatedStorage as well.

Related

Xbox Live leaderboard in UWP game returns with error 404

When trying to retrieve a leaderboard from Xbox Live, the stat event type get_leaderboard_complete returns the error code 404. I'm using Xbox Live in a UWP game in the Creators Program.
I'm am able to set and retrieve the stat for the user. This part works without issue:
xbox_live_result<stat_value> serverStat = m_statsManager->get_stat(m_user, L"score");
auto result = serverStat.payload();
if (result.as_integer() < score) {
setStatForUser(m_user, L"score", score);
}
My code is adopted from the leaderboard example in the Xbox Live Samples. So to retrieve my leaderboard I'm calling getLeaderboard(m_user, L"score"); and every frame I'm calling statsManager->do_work();.
// Process events from the stats manager
// This should be called each frame update
auto statsEvents = m_statsManager->do_work();
std::wstring text;
for (const auto& evt : statsEvents)
{
switch (evt.event_type())
{
case stat_event_type::local_user_added:
text = L"local_user_added";
break;
case stat_event_type::local_user_removed:
text = L"local_user_removed";
break;
case stat_event_type::stat_update_complete:
text = L"stat_update_complete";
break;
case stat_event_type::get_leaderboard_complete:
text = L"get_leaderboard_complete";
auto getLeaderboardCompleteArgs = std::dynamic_pointer_cast<leaderboard_result_event_args>(evt.event_args());
processLeaderboards(evt.local_user(), getLeaderboardCompleteArgs->result());
break;
}
stringstream_t source;
source << _T("StatsManager event: ");
source << text;
source << _T(".");
log("%S", source.str().c_str());
}
Because I'm able to set and retrieve the stat without issue, I wonder if maybe it's an issue with the Xbox Live backend? However, I'm not very familiar with the xbox live 2017 data platform, and I may be calling something incorrectly.
I discovered a solution:
Create a new stat/leaderboard in Dev Center.
Press the “Test” button. This is important because it publishes the service configuration.
I’m unsure why the original stat didn’t work. Perhaps because I used the word “score” as the statID.

Osmdroid 5.6, offline MBTiles

I'm trying to use MBtiles offline with osmdroid,
I took code sample from here https://github.com/osmdroid/osmdroid/blob/master/OpenStreetMapViewer/src/main/java/org/osmdroid/samplefragments/tileproviders/SampleOfflineOnly.java
But always empty map shown, is there problem with my code?
my code is:
public class OSMDroid extends AppCompatActivity {
private MapView mapView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_osmdroid);
mapView = (MapView) findViewById(R.id.map);
String name = "map.mbtiles";
File f = new File(Environment.getExternalStorageDirectory() + "/osmdroid", name);
if (f.exists()) {
try {
//ok found a file we support and have a driver for the format, for this demo, we'll just use the first one
//create the offline tile provider, it will only do offline file archives
//again using the first file
OfflineTileProvider tileProvider = new OfflineTileProvider(new SimpleRegisterReceiver(this),
new File[]{f});
//tell osmdroid to use that provider instead of the default rig which is (asserts, cache, files/archives, online
mapView.setTileProvider(tileProvider);
//this bit enables us to find out what tiles sources are available. note, that this action may take some time to run
//and should be ran asynchronously. we've put it inline for simplicity
String source = "";
IArchiveFile[] archives = tileProvider.getArchives();
if (archives.length > 0) {
//cheating a bit here, get the first archive file and ask for the tile sources names it contains
Set<String> tileSources = archives[0].getTileSources();
//presumably, this would be a great place to tell your users which tiles sources are available
if (!tileSources.isEmpty()) {
//ok good, we found at least one tile source, create a basic file based tile source using that name
//and set it. If we don't set it, osmdroid will attempt to use the default source, which is "MAPNIK",
//which probably won't match your offline tile source, unless it's MAPNIK
source = tileSources.iterator().next();
mapView.setTileSource(FileBasedTileSource.getSource(source));
} else {
mapView.setTileSource(TileSourceFactory.DEFAULT_TILE_SOURCE);
}
} else {
mapView.setTileSource(TileSourceFactory.DEFAULT_TILE_SOURCE);
}
mapView.setUseDataConnection(false);
mapView.setBuiltInZoomControls(true);
IMapController mapController = mapView.getController();
mapController.setZoom(10);
GeoPoint startPt = new GeoPoint(61.5797,51.5997);
mapController.setCenter(startPt);
mapView.invalidate();
return;
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
MBTilesFileArchive getTileSources always returns empty string, here is implementation:
public Set<String> getTileSources(){
//the MBTiles spec doesn't store source information in it, so we can't return anything
return Collections.EMPTY_SET;
}
In order to make a offline map you should add tiles first. You can use Maperitive app to make your map tiles(zip is easier to manage than sql). Name the zip MapquestOSM. After you have done it create a folder "osmdroid" in phones memory(Directly into the internal memory or sd card) and add your map tiles in it.
Parameters of the XYTileSource changes related to the map tiles you have created. This code handles everything about map tiles itself. I hope it helps you
mapView.setUseDataConnection(false);
mapView.setTileSource(new XYTileSource("MapquestOSM", 2, 15, 256, ".png", new String[]{}));

captureScreen in cocos-2d C++ - how to use or save the captured image?

I want to have a button in my (iOS) app that takes a screenshot of the current screen and then attaches it to a text message.
Like I have seen in this other app...
I have message sending working, and I think I have screenshot working, but I don't know where the screenshot is saved or how to use it.
My message sending is called from a button in the app...
void GameOverScene::messageCallBack(cocos2d::Ref *sender) {
CocosDenshion::SimpleAudioEngine::getInstance()->playEffect(ALL_BUTTONS_CLICK_SOUND_NAME);
utils::captureScreen( CC_CALLBACK_2(GameOverScene::afterCaptured, this), "screenshot.png" );
__String *messageTextOne = __String::create("sms:&body=Hey,%20I%20just%20hit%20a%20score%20of%20");
__String *messageTextTwo = __String::createWithFormat("%i", score);
__String *messageURL = __String::createWithFormat("%s%s", messageTextOne->getCString(), messageTextTwo->getCString());
Application::getInstance()->openURL(messageURL->getCString());
}
and the screenshot function is...
void GameOverScene::afterCaptured( bool succeed, const std::string &outputFile ) {
if (succeed) {
log("Screen capture succeeded");
Size screenSize = Director::getInstance()->getWinSize();
RenderTexture * tex = RenderTexture::create(screenSize.width, screenSize.height);
tex->setPosition(screenSize.width/2, screenSize.height/2);
tex->begin();
this->getParent()->visit();
tex->end();
tex->saveToFile("Image_Save.png", Image::Format::PNG);
} else {
log("Screen capture failed");
}
}
I get the message in the console "Screen capture succeeded", and my message app opens up with the prefilled text message.
What I need to do is to add the screenshot to this message, but I cant see how to do that, or where the screenshot is saved, or how to use the saved screenshot.
If it succeeded it's saved in private application directory. Use software like iExplorer, find your app and it should be inside Documents directory. If you want to see it in Photos you have manually add it there.
In order to achieve this, you have to call
UIImageWriteToSavedPhotosAlbum
see: How to save picture to iPhone photo library?
You have to create a function in AppController.m and use it from here. You can call objc code from c++. It requires UIImage so first you have to pass filepath and load uiimage from it and then finally add to gallery.
You can use something like this to get the path of image saved:
void CustomLayerColor::saveFile(Node*node, std::string fileName, const renderTextureCallback& callBack) {
float renderTextureWidth = node->getBoundingBox().size.width;
float renderTextureHeight = node->getBoundingBox().size.height;
RenderTexture * renderTexture = RenderTexture::create(renderTextureWidth,renderTextureHeight);
renderTexture->begin();
node->visit();
renderTexture->end();
renderTexture->saveToFile(fileName, Image::Format::PNG, true,callBack);
}
void CustomLayerColor::onSaveFileCallBack(RenderTexture * mRenderTexture, const std::string& savedFileNameWithFullPath) {
}

How do I make a custom event with VTK?

I'm making a thread software with VTK, where I need to change the model itself in real time, while I need to change his method of rendering. Everything is working fine, but, the problem start with the interactor->start(); , the model data gets updated just fine, but it's only showed on screen when I move The camera. Also I have selected some methods for generating a 3D data from a imagedata file, for that I need to close the vtk window (interactor window) and then the code will reopen it and send the new data generated to it...
I would need something like these:
int force close_window = false; int refresh_interactor = false;
I managed to make the Window close, but only with vtkcommand::Keypressed command, but idk how do I do with a new command :S, I tried the vtkcommand::UserEvent but I didn't found a good information about how to deal with that data (like some way to call it)
the way I'm dealing with VTK is with two threads, the first one, is just about the vtk iren loop, and the second one would manage the models and check if iren requires to be updated.
In my dream code it should be something like this:
=======================================================
bool VTKWindow()
{
...
vtkSmartPointer ator = vtkSmartPointer::New();
iren = vtkSmartPointer::New();
RenWindow = vtkSmartPointer::New();
render->SetBackground(.1, .2, .3);
RenWindow->AddRenderer(renderer);
iren->SetRenderWindow(RenWindow);
if(data_type == voxel_type)
{
Render->AddViewProp(VoxelData);
}
else
{
actor->SetMapper(PolyData);
Render->AddActor(Actor);
}
RenWindow->Render();
iren->Start();
}
void ManageVTK()
{
while true loop...
if(force close_window == true)
do some command to exit the iren loop
if(refresh_interactor == true)
do some command to refresh iren
}
Sorry for the english, it's not my native language, and also sorry about the question format, it's the first time I'm using stackoverflow
It may sounds stupid, but, I found a kind of solution for the problem.
I saw on related links this guy vtkRenderWindowInteractor event loop and threading and, it's almost the same problem...
class VTKNewEvent : public vtkCommand{
public:
vtkTypeMacro(VTKNewEvent , vtkCommand);
static VTKNewEvent * New(){
return new VTKNewEvent ;
}
void Execute(vtkObject * caller, unsigned long vtkNotUsed(eventId), void * vtkNotUsed(callData)){
vtkRenderWindowInteractor *iren = static_cast<vtkRenderWindowInteractor*>(caller);
if (iren_close == true){
iren->GetRenderWindow()->Finalize // Stop the interactor
iren->TerminateApp();
iren_close = false;
}
if (iren_update== true){
renderJanela->Render();
iren_update= false;
}
}
};
bool VTKWindow(){
vtkSmartPointer<VTKNewEvent > IrenRefresh= vtkSmartPointer<VTKNewEvent>::New();
...
iren->CreateRepeatingTimer(1);//this makes that IrenRefresh will be called at every 1ms
iren->AddObserver(vtkCommand::TimerEvent, IrenRefresh);
iren->Start();
...
}
it's simple, but, maybe not the best, but it did Th job, I hope this link will help people that are starting into the VTK world, since threads + rendering loop wasn't a simple job to understand what was going on

VS 2005 C++ edit content of the CRichEditCtrl instance

I've installed a Windows XP Professional SP3 on a VMWare image and the Visual Studio 2005 on it. I've created a new dialog based C++ MFC project with /clr support. I've put a RichEdit 2.0 control onto the auto-generated dialog and I'm trying to read up a text file and put its content into this RichEdit 2.0 control by button click without formatting. I've added a variable to the RichEdit 2.0 called pCRichEditCtrl and here is my code which doesn't work.
CWinApp inheritance:
BOOL CTextFormatterApp::InitInstance()
{
...
AfxInitRichEdit2();
CWinApp::InitInstance();
...
}
CDialog inheritance:
void CTextFormatterDlg::OnBnClickedButton1()
{
StreamReader^ objReader = gcnew StreamReader("c:\\text.txt");
String ^sLine = "";
sLine = objReader->ReadLine();
while (sLine != nullptr)
{
pCRichEditCtrl.SetSel(pCRichEditCtrl.GetTextLength(), -1);
pCRichEditCtrl.ReplaceSel(CString(sLine));
sLine = objReader->ReadLine();
}
objReader->Close();
}
I don't know whether it counts but I get the following warnings at linking:
TextFormatterDlg.obj : warning LNK4248: unresolved typeref token (01000016) for 'AFX_CMDHANDLERINFO'; image may not run
TextFormatter.obj : warning LNK4248: unresolved typeref token (01000012) for 'AFX_CMDHANDLERINFO'; image may not run
TextFormatterDlg.obj : warning LNK4248: unresolved typeref token (01000015) for 'IAccessibleProxy'; image may not run
I'm not sure what I'm doing because I'm familiar only with newer frameworks and I don't know either Windows.
Input file exists, I can see the read text if I debug the application but I can't see any changes in the edit box. I've tried to call pCRichEditCtrl.UpdateData(true); but nothing has changed.
Is it enough to add a variable for getting the controller of the box (pCRichEditCtrl)? It seems to the pointer doesn't point to the proper control item.
Do you have any idea what is missing?
There's no need to use CLI to just read text files, try something like:
void CTextFormatterDlg::OnBnClickedButton1()
{ CStdioFile f1;
CString sLine;
if (!f1.Open(_T("c:\\text.txt"), CFile::modeRead | CFile::typeText))
return;
while (f1.ReadString(sLine))
{ pCRichEditCtrl.SetSel(pCRichEditCtrl.GetTextLength(), -1);
pCRichEditCtrl.ReplaceSel(sLine);
}
f1.Close();
}
EDIT: control variable pCRichEditCtrl
a) should be declared in the dialog class as CRichEditCtrl pCRichEditCtrl;
b) should be connected to the ID of the control (e.g.: IDC_RICHEDIT21), like
void CTextFormatterDlg::DoDataExchange(CDataExchange* pDX)
{ CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX, IDC_RICHEDIT21, pCRichEditCtrl);
}
c) I have tested the following code and it works for me (adds "aa" to the control window on each button click)
pCRichEditCtrl.SetSel(pCRichEditCtrl.GetTextLength(), -1);
pCRichEditCtrl.ReplaceSel(TEXT("aa"));
I share the final solution with the community to be available for those who faces with the same issue. I don't know why do I have to use Update(FALSE); on the CWinApp inheritance two times but it solves everything. If someone has an idea or a better (nicer) solution don't hesitate to share it with us, I'll move the accepted flag to that version (if it is possible, I haven't tried it before).
void CTextFormatterDlg::OnBnClickedButton1()
{
StreamReader^ objReader = gcnew StreamReader("c:\\text.txt");
String ^sLine = objReader->ReadLine();
UpdateData(FALSE); //this is the first unexpected first aid
while (sLine != nullptr)
{
pCRichEditCtrl.SetSel(pCRichEditCtrl.GetTextLength(), -1);
pCRichEditCtrl.ReplaceSel(CString(sLine + "\r\n"));
UpdateData(FALSE); //this is the second unexpected first aid
sLine = objReader->ReadLine();
}
objReader->Close();
}