iOS Tutorial Background playback

Discussion in 'iOS Tutorials' started by Erel, Jan 13, 2015.

  1. Erel

    Erel Administrator Staff Member Licensed User

    This tutorial explains how you can play local audio files or stream audio while your app is in the background.
    Standard applications are killed when moved to the background. However you can add the following attribute to your project to mark it as an application that plays audio in the background:
    Code:
    #Region  Project Attributes
       
    #ApplicationLabel: B4i Example
       
    #Version: 1.0.0
       
    'Orientation possible values: Portrait, LandscapeLeft, LandscapeRight and PortraitUpsideDown
       #iPhoneOrientations: Portrait, LandscapeLeft, LandscapeRight
       
    #iPadOrientations: Portrait, LandscapeLeft, LandscapeRight, PortraitUpsideDown
       
    #Target: iPhone, iPad
       
    #MinVersion: 7.1
    #End Region
    #PlistExtra: <key>UIBackgroundModes</key><array><string>audio</string></array>

    Sub Process_Globals
      
    Public App As Application
      
    Public NavControl As NavigationController
      
    Private Page1 As Page
      
    Private NativeMe As NativeObject
      
    Private vv As VideoView
    End Sub

    Private Sub Application_Start (Nav As NavigationController)
      NativeMe = Me
      NavControl = Nav
      Page1.Initialize(
    "Page1")
      Page1.Title = 
    "Page 1"
      Page1.RootPanel.Color = 
    Colors.White
      NavControl.ShowPage(Page1)
      NativeMe.RunMethod(
    "setAudioSession"Null)
      vv.Initialize(
    "vv")
      vv.LoadVideoUrl(
    "http://stream-dc1.radioparadise.com/mp3-32")
      vv.Play
      NativeMe.RunMethod(
    "register"Null)
    End Sub

    Public Sub ControlEvent (Command As String)
       
    Select Command
         
    Case "play"
           vv.Play
         
    Case "pause"
           vv.Pause
       
    End Select
    End Sub



    #If OBJC
    @import MediaPlayer;
    #import <AVFoundation/AVFoundation.h>
    #import <AudioToolbox/AudioToolbox.h>
    - (void)setAudioSession {
      AVAudioSession *audioSession = [AVAudioSession sharedInstance];
      NSError *err = nil;
      BOOL success = [audioSession setCategory:AVAudioSessionCategoryPlayback error:&err];
      if (success) {
      success = [audioSession setActive:YES error:&err];
      }
      if (!success)
      [NSException raise:@"" format:@"Error setting audio session: %@", err];
    }
    - (void)register {
       MPRemoteCommandCenter* center = [MPRemoteCommandCenter sharedCommandCenter];
       center.playCommand.enabled = true;
       center.pauseCommand.enabled = true;
       [center.playCommand addTarget:self action:@selector(play)];
       [center.pauseCommand addTarget:self action:@selector(pause)];
    }
    - (void) play {
       NSLog(@"test");
       [self.bi raiseEvent:nil event:@"controlevent:"  params:@[@"play"]];
    }
    - (void) pause {
       [self.bi raiseEvent:nil event:@"controlevent:"  params:@[@"pause"]];
    }

    #end if
    The above code (depends on iMedia library and B4i v1.8+) will play the radio stream. You can leave your app and the streaming will continue.

    With this code the user can play and pause the playback from the bottom drawer.
    Note that it is supported by iOS 7.1+.
     
    Last edited: Apr 27, 2016
  2. ilan

    ilan Expert Licensed User

    this is great... just discovered this thread, did not knew its possible with b4i..
     
  3. yonson

    yonson Active Member Licensed User

    brilliant thank you so much for this, really essential for a lot of apps I'm srue
     
  4. susu

    susu Well-Known Member Licensed User

    How can I play online audio source with .m3u file?
     
  5. susu

    susu Well-Known Member Licensed User

    Sorry, .m3u file works fine for MP3, AAC codec not Ogg Vorbis.
     
  6. gudino jose luis

    gudino jose luis Active Member Licensed User

    Thank you so much for this...
     
  7. Erel

    Erel Administrator Staff Member Licensed User

  8. Pablo Torres

    Pablo Torres Active Member Licensed User

    Hi, I can't make the video viewer play a video

    I declared the VideoViewer in Sub Process_Globals
    <code>
    Private RepVideo As VideoView
    </code>

    I initializated the VideoViewer in the application start:
    <code>
    RepVideo.Initialize("ReproductorVideo")
    pagVideo.Initialize("pagVideo")
    pagVideo.RootPanel.Color = Colors.White
    pagVideo.RootPanel.LoadLayout("Video")
    pagVideo.RootPanel.AddView(RepVideo.View,0,0,100%x,100%y)
    RepVideo.View.Visible=True
    RepVideo.ShowControls=True
    </code>

    I added a button where I send the application to the pagVideo and this is the code for that button:
    <code>
    NavControl.ShowPage2(pagVideo,True)
    </code>

    I also added this code:
    <code>
    Sub pagVideo_Appear
    RepVideo.LoadVideoUrl(strVideo)
    Log(strVideo)
    RepVideo.View.Visible=True
    RepVideo.View.Alpha=255
    RepVideo.View.SetBorder(3,Colors.White,10)
    RepVideo.View.TintColor=Colors.White
    ud.ProgressDialogShow("Cargando Video")
    End Sub

    Sub ReproductorVideo_Ready
    ud.ProgressDialogHide
    RepVideo.Position=0
    RepVideo.Play
    End Sub
    </code>

    i dont understand what I'm doing wrong

    strvideo=""

    I will be very happy with any help
    Thanks
     
  9. Erel

    Erel Administrator Staff Member Licensed User

    Your question is not related to this thread. Please start a new one.
     
  10. Daniel Uribe

    Daniel Uribe Member Licensed User

    Greetings, here the next and previous buttons in background, result very efficient when you have a playlist.

    Code:
    Public Sub ControlEvent (Command AsString)
    Select Command
             
    Case"play"
                vv.Play
             
    Case"pause"
                vv.Pause
            
    Case "nextTrack"
                
    Log("next")
            
    Case "previousTrack"
                
    Log("previous")
        
    End Select
    End Sub



    #If OBJC
    @import MediaPlayer;
    #import <AVFoundation/AVFoundation.h>
    #import <AudioToolbox/AudioToolbox.h>
    - (void)setAudioSession {
      AVAudioSession *audioSession = [AVAudioSession sharedInstance];
      NSError *err = nil;
      BOOL success = [audioSession setCategory:AVAudioSessionCategoryPlayback error:&err];
      if (success) {
      success = [audioSession setActive:YES error:&err];
      }
      if (!success)
      [NSException raise:@"" format:@"Error setting audio session: %@", err];
      [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    }
    - (void)register {
       MPRemoteCommandCenter* center = [MPRemoteCommandCenter sharedCommandCenter];
       center.playCommand.enabled = true;
       center.pauseCommand.enabled = true;
       center.nextTrackCommand.enabled = true;
       center.previousTrackCommand.enabled = true;
       [center.playCommand addTarget:self action:@selector(play)];
       [center.pauseCommand addTarget:self action:@selector(pause)];
       [center.nextTrackCommand addTarget:self action:@selector(nextTrack)];
       [center.previousTrackCommand addTarget:self action:@selector(previousTrack)];
    }
    - (void) play {
       NSLog(@"test");
       [self.bi raiseEvent:nil event:@"controlevent:"  params:@[@"play"]];
    }
    - (void) pause {
       [self.bi raiseEvent:nil event:@"controlevent:"  params:@[@"pause"]];
    }
    - (void) nextTrack {
       [self.bi raiseEvent:nil event:@"controlevent:"  params:@[@"nextTrack"]];
    }
    - (void) previousTrack {
       [self.bi raiseEvent:nil event:@"controlevent:"  params:@[@"previousTrack"]];
    }
    - (void)remoteControlReceivedWithEvent:(UIEvent *)receivedEvent {
      if (receivedEvent.type == UIEventTypeRemoteControl) {
         [self.bi raiseEvent:nil event:@"remotecontrol_event:"
           params:@[@(receivedEvent.subtype)]];
       }
    }

    #end if
     
    ilan, JordiCP and Erel like this.
  11. cloner7801

    cloner7801 Active Member Licensed User

    How can I add name and image to controll center music player ?
     
  12. Erel

    Erel Administrator Staff Member Licensed User

    Are you using VideoView? What are you playing?
     
  13. cloner7801

    cloner7801 Active Member Licensed User

    yes , I use videoView to stream mp3 file and play
     
  14. Erel

    Erel Administrator Staff Member Licensed User

    1. Add this code to the OBJC block:
    Code:
    - (MPMediaItemArtwork*)createArtwork:(UIImage*) image {
       
    return [[MPMediaItemArtwork alloc] initWithBoundsSize:image.size requestHandler:^UIImage* _Nonnull(CGSize aSize) { return image}];
    }
    2. Set title and artwork:
    Code:
    Dim artwork As NativeObject = NativeMe.RunMethod("createArtwork:"Array(LoadBitmapResize(File.DirAssets, "b4i_128_128.png"6464True)))
       SetPlayingInfo(CreateMap(
    "title""Radio Paradise""artwork": artwork))
    End Sub

    Sub SetPlayingInfo (info As Map)
       
    Dim NowPlayingInfoCenter As NativeObject
       NowPlayingInfoCenter = NowPlayingInfoCenter.Initialize(
    "MPNowPlayingInfoCenter").RunMethod("defaultCenter"Null)
       
    Dim no As NativeObject = info
       NowPlayingInfoCenter.SetField(
    "nowPlayingInfo", no.RunMethod("ToDictionary"Null))
    End Sub
     
    Daniel Uribe likes this.
Loading...