티스토리 뷰
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.java
package com.jwsoft.javaproject;
import androidx.appcompat.app.AppCompatActivity;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
private MediaPlayer mediaPlayer;
private MediaPlayer mediaPlayer2;
private MediaPlayer mediaPlayer3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
// 소리만 나오는 플레이어 방법 1
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource("http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4");
mediaPlayer.prepare();
mediaPlayer.start();
// 소리만 나오는 플레이어 방법 2
Uri uri = Uri.parse("http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4");
mediaPlayer2 = MediaPlayer.create(this, uri);
mediaPlayer2.start();
// 소리만 나오는 플레이어 방법 3
mediaPlayer3 = MediaPlayer.create(this, R.raw.sample);
mediaPlayer3.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
실행하면 소리만 재생된다.
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>
MainActivity.java
package com.jwsoft.javaproject;
import androidx.appcompat.app.AppCompatActivity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import java.io.IOException;
public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback {
private MediaPlayer mediaPlayer;
private SurfaceHolder surfaceHolder;
private SurfaceView surfaceView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mediaPlayer = new MediaPlayer();
surfaceView = findViewById(R.id.surfaceView);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
mediaPlayer.setDataSource("http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4");
mediaPlayer.setDisplay(holder);
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
@Override
protected void onDestroy() {
super.onDestroy();
mediaPlayer.release();
mediaPlayer = null;
}
}
'Android > Java' 카테고리의 다른 글
[Java] String.replace() (0) | 2020.08.20 |
---|---|
[Java] for each (0) | 2020.08.20 |
[Java] ExoPlayer (0) | 2020.08.15 |
[Java] VideoView (0) | 2020.08.15 |
[Java] SeekBar (0) | 2020.08.15 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- James Kim
- coroutine
- 안드로이드
- Architecture Pattern
- View
- 안드로이드 #코틀린 #Android #Kotlin
- Kotlin
- 혀가 길지 않은 개발자
- Livedata
- Intent
- ArrayList
- JSONObject
- Design Pattern
- handler
- DataBinding
- JSONArray
- 자바
- TabLayout
- activity
- XML
- java
- fragment
- MVVM
- 코틀린
- ViewModel
- recyclerview
- Vue.js #Vue.js + javascript
- CoordinatorLayout
- ViewPager2
- Android
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함