Android: 音频正在播放,但我听不到
Android: Audio is Playing but I am unable to hear
我正在上 Udacity 的课程并开发一个应用程序。
问题 - 音频正在播放,但我听不见??
当我在 WordAdapter.java 文件中记录信息时,我才知道正在播放音频
下面提到的文件-
NumbersActivity.java
package com.example.miwok;
{VARIOUS IMPORT SATEMENTS}
public class NumbersActivity extends AppCompatActivity {
private MediaPlayer audio;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.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", "ottiko", R.drawable.number_two, R.raw.number_two));
words.add(new Word("Three", "tolookosu", R.drawable.number_three, R.raw.number_three));
WordAdapter itemsAdapter = new WordAdapter(this, words, R.color.category_numbers);
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(itemsAdapter);
}
}
Word.java (Class)
package com.example.miwok;
public class Word {
private String mEnglishWord;
private String mMiwokWord;
private int mImageResourceId = NO_IMAGE_PROVIDED;
private int mAudioResourceId;
private static final int NO_IMAGE_PROVIDED = -1;
public Word(String englishTranslation, String miwokTranslation, int imageResourceId, int audioResourceId) {
mEnglishWord = englishTranslation;
mMiwokWord = miwokTranslation;
mImageResourceId = imageResourceId;
mAudioResourceId = audioResourceId;
}
public Word(String englishTranslation, String miwokTranslation, int audioResourceId) {
mEnglishWord = englishTranslation;
mMiwokWord = miwokTranslation;
mAudioResourceId = audioResourceId;
}
public String getEnglishWord() {
return mEnglishWord;
}
public String getMiwokWord() {
return mMiwokWord;
}
public int getImageResourceId() {
return mImageResourceId;
}
public int getAudioResourceId(){ return mAudioResourceId; }
public boolean hasImage(){
return mImageResourceId != NO_IMAGE_PROVIDED;
}
}
WordAdapter.java (Class)
package com.example.miwok;
{VARIOUS IMPORT SATEMENTS}
public class WordAdapter extends ArrayAdapter {
private int mBackgroundColour;
private MediaPlayer audio;
public WordAdapter(Activity context, ArrayList<Word> words, int colour){
super(context, 0, words);
mBackgroundColour = colour;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View listItemView = convertView;
if(listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
Word currentWord = (Word) getItem(position);
LinearLayout word_item = (LinearLayout) listItemView.findViewById(R.id.word_item);
TextView miwokTextView = (TextView) listItemView.findViewById(R.id.miwok_word);
TextView englishTextView = (TextView) listItemView.findViewById(R.id.english_word);
ImageView wordImageView = (ImageView) listItemView.findViewById(R.id.word_image);
if (currentWord.hasImage()){
wordImageView.setImageResource(currentWord.getImageResourceId());
wordImageView.setVisibility(View.VISIBLE);
}else{
wordImageView.setVisibility(View.GONE);
}
miwokTextView.setText(currentWord.getMiwokWord());
englishTextView.setText(currentWord.getEnglishWord());
View text_container = listItemView.findViewById(R.id.word_container);
int color = ContextCompat.getColor(getContext(), mBackgroundColour);
text_container.setBackgroundColor(color);
word_item.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getContext(), "Hi", Toast.LENGTH_LONG).show();
audio = MediaPlayer.create(getContext(), currentWord.getAudioResourceId());
audio.start();
Log.i("isplaying", String.valueOf(audio.isPlaying()));
}
});
return listItemView;
}
}
list_item.xml -
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="88dp"
android:id="@+id/word_item">
<ImageView
android:id="@+id/word_image"
android:layout_width="88dp"
android:layout_height="88dp"
android:background="@color/tan_background"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/word_container"
android:paddingLeft="16dp">
<TextView
android:id="@+id/miwok_word"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="18sp"
android:layout_weight="1"
android:gravity="bottom"
android:textColor="@color/white"
tools:text="miwok" />
<TextView
android:id="@+id/english_word"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_weight="1"
android:gravity="top"
android:textColor="@color/white"
tools:text="english"/>
</LinearLayout>
</LinearLayout>
再次 - 音频正在播放,但我听不到。
尝试将 WordAdapter.java (Class) audio = MediaPlayer.create(getContext(), currentWord.getAudioResourceId());
更改为 audio = MediaPlayer.create(getContext().getApplicationContext(), currentWord.getAudioResourceId());
我正在上 Udacity 的课程并开发一个应用程序。
问题 - 音频正在播放,但我听不见??
当我在 WordAdapter.java 文件中记录信息时,我才知道正在播放音频
下面提到的文件-
NumbersActivity.java
package com.example.miwok;
{VARIOUS IMPORT SATEMENTS}
public class NumbersActivity extends AppCompatActivity {
private MediaPlayer audio;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.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", "ottiko", R.drawable.number_two, R.raw.number_two));
words.add(new Word("Three", "tolookosu", R.drawable.number_three, R.raw.number_three));
WordAdapter itemsAdapter = new WordAdapter(this, words, R.color.category_numbers);
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(itemsAdapter);
}
}
Word.java (Class)
package com.example.miwok;
public class Word {
private String mEnglishWord;
private String mMiwokWord;
private int mImageResourceId = NO_IMAGE_PROVIDED;
private int mAudioResourceId;
private static final int NO_IMAGE_PROVIDED = -1;
public Word(String englishTranslation, String miwokTranslation, int imageResourceId, int audioResourceId) {
mEnglishWord = englishTranslation;
mMiwokWord = miwokTranslation;
mImageResourceId = imageResourceId;
mAudioResourceId = audioResourceId;
}
public Word(String englishTranslation, String miwokTranslation, int audioResourceId) {
mEnglishWord = englishTranslation;
mMiwokWord = miwokTranslation;
mAudioResourceId = audioResourceId;
}
public String getEnglishWord() {
return mEnglishWord;
}
public String getMiwokWord() {
return mMiwokWord;
}
public int getImageResourceId() {
return mImageResourceId;
}
public int getAudioResourceId(){ return mAudioResourceId; }
public boolean hasImage(){
return mImageResourceId != NO_IMAGE_PROVIDED;
}
}
WordAdapter.java (Class)
package com.example.miwok;
{VARIOUS IMPORT SATEMENTS}
public class WordAdapter extends ArrayAdapter {
private int mBackgroundColour;
private MediaPlayer audio;
public WordAdapter(Activity context, ArrayList<Word> words, int colour){
super(context, 0, words);
mBackgroundColour = colour;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View listItemView = convertView;
if(listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
Word currentWord = (Word) getItem(position);
LinearLayout word_item = (LinearLayout) listItemView.findViewById(R.id.word_item);
TextView miwokTextView = (TextView) listItemView.findViewById(R.id.miwok_word);
TextView englishTextView = (TextView) listItemView.findViewById(R.id.english_word);
ImageView wordImageView = (ImageView) listItemView.findViewById(R.id.word_image);
if (currentWord.hasImage()){
wordImageView.setImageResource(currentWord.getImageResourceId());
wordImageView.setVisibility(View.VISIBLE);
}else{
wordImageView.setVisibility(View.GONE);
}
miwokTextView.setText(currentWord.getMiwokWord());
englishTextView.setText(currentWord.getEnglishWord());
View text_container = listItemView.findViewById(R.id.word_container);
int color = ContextCompat.getColor(getContext(), mBackgroundColour);
text_container.setBackgroundColor(color);
word_item.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getContext(), "Hi", Toast.LENGTH_LONG).show();
audio = MediaPlayer.create(getContext(), currentWord.getAudioResourceId());
audio.start();
Log.i("isplaying", String.valueOf(audio.isPlaying()));
}
});
return listItemView;
}
}
list_item.xml -
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="88dp"
android:id="@+id/word_item">
<ImageView
android:id="@+id/word_image"
android:layout_width="88dp"
android:layout_height="88dp"
android:background="@color/tan_background"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/word_container"
android:paddingLeft="16dp">
<TextView
android:id="@+id/miwok_word"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="18sp"
android:layout_weight="1"
android:gravity="bottom"
android:textColor="@color/white"
tools:text="miwok" />
<TextView
android:id="@+id/english_word"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_weight="1"
android:gravity="top"
android:textColor="@color/white"
tools:text="english"/>
</LinearLayout>
</LinearLayout>
再次 - 音频正在播放,但我听不到。
尝试将 WordAdapter.java (Class) audio = MediaPlayer.create(getContext(), currentWord.getAudioResourceId());
更改为 audio = MediaPlayer.create(getContext().getApplicationContext(), currentWord.getAudioResourceId());