Stream an audio file with MediaPlayer 오디오 파일 스트리밍 하기
Android 2019. 10. 21. 20:55 |반응형
You can stream an audio file with MediaPlayer.
<AndroidManifest.xml>
1 | <uses-permission android:name="android.permission.INTERNET"/> |
<MainActivity.java>
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | public class MainActivity extends AppCompatActivity { MediaPlayer mediaPlayer; int position = 0; boolean isPaused = false; // To prevent false resume. @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Play Button button = findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { if (mediaPlayer != null) { mediaPlayer.release(); } try { mediaPlayer = new MediaPlayer(); mediaPlayer.setDataSource("http://nexoft.tk/download/Davich.mp3"); // or your audio file url. mediaPlayer.prepare(); mediaPlayer.start(); isPaused = false; } catch (Exception e) { e.printStackTrace(); } Toast.makeText(getApplicationContext(), "Media player started", Toast.LENGTH_SHORT).show(); } }); // Stop Button button2 = findViewById(R.id.button2); button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (mediaPlayer != null) { mediaPlayer.stop(); isPaused = false; Toast.makeText(getApplicationContext(), "Media player stopped", Toast.LENGTH_SHORT).show(); } } }); // Pause Button button3 = findViewById(R.id.button3); button3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (mediaPlayer != null && mediaPlayer.isPlaying()) { position = mediaPlayer.getCurrentPosition(); mediaPlayer.pause(); isPaused = true; Toast.makeText(getApplicationContext(), "Media player paused at " + position / 1000 + " sec", Toast.LENGTH_SHORT).show(); } } }); // Resume Button button4 = findViewById(R.id.button4); button4.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (mediaPlayer != null && !mediaPlayer.isPlaying() && isPaused == true) { mediaPlayer.start(); mediaPlayer.seekTo(position); isPaused = false; Toast.makeText(getApplicationContext(), "Media player resumed at " + position / 1000 + " sec", Toast.LENGTH_SHORT).show(); } } }); } @Override protected void onDestroy() { super.onDestroy(); if (mediaPlayer != null) { mediaPlayer.release(); } } } |
Run the app and start the audio file.
반응형
'Android' 카테고리의 다른 글
Play Youtube videos with YouTubePlayer 유튜브 플레이어로 유튜브 영상 재생하기 (0) | 2019.10.24 |
---|---|
Stream a video file with VideoView 비디오 파일 스트리밍 하기 (0) | 2019.10.22 |
Take a photo with a camera app and save the full-size photo (0) | 2019.10.18 |
Take a photo with a camera app and get the thumbnail (0) | 2019.10.18 |
Get contact data 연락처 정보 가져오기 (0) | 2019.10.16 |