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

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
104
105
106
107
108
109
110
111
# File 'motion/media/player.rb', line 66

def play(content_url, options = {}, &block)
  options = {
    delay_play: false
  }.merge(options)

  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.init

  self.media_player.prepareToPlay if not display_modal

  set_player_options(options)
  self.media_player.setContentURL(content_url)

  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 ||= App.window.rootViewController
    if Device.ios_version < "7.0"
      @presenting_controller.presentMoviePlayerViewControllerAnimated(@media_player)
    else
      @presenting_controller.presentViewController(@media_player, animated:true, completion:nil)
    end
  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


114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'motion/media/player.rb', line 114

def stop
  if @media_player.is_a? MPMoviePlayerViewController
    if Device.ios_version < "7.0"
      @presenting_controller.dismissMoviePlayerViewControllerAnimated
    else
      @presenting_controller.dismissViewControllerAnimated(true, completion:nil)
    end
    @presenting_controller = nil
  else
    @media_player.stop
  end
  @media_player = nil
end