Class: BubbleWrap::Media::Player

Inherits:
Object
  • Object
show all
Defined in:
motion/media/player.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#media_playerObject (readonly)

Returns the value of attribute media_player.



9
10
11
# File 'motion/media/player.rb', line 9

def media_player
  @media_player
end

Instance Method Details

#play(content_url, options = {}, &block) ⇒ Object

the form

### These are properties of MPMoviePlayerController
allows_air_play: true/false; default false,
control_style: [MPMovieControlStyle]; default MPMovieControlStyleDefault,
end_playback_time: [Integer] end time (in seconds) for media; default is -1,
initial_playback_time: [Integer] start time (in seconds) for media; default is -1,
movie_source_type: [MPMovieSourceType] a "hint" so the player knows how to load the data type;
  either MPMovieSourceTypeFile or MPMovieSourceTypeStreaming; default is MPMovieSourceTypeUnknown
  which may delay playback,
repeat_mode: [MPMovieRepeatMode] how the player repeats at the end of playback; defautl is
  MPMovieRepeatModeNone
scaling_mode: [MPMovieScalingMode] scaling mode for movies; default is MPMovieScalingModeAspectFit
should_autoplay: true/false; default true,
use_application_audio_session: true/false; default true.

### These are properties of just the ::Player
delay_play: true/false, default false. If false then you have to manually call
  @media_player.play in your code
modal: true/false; default false,
controller: [UIViewController] used to present the player modally;
  default uses root view controller of window

EX

From a local URL:
  file = File.join(App.resources_path, 'test.mp3')
  BW::Media::Player.play(NSURL.fileURLWithPath(file)) do |media_player|
    media_player.view.frame = some_view.bounds
    self.view.addSubview media_player.view
  end

From a remote URL:
  BW::Media::Player.play("http://www.hrupin.com/wp-content/uploads/mp3/testsong_20_sec.mp3") do |media_player|
    media_player.view.frame = some_view.bounds
    self.view.addSubview media_player.view
  end

Parameters:

  • content_url (String, NSURL)

    is either a local or remote string for the location of the media you’re playing. NOTE: if you’re playing a remote file, your server needs to support range requests for that URL.

  • options (Hash) (defaults to: {})

    to open the MPMoviePlayerController with



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'motion/media/player.rb', line 66

def play(content_url, options = {}, &block)
  display_modal = !!options[:modal]

  klass = display_modal ? MPMoviePlayerViewController : MPMoviePlayerController

  content_url = content_url.is_a?(NSURL) ? content_url : NSURL.URLWithString(content_url)
  @media_player = klass.alloc.initWithContentURL(content_url)

  self.media_player.prepareToPlay if not display_modal

  options[:delay_play] = false if not options.has_key? :delay_play
  set_player_options(options)

  NSNotificationCenter.defaultCenter.observe MPMoviePlayerPlaybackDidFinishNotification do |notification|
    h = notification.userInfo
    error = h["error"]
    if error
      p "BW::Media::Player error: #{error.userInfo.inspect}"
      p "Code: #{error.code}, Domain: #{error.domain}"
    end
  end

  if display_modal
    @presenting_controller = options[:controller]
    @presenting_controller ||= UIApplication.sharedApplication.keyWindow.rootViewController
    @presenting_controller.presentMoviePlayerViewControllerAnimated(@media_player)
  else
    if block.nil?
      raise Error::NilPlayerCallback, "no block callback given in #play; you need\
                                        to supply one if options[:modal] == false"
    end
    block.call(@media_player)
  end

  if not display_modal and not options[:delay_play]
    @media_player.play
  end
end

#play_modal(content_url, options = {}) ⇒ Object

Plays media in the system-default modal controller Takes same parameters as #play NOTE: If you don’t supply a :controller option, the rootViewController will be used.



17
18
19
# File 'motion/media/player.rb', line 17

def play_modal(content_url, options = {})
  play(content_url, options.merge(modal: true))
end

#stopObject

Stops playback for a Media::Player



106
107
108
109
110
111
112
113
114
# File 'motion/media/player.rb', line 106

def stop
  if @media_player.is_a? MPMoviePlayerViewController
    @presenting_controller.dismissMoviePlayerViewControllerAnimated
    @presenting_controller = nil
  else
    @media_player.stop
  end
  @media_player = nil
end