728x90
반응형
https://developer.android.com/guide/topics/media/mediaplayer?hl=ko
song1.mp3 raw 폴더에 붙여넣기
package com.androidappstudy.seekbarmusicapp;
import androidx.appcompat.app.AppCompatActivity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Switch;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final MediaPlayer mPlayer;
mPlayer = MediaPlayer.create(this, R.raw.song1);
final Switch switch1 = findViewById(R.id.switch1);
switch1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
if (switch1.isChecked() == true)
mPlayer.start();
else {
mPlayer.stop();
}
}
});
}
}
makeThread alt+ shift +enter 누르면
void makeTread 함수가 생성된다.
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat 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:orientation="vertical"
tools:context=".MainActivity">
<Switch
android:id="@+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="음악듣기" />
<SeekBar
android:id="@+id/pmMP3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
/>
</androidx.appcompat.widget.LinearLayoutCompat>
package com.androidappstudy.seekbarmusicapp;
import androidx.appcompat.app.AppCompatActivity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.widget.SeekBar;
import android.widget.Switch;
public class MainActivity extends AppCompatActivity {
MediaPlayer mPlayer;
SeekBar pbMP3;
Switch switch1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("SeekBar Music 앱!");
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setIcon(R.drawable.ic_launcher_foreground);
pbMP3 = findViewById(R.id.pmMP3);
switch1 = findViewById(R.id.switch1);
switch1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (switch1.isChecked() == true){
mPlayer = MediaPlayer.create(getApplicationContext(), R.raw.song1);
mPlayer.start();
makeThread();
}else {
mPlayer.stop();
}
}
});
pbMP3.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (fromUser){
mPlayer.seekTo(progress);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
void makeThread() {
new Thread(){
@Override
public void run() {
//음악이 계속 작동 중이라면
while (mPlayer.isPlaying()){
//음악의 시간을 최대로 설정
pbMP3.setMax(mPlayer.getDuration());
// 현재 위치를 지정
pbMP3.setProgress(mPlayer.getCurrentPosition());
SystemClock.sleep(100);
}
pbMP3.setProgress(0);
}
}.start();
}
}
노래가 나온다!
728x90
반응형
'☭DEVELOPER > #2 웹개발(자바기반 풀스택)' 카테고리의 다른 글
[BACKEND]컴포넌트의 라이프 사이클 이해 (1) | 2023.10.18 |
---|---|
[FRONTEND]리액트 JSX와 컴포넌트2 (1) | 2023.10.18 |
[FRONTEND]리액트 ES6 문법 이해 (0) | 2023.10.17 |
[BACKEND]리소스 파일과 저장폴더 (0) | 2023.10.17 |
[BACKEND]안드로이드앱 아이콘 변경 (1) | 2023.10.17 |