java - 我无法从项目资源中播放声音文件
问题描述
构建项目失败我收到错误我使用资源中的声音文件但我收到以下错误
F:\Android Studio Projects\korean\app\src\main\res\raw~BROMIUM
我无法理解我的代码的原因是:
package com.example.android.miwok;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import java.util.ArrayList;
public class FamilyActivity extends AppCompatActivity {
/**
* Handles playback of all the sound files
*/
private MediaPlayer mMediaPlayer;
/**
* Handles audio focus when playing a sound file
*/
private AudioManager mAudioManager;
/**
* This listener gets triggered when the {@link MediaPlayer} has completed
* playing the audio file.
*/
private MediaPlayer.OnCompletionListener mCompletionListener = new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
// Now that the sound file has finished playing, release the media player resources.
releaseMediaPlayer();
}
};
/**
* This listener gets triggered whenever the audio focus changes
* (i.e., we gain or lose audio focus because of another app or device).
*/
private AudioManager.OnAudioFocusChangeListener mOnAudioFocusChangeListener = new AudioManager.OnAudioFocusChangeListener() {
@Override
public void onAudioFocusChange(int focusChange) {
if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT ||
focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) {
// The AUDIOFOCUS_LOSS_TRANSIENT case means that we've lost audio focus for a
// short amount of time. The AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK case means that
// our app is allowed to continue playing sound but at a lower volume. We'll treat
// both cases the same way because our app is playing short sound files.
// Pause playback and reset player to the start of the file. That way, we can
// play the word from the beginning when we resume playback.
mMediaPlayer.pause();
mMediaPlayer.seekTo(0);
} else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
// The AUDIOFOCUS_GAIN case means we have regained focus and can resume playback.
mMediaPlayer.start();
} else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {
// The AUDIOFOCUS_LOSS case means we've lost audio focus and
// Stop playback and clean up resources
releaseMediaPlayer();
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.word_list);
// Create and setup the {@link AudioManager} to request audio focus
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
// Create a list of words
final ArrayList<Word> words = new ArrayList<Word>();
words.add(new Word(R.string.family_father, R.string.korean_family_father,
R.drawable.family_father, R.raw.father));
words.add(new Word(R.string.family_mother, R.string.korean_family_mother,
R.drawable.family_mother, R.raw.family_mother));
words.add(new Word(R.string.family_son, R.string.korean_family_son,
R.drawable.family_son, R.raw.family_son));
words.add(new Word(R.string.family_daughter, R.string.korean_family_daughter,
R.drawable.family_daughter, R.raw.family_daughter));
words.add(new Word(R.string.family_older_brother_boy, R.string.korean_family_older_brother_boy,
R.drawable.family_older_brother, R.raw.family_older_brother));
words.add(new Word(R.string.family_older_brother_girl, R.string.korean_family_older_brother_girl,
R.drawable.family_older_brother, R.raw.family_older_brother));
words.add(new Word(R.string.family_younger_brother, R.string.korean_family_younger_brother,
R.drawable.family_younger_brother, R.raw.family_younger_brother));
words.add(new Word(R.string.family_older_sister_boy, R.string.korean_family_older_sister_boy,
R.drawable.family_older_sister, R.raw.family_older_sister));
words.add(new Word(R.string.family_older_sister_girl, R.string.korean_family_older_sister_girl,
R.drawable.family_older_sister, R.raw.family_older_sister));
words.add(new Word(R.string.family_younger_sister, R.string.korean_family_younger_sister,
R.drawable.family_younger_sister, R.raw.family_younger_sister));
words.add(new Word(R.string.family_grandmother, R.string.korean_family_grandmother,
R.drawable.family_grandmother, R.raw.family_grandmother));
words.add(new Word(R.string.family_grandfather, R.string.korean_family_grandfather,
R.drawable.family_grandfather, R.raw.family_grandfather));
words.add(new Word(R.string.family_uncle, R.string.korean_family_grandfather,
R.drawable.family_grandfather, R.raw.family_grandfather));
words.add(new Word(R.string.family_aunt, R.string.korean_family_grandfather,
R.drawable.family_grandfather, R.raw.family_grandfather));
// Create an {@link WordAdapter}, whose data source is a list of {@link Word}s. The
// adapter knows how to create list items for each item in the list.
WordAdapter adapter = new WordAdapter(this, words, R.color.category_family);
// Find the {@link ListView} object in the view hierarchy of the {@link Activity}.
// There should be a {@link ListView} with the view ID called list, which is declared in the
// word_list.xml layout file.
ListView listView = (ListView) findViewById(R.id.list);
// Make the {@link ListView} use the {@link WordAdapter} we created above, so that the
// {@link ListView} will display list items for each {@link Word} in the list.
listView.setAdapter(adapter);
// Set a click listener to play the audio when the list item is clicked on
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
// Release the media player if it currently exists because we are about to
// play a different sound file
releaseMediaPlayer();
// Get the {@link Word} object at the given position the user clicked on
Word word = words.get(position);
// Request audio focus so in order to play the audio file. The app needs to play a
// short audio file, so we will request audio focus with a short amount of time
// with AUDIOFOCUS_GAIN_TRANSIENT.
int result = mAudioManager.requestAudioFocus(mOnAudioFocusChangeListener,
AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
// We have audio focus now.
// Create and setup the {@link MediaPlayer} for the audio resource associated
// with the current word
mMediaPlayer = MediaPlayer.create(FamilyActivity.this, word.getAudioResourceId());
// Start the audio file
mMediaPlayer.start();
// Setup a listener on the media player, so that we can stop and release the
// media player once the sound has finished playing.
mMediaPlayer.setOnCompletionListener(mCompletionListener);
}
}
});
}
@Override
protected void onStop() {
super.onStop();
// When the activity is stopped, release the media player resources because we won't
// be playing any more sounds.
releaseMediaPlayer();
}
/**
* Clean up the media player by releasing its resources.
*/
private void releaseMediaPlayer() {
// If the media player is not null, then it may be currently playing a sound.
if (mMediaPlayer != null) {
// Regardless of the current state of the media player, release its resources
// because we no longer need it.
mMediaPlayer.release();
// Set the media player back to null. For our code, we've decided that
// setting the media player to null is an easy way to tell that the media player
// is not configured to play an audio file at the moment.
mMediaPlayer = null;
// Regardless of whether or not we were granted audio focus, abandon it. This also
// unregisters the AudioFocusChangeListener so we don't get anymore callbacks.
mAudioManager.abandonAudioFocus(mOnAudioFocusChangeListener);
}
}
}
解决方案
推荐阅读
- oauth - 身份验证服务是否能够提供重置密码和电子邮件验证以及存储用户凭据
- javascript - Tooltipster 未在初始悬停时显示
- python - 使用颜色/样式格式时奇怪的终端输出
- python-3.x - Python如何将一维列表打印为二维
- reactjs - 电子反应逻辑
- ios - 任何 Scroll 事件(例如 onScroll)都不适用于 FlatList 组件
- html - 滑块句柄或两个并排 html 范围之间的颜色
- c# - 从 SQL Server 加载对象,但对 c# 类使用不同的对象属性
- cron - Ansible 是否支持评论 cron 作业
- constructor - 使用类构造函数编辑外部变量