Platforms to show: All Mac Windows Linux Cross-Platform
Required plugins for this example: MBS MacCF Plugin, MBS MacCG Plugin, MBS Main Plugin, MBS MacBase Plugin, MBS iOS Plugin, MBS AVFoundation Plugin
Last modified Wed, 10th Dec 2024.
You find this example project in your MBS Xojo Plugin download as a Xojo project file within the examples folder: /AVFoundation/AudioPlayer
Download this example: AudioPlayer.zip
Project "AudioPlayer.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Löschen"
Const kFileQuit = "Beenden"
Const kFileQuitShortcut = ""
End Class
Class Window1 Inherits Window
Control SelectButton Inherits BevelButton
ControlInstance SelectButton Inherits BevelButton
EventHandler Sub Action()
dim f as FolderItem = GetOpenFolderItem(FileTypes1.All)
if f = nil then Return
dim e as NSErrorMBS
player = new AVAudioPlayerMBS(f, e)
if e<>Nil then
MsgBox e.LocalizedDescription
else
player.meteringEnabled = true
end if
End EventHandler
End Control
Control PlayButton Inherits BevelButton
ControlInstance PlayButton Inherits BevelButton
EventHandler Sub Action()
If player.play Then
// ok
updateInfoCenter
else
MsgBox "failed to play."
end if
End EventHandler
End Control
Control PauseButton Inherits BevelButton
ControlInstance PauseButton Inherits BevelButton
EventHandler Sub Action()
player.pause
updateInfoCenter
End EventHandler
End Control
Control StopButton Inherits BevelButton
ControlInstance StopButton Inherits BevelButton
EventHandler Sub Action()
player.stop
updateInfoCenterClear
End EventHandler
End Control
Control Slider1 Inherits Slider
ControlInstance Slider1 Inherits Slider
EventHandler Sub ValueChanged()
player.volume = me.Value / 100.0
End EventHandler
End Control
Control Label1 Inherits Label
ControlInstance Label1 Inherits Label
End Control
Control bar Inherits ProgressBar
ControlInstance bar(0) Inherits ProgressBar
ControlInstance bar(1) Inherits ProgressBar
End Control
Control Label2 Inherits Label
ControlInstance Label2 Inherits Label
End Control
Control Timer1 Inherits Timer
ControlInstance Timer1 Inherits Timer
EventHandler Sub Action()
if player = nil then Return
if player.duration > 0 then
pos.Value = player.currentTime / player.duration * 200.0
else
pos.Value = 0
end if
End EventHandler
End Control
Control Label3 Inherits Label
ControlInstance Label3 Inherits Label
End Control
Control pos Inherits ProgressBar
ControlInstance pos Inherits ProgressBar
End Control
Control Timer2 Inherits Timer
ControlInstance Timer2 Inherits Timer
EventHandler Sub Action()
if player = nil then Return
player.updateMeters
dim c as integer = player.numberOfChannels
if c > 2 then c = 2
for i as integer = 0 to c-1
dim d as double = player.peakPowerForChannel(i)
bar(i).Value = d+160
if bar(i).Visible = false then
bar(i).Visible = true
end if
next
for i as integer = c to 1
if bar(i).Visible then
bar(i).Visible = false
end if
next
End EventHandler
End Control
Control Slider2 Inherits Slider
ControlInstance Slider2 Inherits Slider
EventHandler Sub ValueChanged()
player.pan = me.Value / 100.0
End EventHandler
End Control
Control Label4 Inherits Label
ControlInstance Label4 Inherits Label
End Control
EventHandler Sub Open()
Dim logo As Picture = LogoMBS(500)
Artwork = New MPMediaItemArtworkMBS(logo)
End EventHandler
Sub updateInfoCenter()
Dim nowPlayingInfo As New Dictionary
nowPlayingInfo.Value(MPNowPlayingInfoCenterMBS.NowPlayingInfoPropertyElapsedPlaybackTime) = player.CurrentTime
nowPlayingInfo.Value(MPNowPlayingInfoCenterMBS.NowPlayingInfoPropertyPlaybackRate) = player.Rate
nowPlayingInfo.Value(MPNowPlayingInfoCenterMBS.NowPlayingInfoPropertyMediaType) = MPNowPlayingInfoCenterMBS.MediaTypeAudio
nowPlayingInfo.Value(MPNowPlayingInfoCenterMBS.MediaItemPropertyPlaybackDuration) = player.Duration
nowPlayingInfo.Value(MPNowPlayingInfoCenterMBS.MediaItemPropertyTitle) = "Hello World"
'nowPlayingInfo.Value(MPNowPlayingInfoCenterMBS.MediaItemPropertyMediaType) =
nowPlayingInfo.Value(MPNowPlayingInfoCenterMBS.MediaItemPropertyTitle) = "World Tour"
nowPlayingInfo.Value(MPNowPlayingInfoCenterMBS.MediaItemPropertyArtist) = "Great Singer"
nowPlayingInfo.Value(MPNowPlayingInfoCenterMBS.MediaItemPropertyArtwork) = Artwork
nowPlayingInfo.Value(MPNowPlayingInfoCenterMBS.MediaItemPropertyGenre) = "Rock"
Dim defaultCenter As MPNowPlayingInfoCenterMBS = MPNowPlayingInfoCenterMBS.defaultCenter
defaultCenter.nowPlayingInfo = nowPlayingInfo
If player.isPlaying Then
defaultCenter.playbackState = defaultCenter.PlaybackStatePlaying
Else
defaultCenter.playbackState = defaultCenter.PlaybackStatePaused
End If
End Sub
Sub updateInfoCenterClear()
// clear by setting to nil
Dim defaultCenter As MPNowPlayingInfoCenterMBS = MPNowPlayingInfoCenterMBS.defaultCenter
defaultCenter.nowPlayingInfo = Nil
defaultCenter.playbackState = defaultCenter.PlaybackStateStopped
End Sub
Property Artwork As MPMediaItemArtworkMBS
Property player As AVAudioPlayerMBS
End Class
MenuBar MenuBar1
MenuItem FileMenu = "&Ablage"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Bearbeiten"
MenuItem EditUndo = "&Rückgängig"
MenuItem UntitledMenu1 = "-"
MenuItem EditCut = "&Ausschneiden"
MenuItem EditCopy = "&Kopieren"
MenuItem EditPaste = "&Einfügen"
MenuItem EditClear = "#App.kEditClear"
MenuItem UntitledMenu0 = "-"
MenuItem EditSelectAll = "&Alles auswählen"
End MenuBar
FileTypes1
Filetype Mp3
Filetype AudioM4a
Filetype AudioM4b
Filetype AudioMpeg
Filetype AudioMpeg3
Filetype AudioAac
Filetype AudioAiff
End FileTypes1
Sign
End Sign
End Project
Download this example: AudioPlayer.zip
The items on this page are in the following plugins: MBS AVFoundation Plugin.