iT邦幫忙

第 12 屆 iT 邦幫忙鐵人賽

DAY 19
0
Mobile Development

菜雞 Android APP 從安裝 Android Studio 到上架 Google Play系列 第 19

Day19 - Spinner 選擇音樂藉由 Service 背景播放

Setting 畫面 我用 Java 寫

利用 Spinner 切換音樂

spinner.setAdapter(adapter);
spinner.setSelection(Integer.parseInt(Music_num[0]));
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    public void onItemSelected(AdapterView<?> parent, View View, int position, long id) {
        int b = parent.getSelectedItemPosition();
        Intent intent = new Intent(Setting.this,MusicService.class);
        intent.putExtra("i",b);
        startService(intent);

        try {
            method.ReWrite(getFilesDir()+"Volume.txt",String.valueOf(b),0);
        } catch (Exception v){
            v.printStackTrace();
        }

    }
    public void onNothingSelected(AdapterView<?> parent) {
    }
});

交給 MusicService 操作

@SuppressLint("Registered")
public class MusicService extends Service{

    private MediaPlayer music  ;
    int num ;
    int current_num;
    float volume;
    public IBinder onBind(Intent intent) {
        //throw new UnsupportedOperationException("Not yet implemented");
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        num = 0;
        current_num = num;
        music = new MediaPlayer();
        music = MediaPlayer.create(this,R.raw.music_1);
        music.setLooping(true);
        music.setAudioAttributes(new AudioAttributes.Builder()
                                                    .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
                                                    .build());
        music.setVolume(0.5f,0.5f);
        music.start();

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        int i = intent.getIntExtra("i",-1);


        if(num == -1){
            volume = intent.getFloatExtra("volume",-1);
            music.setVolume(volume,volume);
        }

        num = i;
        if(current_num != num) {                                                // -1 為音量 -2為暫停 -3為開始  -4為考試響鈴
                                                                                // 正整數為不同背景音樂
            if (num != -1) {
                music.reset();

                if(num == -3){
                    num = current_num;
                }

                if(num == -2){
                    music.pause();
                }

                switch (num) {
                    case 0:
                        current_num = 0;
                        music = MediaPlayer.create(this, R.raw.music_1);
                        music.setLooping(true);
                        music.start();
                        break;
                    case 1:
                        current_num = 1;
                        music = MediaPlayer.create(this, R.raw.music_2);
                        music.setLooping(true);
                        music.start();
                        break;
                    case 2:
                        current_num = 2;
                        music = MediaPlayer.create(this, R.raw.music_3);
                        music.setLooping(true);
                        music.start();
                        break;
                    case -4:
                        music = MediaPlayer.create(this,R.raw.exam_bell);
                        music.setLooping(false);
                        music.start();
                        break;
                    case -5:
                        music.release();
                        stopSelf();
                        break;
                }

            }
        }

        return super.onStartCommand(intent, flags, startId);
    }

}
```java


上一篇
Day18 - 利用 ObjectAnimator 跑動畫
下一篇
Day20 - SeekBar 控制音量
系列文
菜雞 Android APP 從安裝 Android Studio 到上架 Google Play30

尚未有邦友留言

立即登入留言