Android pri (22)

Android Basics: Multiscreen Apps

Lesson 4: Audio & Libraries(2)

[TOC]

Async Callbacks

  • 这个就是异步的调用方法,在MediaPlayer中也有很多的Callbacks。就是说使用这个异步的调用方法的话,你就不用等它完成了才能去做其他事情,你可以在它返回给你消息的时候去做另外的事情。
  • Markdown
  • 这里我们用到了setOnCompletionListener,这个方法是当一个media source播放完成的时候才会被调用。
  • 示例代码如下:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    playBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    mediaPlayer.start();
    mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
    @Override
    public void onCompletion(MediaPlayer mp) {
    Toast.makeText(MainActivity.this, "I'm Done!", Toast.LENGTH_SHORT).show();
    }
    });
    }
    });

Clean Up Media Resources

  • 因为我们同时要播放音频,所以当我们的音频已经播放完或是我们在另一个时候点击其他的音频的时候,我们需要释放掉当前的,然后去播放当前的音频。
  • 已经播放完的话可以使用上面介绍的OnCompletionListener来进行判断,如果播放完就releaseMediaPlayer();
  • 代码实现:
  • NumbersActivity.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
    package com.example.android.miwok;
    import android.media.MediaPlayer;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.ListView;
    import java.util.ArrayList;
    public class NumbersActivity extends AppCompatActivity {
    private MediaPlayer mediaPlayer;
    /**
    * 这个Listener是当这个media被放完的时候进行调用的!
    */
    private MediaPlayer.OnCompletionListener mCompletionListener = new MediaPlayer.OnCompletionListener() {
    @Override
    public void onCompletion(MediaPlayer mediaPlayer) {
    releaseMediaPlayer();
    }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.word_list);
    ArrayList<Word> words = new ArrayList<Word>();
    words.add(new Word("one", "lutti", R.drawable.number_one, R.raw.number_one));
    words.add(new Word("two", "otiiko", R.drawable.number_two, R.raw.number_two));
    words.add(new Word("three", "tolookosu", R.drawable.number_three, R.raw.number_three));
    words.add(new Word("four", "oyyisa", R.drawable.number_four, R.raw.number_four));
    words.add(new Word("five", "massokka", R.drawable.number_five, R.raw.number_five));
    words.add(new Word("six", "temmokka", R.drawable.number_six, R.raw.number_six));
    words.add(new Word("seven", "kenekaku", R.drawable.number_seven, R.raw.number_seven));
    words.add(new Word("eight", "kawinta", R.drawable.number_eight, R.raw.number_eight));
    words.add(new Word("nine", "wo'e", R.drawable.number_nine, R.raw.number_nine));
    words.add(new Word("ten", "na'aacha", R.drawable.number_ten, R.raw.number_ten));
    WordAdapter wordAdapter = new WordAdapter(this, words, R.color.category_numbers);
    ListView listView = (ListView) findViewById(R.id.list);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
    releaseMediaPlayer();
    Word word = (Word)adapterView.getItemAtPosition(i);
    int songResourceID = word.getSongResourceID();
    mediaPlayer = MediaPlayer.create(NumbersActivity.this, songResourceID);
    mediaPlayer.start();
    mediaPlayer.setOnCompletionListener(mCompletionListener);
    }
    });
    // 这样我们就能够往其中进行填充数据了!
    listView.setAdapter(wordAdapter);
    }
    private void releaseMediaPlayer() {
    // If the media player is not null, then it may be currently playing a sound.
    if (mediaPlayer != null) {
    mediaPlayer.release();
    mediaPlayer = null;
    }
    }
    }

热评文章