×

Discussion Board

Results 1 to 9 of 9
  1. #1
    Nokia Developer Champion
    Join Date
    Nov 2012
    Location
    Karachi
    Posts
    89

    Background Music - MediaElement

    I have declared a MediaElement in my App.xaml for playing background music so that the music dont stop durring Frame Navigation.

    But,
    I am unable that MediaElement(declaredin App.xaml) in my other pages like(MainMenu.xaml, Page1.xaml) to stop or replay background music.

    how do I pass that MediaElement to other pages?

  2. #2
    Nokia Developer Champion
    Join Date
    Jul 2012
    Posts
    88

    Re: Background Music - MediaElement

    Hello,
    Try using a Audio Play back Agent, here's the link for a How-to from MSDN : http://msdn.microsoft.com/en-us/libr...v=vs.105).aspx

    Hope this helps,

  3. #3
    Nokia Developer Champion
    Join Date
    Nov 2012
    Location
    Karachi
    Posts
    89

    Re: Background Music - MediaElement

    Thanks for the reply WPMorocco .
    I will try that out

    I just want to know that .. Is there any method to acess APP.XAML elements in other pages.?

  4. #4
    Nokia Developer Champion
    Join Date
    Jul 2012
    Posts
    88

    Re: Background Music - MediaElement

    Yes you can
    Code:
    public static string test;
    And then access it using : App.test

  5. #5
    Nokia Developer Champion
    Join Date
    Nov 2012
    Location
    Karachi
    Posts
    89

    Re: Background Music - MediaElement

    I mean APP.XAML

    Like
    <MediaElement x:Name="MediaPlayer" Source="Audio/Background.mp3" AutoPlay="True" />

    how do I acess "MediaPlayer" even in App.Xaml.cs

  6. #6
    Nokia Developer Champion
    Join Date
    Sep 2012
    Location
    Morocco
    Posts
    291

    Re: Background Music - MediaElement

    Quote Originally Posted by Shayan Rais View Post
    I mean APP.XAML

    Like
    <MediaElement x:Name="MediaPlayer" Source="Audio/Background.mp3" AutoPlay="True" />

    how do I acess "MediaPlayer" even in App.Xaml.cs
    App.xaml don't have a LayoutRoot so basically you're going to put the MediaElement in the
    Code:
     <Application.Resources> <MediaElement x:Key="YourKey" ... /> </Application.Resources>
    and simply get that resource from anywhere in your app, using this code
    Code:
    MediaElement myMedia = App.Current.Resources["YourKey"] as MediaElement;
    You may have some certification issues doing so.
    check the link below
    http://stackoverflow.com/questions/1...y-throung-xaml

  7. #7
    Nokia Developer Champion
    Join Date
    Nov 2012
    Location
    Karachi
    Posts
    89

    Re: Background Music - MediaElement

    Thank You Loukt
    Problem Solved

  8. #8
    Nokia Developer Champion
    Join Date
    Nov 2012
    Location
    Karachi
    Posts
    89

    Re: Background Music - MediaElement

    I am using Audio Play back Agent, as suggested by WPMorocco

    The problem I am facing in Application_Activated Method

    Code:
           private void Application_Activated(object sender, ActivatedEventArgs e)
            {
                if (PlayState.Paused== BackgroundAudioPlayer.Instance.PlayerState)
                {
                    BackgroundAudioPlayer.Instance.Play();
                }
            }
    
           private void Application_Deactivated(object sender, DeactivatedEventArgs e)
            {
                if (PlayState.Playing == BackgroundAudioPlayer.Instance.PlayerState)
                {
                    BackgroundAudioPlayer.Instance.Pause();
                }
            }
    This code is working fine , if I navigate back using Windows Button(HOME key) and return using the Back button

    and on lockScreen , BackgroundAudioPlayer.Instance.Pause(); is called

    but on Unlocking Screen, BackgroundAudioPlayer.Instance.Play(); in the Application_Activated is not playing .. and No Audio is played once I returned to my app after Unlocking Screen.

  9. #9
    Nokia Developer Champion
    Join Date
    Nov 2012
    Location
    Karachi
    Posts
    89

    Re: Background Music - MediaElement

    I have figure out the solution.

    on lockScreen,
    PlayerState changes to PlayState.Stopped instead of PlayState.Paused

    SOLUTION:

    Changes in PlayTrack Method in AudioPlayer.cs
    Code:
     private void PlayTrack(BackgroundAudioPlayer player)
    {
     if (PlayState.Paused == player.PlayerState || PlayState.Stopped == player.PlayerState)
                {
                    // If we're paused, we already have 
                    // the track set, so just resume playing.
                    player.Play();
                }
                else
                {
                    // Set which track to play. When the TrackReady state is received 
                    // in the OnPlayStateChanged handler, call player.Play().
                    player.Track = _playList[currentTrackNumber];
                }
    }
    Changes in Application_Activated Method in App.xaml.cs
    Code:
     private void Application_Activated(object sender, ActivatedEventArgs e)
            {
                if (PlayState.Stopped == BackgroundAudioPlayer.Instance.PlayerState || PlayState.Paused == BackgroundAudioPlayer.Instance.PlayerState)
                {
                    BackgroundAudioPlayer.Instance.Play();
                }
            }

Similar Threads

  1. How to pause the Music Player in background?
    By Julischeng in forum Symbian C++
    Replies: 1
    Last Post: 2011-05-20, 14:54
  2. Background music and sound FX for Qt/Maemo
    By tamhanna in forum Nokia N9
    Replies: 13
    Last Post: 2010-06-17, 21:33
  3. background music during call
    By pankaj gupta55 in forum Symbian C++
    Replies: 3
    Last Post: 2008-08-27, 12:13
  4. Background Music - Urgent
    By davidhamburger in forum Mobile Java Media (Graphics & Sounds)
    Replies: 4
    Last Post: 2003-07-25, 15:13
  5. Background Music
    By davidhamburger in forum Mobile Java Media (Graphics & Sounds)
    Replies: 1
    Last Post: 2003-06-20, 09:18

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •