iOS Question How to get the current position for AVPlayer?

aberezhnykh

Member
Licensed User
Longtime User
To play remote mp3 file I use AVPlayer. How can I get the value currentTime and adjust the volume?
 
Last edited:

aberezhnykh

Member
Licensed User
Longtime User
Hi Erel! This is my code. I can not convert NSTimer data.
Thank you for any help!


B4X:
'Code module
#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
    #ATSEnabled: True
    #MinVersion: 7
#End Region
#PlistExtra:<key>NSAppTransportSecurity</key><dict><key>NSAllowsArbitraryLoads</key><true/></dict>

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public App As Application
    Public NavControl As NavigationController
    Private Page1 As Page
    Private Timer1 As Timer
    Private player1 As NativeObject
End Sub

Private Sub Application_Start (Nav As NavigationController)
    NavControl = Nav
    Page1.Initialize("Page1")
    Page1.Title = "Page 1"
    Page1.RootPanel.Color = Colors.White
    NavControl.ShowPage(Page1)
 
    player1 = CreatePlayer("http://www.mysite.com/mp3")
     
    player1.RunMethod("play", Null)
 
    Timer1.Initialize("Timer1", 1000)
    Timer1.Enabled = True
End Sub

Private Sub Timer1_Tick
 
    Dim status As Int = player1.GetField("status").AsNumber
    Log ( "status " & status )

    Dim rate As Int = player1.GetField("rate").AsNumber
    Log ( "rate " & rate )
 
    Log ( "currentTime: " & player1.RunMethod("currentTime", Null) ) '???
 
End Sub

Sub CreatePlayer(url As String) As NativeObject
    Dim u As NativeObject
    u = u.Initialize("NSURL").RunMethod("URLWithString:", Array(url))
    Dim player As NativeObject
    player = player.Initialize("AVPlayer").RunMethod("alloc", Null).RunMethod("initWithURL:", Array(u))
    Return player
End Sub
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Current time:
B4X:
Private Sub Timer1_Tick
 
   Dim status As Int = player1.GetField("status").AsNumber
   Log ( "status " & status )

   Dim rate As Int = player1.GetField("rate").AsNumber
   Log ( "rate " & rate )
   Dim no As NativeObject = Me
   
   Log ( "currentTime: " & no.RunMethod("GetTime:", Array(player1)).AsNumber)
 
End Sub

#if OBJC
@import CoreMedia;
@import AVFoundation;
- (double)GetTime:(AVPlayer*)player {
   return CMTimeGetSeconds([player.currentItem currentTime]);
}
#End If

Volume:
B4X:
player1.SetField("volume", 0.1)
 
Upvote 0
Top