Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Androidでオーディオアプリを作るということ

rmakiyama
September 26, 2020

 Androidでオーディオアプリを作るということ

Androidでオーディオアプリを作るときに考慮すべきところなどを簡単に。

rmakiyama

September 26, 2020
Tweet

More Decks by rmakiyama

Other Decks in Technology

Transcript

  1. Ի੠Λ࠶ੜ͢Δʹ͸ʁ • AudioTrack • ੜͷԻ੠σʔλΛѻ͑Δ • ࠷΋஗Ԇ͕গͳ͍ • ѻ͍͕೉͍͠… •

    SoundPool • ୹͍Ի੠ʹద͍ͯ͠Δ • ޮՌԻͳͲ • ෛՙ͕௿͍ • ஗Ԇ͕গͳ͍
  2. Ի੠Λ࠶ੜ͢Δʹ͸ʁ • MediaPlayer • ࠷΋Ұൠత • ө૾ʹ΋ରԠ • ѻ͍͕؆୯ •

    HLSະରԠ • ExoPlayer • Google੡ͷϥΠϒϥϦ • ө૾ʹ΋ରԠ • ΧελϚΠζੑ͕ߴ͍ • HLS΍DASH౳ʹରԠ
  3. ExoPlayer val audioUri = Uri.parse(“https:"//sample.jp/media/media1.mp3”) val player = SimpleExoPlayer.Builder(context).build() val

    sourceFactory = DefaultDataSourceFactory( context, Util.getUserAgent(context, "sample") ) val extractorsFactory = DefaultExtractorsFactory() val source = ProgressiveMediaSource.Factory(sourceFactory, extractorsFactory) .createMediaSource(audioUri) player.prepare(source)
  4. MediaBrowserService(session/player) class MyMediaService : MediaBrowserServiceCompat() { private lateinit var mediaSession:

    MediaSessionCompat private lateinit var player: Player override fun onCreate() { super.onCreate() mediaSession = MediaSessionCompat(this, "MyMediaService").apply { … setCallback(myCallback) ɹɹɹɹɹɹ …
  5. MediaBrowserService(session/player) override fun onCreate() { … } private val myCallback

    = object : MediaSessionCompat.Callback() { override fun onPrepare() {} override fun onPlay() {} override fun onPause() {} override fun onStop() {} … }
  6. MediaBrowserService(session/player) … override fun onGetRoot( clientPackageName: String, clientUid: Int, rootHints:

    Bundle? ): BrowserRoot? { "// ઀ଓ͞ΕͨΫϥΠΞϯτͷݕূΛ͢Δ "// ઀ଓΛڋ൱͢Δͱ͖͸nullΛฦ͢ return BrowserRoot("root", null) }
  7. MediaBrowserService(session/player) …
 override fun onLoadChildren( parentId: String, result: Result<MutableList<MediaBrowserCompat.MediaItem">> )

    { "// ΫϥΠΞϯτʹϝσΟΞͷϦετΛฦ͢ val list = getMetadata() result.sendResult(list) } …
  8. Activity(UI/controller) class MediaPlayerActivity : AppCompatActivity() { private lateinit var mediaBrowser:

    MediaBrowserCompat private lateinit var mediaController: MediaControllerCompat override fun onCreate(savedInstanceState: Bundle?) { … mediaBrowser = MediaBrowserCompat( this, ComponentName(this, MyMediaService"::class.java), connectionCallbacks, null
  9. Activity(UI/controller) class MediaPlayerActivity : AppCompatActivity() { private lateinit var mediaBrowser:

    MediaBrowserCompat private lateinit var mediaController: MediaControllerCompat override fun onCreate(savedInstanceState: Bundle?) { … } override fun onStart() { super.onStart() "// MediaBrowserServiceʹ઀ଓ mediaBrowser.connect()
  10. Activity(UI/controller) class MediaPlayerActivity : AppCompatActivity() { … private lateinit var

    mediaController: MediaControllerCompat … private val connectionCallbacks = object : MediaBrowserCompat.ConnectionCallback() { override fun onConnected() { "// sessionͷτʔΫϯ͔ΒcontrollerΛੜ੒ mediaController = MediaControllerCompat(this@MediaPlayerActivity, mediaBrowser.sessionToken) .apply { registerCallback(controllerCallback) } "// UIͷૢ࡞ʹԠͯ͡controllerΛհͯ͠ΦʔσΟΦΛૢ࡞ "// ྫ) ࠶ੜɿmediaController.transportControls.play()
  11. Activity(UI/controller) class MediaPlayerActivity : AppCompatActivity() { … private var controllerCallback

    = object : MediaControllerCompat.Callback() { override fun onMetadataChanged(metadata: MediaMetadataCompat?) { "// ࠶ੜதͷίϯςϯπ৘ใͷมԽʹԠͯ͡UIΛߋ৽ } override fun onPlaybackStateChanged(state: PlaybackStateCompat?) { "// ࠶ੜͷঢ়ଶ(࠶ੜத΍ఀࢭ)ͷมԽʹԠͯ͡UIΛߋ৽͢Δ } }