参考書に従い、音声をバックグラウンドで出力し、音声の終了時に通知を表示したいのですが
通知がされません。
見直したものの参考書との差異はないように思います。
間違っている箇所がありましたら、ご指摘をお願いいたします。
xml
1<?xml version="1.0" encoding="utf-8"?> 2<LinearLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:orientation="vertical"> 7 8 <Button 9 android:id="@+id/btPlay" 10 android:layout_width="match_parent" 11 android:layout_height="wrap_content" 12 android:onClick="onPlayButtonClick" 13 android:text="@string/bt_play_play"/> 14 15 <Button 16 android:id="@+id/btStop" 17 android:layout_width="match_parent" 18 android:layout_height="wrap_content" 19 android:enabled="false" 20 android:onClick="onStopButtonClick" 21 android:text="@string/bt_play_stop"/> 22 23 24</LinearLayout>
xml
1<?xml version="1.0" encoding="utf-8"?> 2<manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.websarva.wings.android.servicesample"> 4 5 <application 6 android:allowBackup="true" 7 android:icon="@mipmap/ic_launcher" 8 android:label="@string/app_name" 9 android:roundIcon="@mipmap/ic_launcher_round" 10 android:supportsRtl="true" 11 android:theme="@style/AppTheme"> 12 <service 13 android:name=".SoundManagerService" 14 android:enabled="true" 15 android:exported="false"></service> 16 17 <activity android:name=".MainActivity"> 18 <intent-filter> 19 <action android:name="android.intent.action.MAIN" /> 20 21 <category android:name="android.intent.category.LAUNCHER" /> 22 </intent-filter> 23 </activity> 24 </application> 25 26</manifest>
kotlin
1package com.websarva.wings.android.servicesample 2 3import android.content.Intent 4import android.support.v7.app.AppCompatActivity 5import android.os.Bundle 6import android.view.View 7import android.widget.Button 8 9class MainActivity : AppCompatActivity() { 10 11 override fun onCreate(savedInstanceState: Bundle?) { 12 super.onCreate(savedInstanceState) 13 setContentView(R.layout.activity_main) 14 } 15 16 fun onPlayButtonClick(view: View) { 17 val intent = Intent(applicationContext, SoundManagerService::class.java) 18 startService(intent) 19 20 val btPlay = findViewById<Button>(R.id.btPlay) 21 val btStop = findViewById<Button>(R.id.btStop) 22 23 btPlay.isEnabled = false 24 btStop.isEnabled = true 25 } 26 27 fun onStopButtonClick(view: View) { 28 val intent = Intent(applicationContext, SoundManagerService::class.java) 29 stopService(intent) 30 31 val btPlay = findViewById<Button>(R.id.btPlay) 32 val btStop = findViewById<Button>(R.id.btStop) 33 34 btPlay.isEnabled = true 35 btStop.isEnabled = false 36 } 37} 38
kotlin
1package com.websarva.wings.android.servicesample 2 3import android.app.NotificationChannel 4import android.app.NotificationManager 5import android.app.Service 6import android.content.Context 7import android.content.Intent 8import android.media.MediaPlayer 9import android.net.Uri 10import android.os.IBinder 11import android.support.v4.app.NotificationCompat 12import android.util.Log 13import android.view.View 14import java.io.IOException 15import java.lang.IllegalArgumentException 16 17class SoundManagerService : Service() { 18 19 override fun onBind(intent: Intent): IBinder { 20 TODO("Return the communication channel to the service.") 21 } 22 23 private var _player: MediaPlayer? = null 24 override fun onCreate() { 25 _player = MediaPlayer() 26 27 val id = "soundmanagerservice_notification_channel" 28 val name = getString(R.string.notification_channel_name) 29 val importance = NotificationManager.IMPORTANCE_DEFAULT 30 val channel = NotificationChannel(id, name, importance) 31 32 val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager 33 manager.createNotificationChannel(channel) 34 } 35 36 override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int { 37 38 val mediaFileUriStr = "android.resource://${packageName}/${R.raw.mountain_stream}" 39 val mediaFileUri = Uri.parse(mediaFileUriStr) 40 41 try { 42 _player?.setDataSource(applicationContext, mediaFileUri) 43 _player?.setOnPreparedListener(PlayerPreparedListener()) 44 _player?.setOnCompletionListener(PlayerCompletionListener()) 45 _player?.prepareAsync() 46 } catch (ex: IllegalArgumentException) { 47 Log.e("ServiceSample", "メディアプレーヤー準備時の例外発生") 48 } catch (ex: IOException) { 49 Log.e("ServiceSample", "メディアプレーヤー準備時の例外発生") 50 } 51 52 return Service.START_NOT_STICKY 53 } 54 55 override fun onDestroy() { 56 57 _player?.let { 58 if (it.isPlaying) { 59 it.stop() 60 } 61 62 it.release() 63 _player = null 64 } 65 } 66 67 private inner class PlayerPreparedListener : MediaPlayer.OnPreparedListener { 68 override fun onPrepared(mp: MediaPlayer) { 69 mp.start() 70 } 71 } 72 73 private inner class PlayerCompletionListener : MediaPlayer.OnCompletionListener { 74 override fun onCompletion(mp: MediaPlayer) { 75 val builder = NotificationCompat.Builder(applicationContext, "soundmanagerservice_notification_channel") 76 builder.setSmallIcon(android.R.drawable.ic_dialog_info) 77 builder.setContentTitle(getString(R.string.msg_notification_title_finish)) 78 builder.setContentText(getString(R.string.msg_notification_text_finish)) 79 80 val notification = builder.build() 81 val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager 82 manager.notify(0, notification) 83 84 stopSelf() 85 } 86 } 87 88 89} 90
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。