한빛 미디어 안드로이드 프로그래밍정복을 학습하며 작성한 것임

정상적으로 동작하는 것은 1번 버튼만임.  낮은  apk버전에서만 정상독작함.

웹스트리밍은 웹은 잘되나 앱은 안됨

====================================================

package com.jstudio.music;

import android.annotation.SuppressLint;
import android.media.MediaParser;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import com.jstudio.music.R;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;

import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    MediaPlayer
mPlayer;
    String
mSdPath;

   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);

       
mPlayer = MediaPlayer.create(this, R.raw.dingdong);
       
mSdPath = Environment.getExternalStorageDirectory().getAbsolutePath();
    }



   
@SuppressLint("NonConstantResourceId")
   
public void mOnClick(View v){
        MediaPlayer player;
       
switch(v.getId()){
           
case R.id.btn1:
                player = MediaPlayer.create(
this,R.raw.dingdong);
                player.start();
               
break;
           
case R.id.btn2:
                player =
new MediaPlayer();
               
try {
                    player.setDataSource(
mSdPath +"/2.mp3");
                    player.prepare();
                    player.start();
                }
catch (Exception e){
                    Toast.makeText(
this,mSdPath,Toast.LENGTH_SHORT).show();
                }
               
break;
           
case R.id.btn3:
                player =
new MediaPlayer();
               
try {
                    Uri uri = Uri.parse(
"http://www.soen.kr/data/saemaul1.mp3");
                    player.setDataSource(
this,uri);
                    player.prepare();
                    player.start();
                }
catch (Exception e){
                    Toast.makeText(
this, "error :"+e.getMessage(),Toast.LENGTH_SHORT).show();
                }
               
break;
           
case R.id.btn4:
                
mPlayer.seekTo(0);
               
mPlayer.start();
               
break;
           
case R.id.btn5:
                player =
new MediaPlayer();
               
try {
                    player.setDataSource(
mSdPath +"/2.mp3");
                    player.start();
                }
catch (Exception e){
                    Toast.makeText(
this, "error :"+e.getMessage(),Toast.LENGTH_SHORT).show();
                }
               
break;
           
case R.id.btn6:
                player = MediaPlayer.create(
this, R.raw.dingdong);
               
try {
                    player.setDataSource(
mSdPath +"/eagles5.mp3");
                    player.prepare();
                    player.start();
                }
catch (IllegalArgumentException e){
                    Toast.makeText(
this, "IllegalArgumentException :"+e.getMessage(),Toast.LENGTH_SHORT).show();
                }
catch (IllegalStateException e){
                    Toast.makeText(
this, "IllegalStateException :"+e.getMessage(),Toast.LENGTH_SHORT).show();
                }
catch (IOException e){
                    Toast.makeText(
this, "IOException :"+e.getMessage(),Toast.LENGTH_SHORT).show();
                }
               
break;

        }

    }

   
@Override
   
protected void onDestroy() {
       
super.onDestroy();
       
if(mPlayer !=null){
           
mPlayer.release();
           
mPlayer = null;
        }
    }
}

 

===========================================================================

<?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"
   
>

    <
Button
       
android:id="@+id/btn1"
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:onClick="mOnClick"
       
android:text="리소스 재생"
       
/>
    <
Button
       
android:id="@+id/btn2"
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:onClick="mOnClick"
       
android:text="파일 재생"
       
/>
    <
Button
       
android:id="@+id/btn3"
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:onClick="mOnClick"
       
android:text="스트림 재생"
       
/>
    <
Button
       
android:id="@+id/btn4"
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:onClick="mOnClick"
       
android:text="객체로 선언 후 재생"
       
/>
    <
Button
       
android:id="@+id/btn5"
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:onClick="mOnClick"
       
android:text="준비안된 상태에서 재생"
       
/>
    <
Button
       
android:id="@+id/btn6"
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:onClick="mOnClick"
       
android:text="다른 파일 열기"
        
/>

</
androidx.appcompat.widget.LinearLayoutCompat>

+ Recent posts