티스토리 뷰
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jwsoft.kotlinproject">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:usesCleartextTraffic="true"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
<uses-permission android:name="android.permission.INTERNET"/> 인터넷 권한 추가
android:usesCleartextTraffic="true" 추가 (에뮬레이터에서 실행하기 위함)
MainActivity.kt
package com.jwsoft.kotlinproject
import android.media.MediaPlayer
import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.SurfaceHolder
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// 소리만 들리는 플레이어 방법 1
var mediaPlayer = MediaPlayer()
mediaPlayer.setDataSource("http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4")
mediaPlayer.prepare()
mediaPlayer.start()
// 소리만 들리는 플레이어 방법 2
var uri = Uri.parse("http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4")
var mediaPlayer2 = MediaPlayer.create(this, uri)
mediaPlayer2.start()
// 소리만 들리는 플레이어 방법 3
var mediaPlayer3 = MediaPlayer.create(this, R.raw.sample)
mediaPlayer3.start()
}
}
실행하면 소리만 재생된다.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
tools:context=".MainActivity">
<SurfaceView
android:id="@+id/surfaceView"
android:layout_width="400dp"
android:layout_height="240dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Activity_main.xml
package com.jwsoft.kotlinproject
import android.media.MediaPlayer
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.SurfaceHolder
import kotlinx.android.synthetic.main.activity_main.*
import java.lang.Exception
class MainActivity : AppCompatActivity(), SurfaceHolder.Callback {
private lateinit var surfaceHolder: SurfaceHolder
private lateinit var mediaPlayer: MediaPlayer
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
mediaPlayer = MediaPlayer()
surfaceHolder = surfaceView.holder
surfaceHolder.addCallback(this)
}
override fun surfaceCreated(holder: SurfaceHolder?) {
try {
mediaPlayer.setDataSource("http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4")
mediaPlayer.setDisplay(holder)
mediaPlayer.prepare()
mediaPlayer.start()
} catch (e: Exception) {
Log.e("", e.toString())
}
}
override fun surfaceChanged(holder: SurfaceHolder?, format: Int, width: Int, height: Int) {
}
override fun surfaceDestroyed(holder: SurfaceHolder?) {
}
override fun onDestroy() {
super.onDestroy()
mediaPlayer.release()
}
}
'Android > Kotlin' 카테고리의 다른 글
[Kotlin] for each (0) | 2020.08.20 |
---|---|
[Kotlin] forEach (0) | 2020.08.20 |
[Kotlin] ExoPlayer (0) | 2020.08.15 |
[Kotlin] VideoView (0) | 2020.08.15 |
[Kotlin] SeekBar (0) | 2020.08.15 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- TabLayout
- ViewPager2
- CoordinatorLayout
- Android
- fragment
- 코틀린
- JSONArray
- ViewModel
- handler
- 자바
- coroutine
- 안드로이드
- Architecture Pattern
- Kotlin
- 혀가 길지 않은 개발자
- 안드로이드 #코틀린 #Android #Kotlin
- java
- Vue.js #Vue.js + javascript
- Intent
- MVVM
- JSONObject
- activity
- DataBinding
- View
- XML
- recyclerview
- Livedata
- Design Pattern
- James Kim
- ArrayList
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
글 보관함