Android pri (21)

Android Basics: Multiscreen Apps

Lesson 4: Audio & Libraries

MediaPlayer

  • References
  • 它可以帮助我们在Android中播放音频文件。

MediaPlayer States

  • 它在播放时是具有不同的状态的,我们可以用图表来进行表示。
  • Markdown
  • Idle:说明你当前是空闲的
  • Prepared: 就像你正在思考唱哪首歌一首歌一样
  • Started: 开始唱歌
  • Paused: 暂停唱歌
  • Stopped: 是完全停止唱歌,这个时候你不能够返回到Started的状态,只能是选择重新播放或者是放另外不同的歌。

Debug Tip

  • 我们可以使用toString方法来实现输出java类中的一些内容。
  • Markdown
  • 这样的话,你就可以直接在Log中打印一个Object对象了(因为直接输出对象名的话是默认调用toString方法的!)

一些代码

  • 为了能够加入歌曲的ResourceID,所以我们这里修改了一下Word.java这个类的构造方法,并且同时添加上去了getSongResourceID的方法!
    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
    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;
    @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) {
    Word word = (Word)adapterView.getItemAtPosition(i);
    int songResourceID = word.getSongResourceID();
    mediaPlayer = MediaPlayer.create(NumbersActivity.this, songResourceID);
    mediaPlayer.start();
    }
    });
    // 这样我们就能够往其中进行填充数据了!
    listView.setAdapter(wordAdapter);
    }
    }

  • Word.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
    package com.example.android.miwok;
    /**
    * Created by hclmaster on 2017/1/20.
    */
    public class Word {
    private String mDefaultTranslation;
    private String mMiwokTranslation;
    private int imageResourceID = -1;
    private int songResourceID = -1;
    public Word(String mDefaultTranslation, String mMiwokTranslation) {
    this.mDefaultTranslation = mDefaultTranslation;
    this.mMiwokTranslation = mMiwokTranslation;
    }
    public Word(String mDefaultTranslation, String mMiwokTranslation, int imageResourceID) {
    this.imageResourceID = imageResourceID;
    this.mDefaultTranslation = mDefaultTranslation;
    this.mMiwokTranslation = mMiwokTranslation;
    }
    public Word(String mDefaultTranslation, String mMiwokTranslation, int imageResourceID, int songResourceID) {
    this.mDefaultTranslation = mDefaultTranslation;
    this.mMiwokTranslation = mMiwokTranslation;
    this.imageResourceID = imageResourceID;
    this.songResourceID = songResourceID;
    }
    public String getmDefaultTranslation() {
    return mDefaultTranslation;
    }
    public String getmMiwokTranslation() {
    return mMiwokTranslation;
    }
    public int getImageResourceID() {
    return imageResourceID;
    }
    public int getSongResourceID() {
    return songResourceID;
    }
    public void setmDefaultTranslation(String mDefaultTranslation) {
    this.mDefaultTranslation = mDefaultTranslation;
    }
    public void setmMiwokTranslation(String mMiwokTranslation) {
    this.mMiwokTranslation = mMiwokTranslation;
    }
    public void setImageResourceID(int imageResourceID) {
    this.imageResourceID = imageResourceID;
    }
    /**
    * Return whether or not there is an image for this word!
    * @return
    */
    public boolean hasImage(){
    return imageResourceID != -1;
    }
    }

  • WordAdapter.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
    package com.example.android.miwok;
    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.ImageView;
    import android.widget.LinearLayout;
    import android.widget.TextView;
    import java.util.List;
    /**
    * Created by hclmaster on 2017/1/20.
    */
    public class WordAdapter extends ArrayAdapter<Word> {
    private int colorResourceID;
    // 虽然这里创建的默认构造方法是有三个参数的
    // 但是你可以把中间那个resource删掉,因为我们这里用不到!
    public WordAdapter(Context context, List<Word> objects, int colorResourceID) {
    super(context, 0, objects);
    this.colorResourceID = colorResourceID;
    }
    // convertView:是需要被填充的回收视图!
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    // Get the data item for this position
    Word word = getItem(position);
    // 检查现有视图是否为空,否则扩充视图!
    // 一般来说,一开始打开Activity的时候,一般都是没有可以重用的图的!
    if (convertView == null) {
    // 如果为null的话,那么我们就从list_item中隐式的展开视图
    // 其实就是将XML布局文件变成实际的视图对象
    convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
    }
    TextView tvMiwok = (TextView) convertView.findViewById(R.id.miwok_text_view);
    TextView tvDefault = (TextView) convertView.findViewById(R.id.default_text_view);
    ImageView imageView = (ImageView) convertView.findViewById(R.id.image);
    tvMiwok.setText(word.getmMiwokTranslation());
    tvDefault.setText(word.getmDefaultTranslation());
    // 这里的作用是用来判断这个word是不是有image被分配的
    // 这样就方便了我们将PhrasesActivity和其他Activity分开来!
    if(word.hasImage() != true) {
    imageView.setVisibility(View.GONE);
    } else{
    imageView.setVisibility(View.VISIBLE);
    imageView.setImageResource(word.getImageResourceID());
    }
    LinearLayout linearLayout = (LinearLayout)convertView.findViewById(R.id.list_item_linearlayout2);
    linearLayout.setBackgroundResource(colorResourceID);
    return convertView;
    }
    }

At last, 这样子我们就初步完成了能够播放音乐的app,但是其中有一个问题就是它播放音乐是异步的(也就是一个应用不需要等待另一个应用的回复),接下来我们要逐步的将它修改成为同步的!

热评文章