tapjoy issue with video playing

583 Views Asked by At

I am having one Windows phone game, built using Silverlight. In this game, I want to add TapJoy. I have downloaded their latest SDK and follow all their steps to intigrate the it within my app.

In the game, I am using silverlight as a main frame work and Global Media Element to play contious Background Music. I am using (Microsoft.Xna.Framework.Media) (Microsoft.Xna.Framework) namespace. Using them, I use following methods to play contious background sound. DispatcherTimer and FrameworkDispatcher.Update

Now, when I click tap joy button to open their offers, they load fine; however, when I open the video within the offer, they show us following error “Video cannot be played, please try again.”

Based on some research and study, I tried few things and found that, a) I need to set Media Element and DispatcherTimer is to null. b) The application is sent in background (deactivated) and then I open it again (activated), the video is coming fine. I checked and found that Media Element and DispatcherTimer were set to null properly.

But if I follow step one only, and do not send the app in background, the media element and dispatcherTimer are not set to null.

Can anyone please help me and answer me following

a) Am I doing anythign wroing with this? b) Can I do anything so that when tap joy button is clicked, my application is sent to background automatically since this can solve the issue. c) I am using gc.collect() after setting value to null but still it is not getting destroyed.

Thanks in advance, David Jacob.

1

There are 1 best solutions below

0
On

I'm trying to follow along with what you've said. I personally would've set it up differently, but I'll get to that later. I have a setup that is similar to your description, and it works with Tapjoy's Videos.

Firstly, you mentioned that it was a Silverlight game, so I created a new Windows Phone Application project under the Silverlight For Windows Phone template, in VS 2010.

Setup Dispatcher:

I added the following class to my project (typically called XNAFrameworkDispatcherService.cs from this msdn example: http://msdn.microsoft.com/en-us/library/ff842408.aspx)

public class XNAFrameworkDispatcherService : IApplicationService
{
    private DispatcherTimer frameworkDispatcherTimer;

    public XNAFrameworkDispatcherService()
    {
        this.frameworkDispatcherTimer = new DispatcherTimer();
        this.frameworkDispatcherTimer.Interval = TimeSpan.FromTicks(333333);
        this.frameworkDispatcherTimer.Tick += frameworkDispatcherTimer_Tick;
        FrameworkDispatcher.Update();
    }

    void frameworkDispatcherTimer_Tick(object sender, EventArgs e)
    {
        FrameworkDispatcher.Update();
    }

    void IApplicationService.StartService(ApplicationServiceContext context)
    {
        this.frameworkDispatcherTimer.Start();
    }

    void IApplicationService.StopService()
    {
        this.frameworkDispatcherTimer.Stop();
    }
}

In order to start this service, make sure you've added it to your App.xaml. Add an attribute to your Application element that points to your namespace, something like this:

xmlns:s="clr-namespace:WindowsPhoneApplication;assembly=WindowsPhoneApplication">

Then within your block add the following:

<s:XNAFrameworkDispatcherService />

Play Music:

Now about playing a looping music file. In the MainPage.xaml.cs, I've setup a Microsoft.Xna.Framework.Media.Song to loop when the page is navigated to, using the Microsoft.Xna.Framework.Media.MediaPlayer.

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
    base.OnNavigatedTo(e);

    Tapjoy.TapjoyConnect.Instance.RequestTapjoyConnect("your-app-id", "your-secret-key");

    try
    {
        Song song = Song.FromUri("example", new Uri("/example.wma", UriKind.Relative));
        MediaPlayer.IsRepeating = true;
        MediaPlayer.Play(song);
    }
    catch (Exception)
    {
        System.Diagnostics.Debug.WriteLine("Can't load sound");
    }
}

I also set it to stop playing music, when the page is navigated away from.

protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
{
    base.OnNavigatedFrom(e);

    MediaPlayer.Stop();
}

I then created a button to launch the Tapjoy Offer wall.

private void button1_Click(object sender, RoutedEventArgs e)
{
    Tapjoy.TapjoyConnect.Instance.ShowOffers();
}

Summary:

What happens now, is when your Application starts up, it launches the XNAFrameworkDispatcherService that ticks at approximately 30fps. This will dispatch messages that are in the XNA Framework Queue for you. This is only needed in silverlight applications that are using audio/media services from XNA.

When the MainPage is navigated to, you ping Tapjoy with the Connect call, and you load up your Song to loop.

Normal gameplay can progress now, and when the Show Offers button is clicked, Tapjoy will navigate away from your page, causing on navigated from event to fire, and the MediaPlayer will stop your song.

Ideas for your game:

You might want to consider creating a new project and using the "Windows Phone Silverlight and XNA Application" option. VS2010 will create a blank project with a Content manager already setup, so you can use sounds and images with the XNA pipeline, which I've found to be easier.

Good luck, and remember that Tapjoy provides support for these issues as well. Just email them at [email protected], or use the Tapjoy Developer group at: https://groups.google.com/group/tapjoy-developer?hl=en