由于我是Android编程的新手,因此我正在开发YouTube教程系列中的笔记记录应用程序。该系列本身不涉及添加音频,但是我想这样做,所以每当我单击音符( View 组)时都想要播放声音。我的原始文件夹(R.raw.menu_ok)中有声音。但是,老实说,我不确定如何实现一个基本的媒体播放器,只要按下列表 View 中的某个项目,该播放器就只会播放一种声音。我已经将媒体播放器插入到onListItemClick()中,但是我不知道如何获取所需的上下文,以免出现“无法解析创建方法”错误。如果除了仅实现媒体播放器以外,此问题还有很多其他内容,我是否可以获得我应该在哪个位置开始阅读此主题的链接?如果要完成这项工作,需要添加太多内容,那么我将删除该主题以防止搜索混乱。
同样,我只是想从其他问题中得到一个答案,但是它们似乎与如何播放列表 View 本身中列出的音频有关。当按下多个列表 View 项目时,我只需要一个声音即可播放它们。该应用程序本身的目的是在按初始列表中的任何 View 时为笔记 View 充气,但是我希望在按初始列表中的任何 View 时播放声音。
package anubitek.missionlog;
import android.app.ListFragment;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
// ================================================
// MainActivityListFragment:
// ================================================
public class MainActivityListFragment extends ListFragment
{
private ArrayList<Note> notes;
private NoteAdapter noteAdapter;
@Override
// Tells the fragment that its activity has completed its own Activity.onCreate().
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
/*String[] values = new String[]{ "Unreal", "Doom", "Quake", "Descent "};
// getActivity(): The Array Adapter's usage context.
// android.R.layout.simple_list_item_1: Which layout file has a text file view.
// Values: The array being used in this context.
// setListAdapter(adapter): Tells list fragment (in this context) to use this adapter to display the list of items.
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);*/
// Create array of notes
notes = new ArrayList<Note>();
// Add a new note to "notes", or ArrayList<Note> to first position
notes.add(new Note("New Note", "Note Body", Note.Category.NOTE));
// Set up the array adapter
noteAdapter = new NoteAdapter(getActivity(), notes);
// Provide the cursor for the list view.
setListAdapter(noteAdapter);
getListView().setDivider(ContextCompat.getDrawable(getActivity(), android.R.color.black));
getListView().setDividerHeight(1);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
MediaPlayer mPlayer = MediaPlayer.create(.this, R.raw.menu_ok);
mPlayer.start();
launchNoteDetailActivity(position);
}
// When you click on a list item, you want to grab all information for the position
// so we can pass it to our note detail activity.
private void launchNoteDetailActivity(int position)
{
// Grab note information associated with whatever note item we clicked on
Note note = (Note) getListAdapter().getItem(position);
// Create a new intent that launches our noteDetailActivity
Intent intent = new Intent(getActivity(), NoteDetailActivity.class);
// Pass along the information of the note we clicked on to our noteDetailActivity
intent.putExtra(MainActivity.NOTE_ID_EXTRA, note.getId());
intent.putExtra(MainActivity.NOTE_TITLE_EXTRA, note.getTitle());
intent.putExtra(MainActivity.NOTE_MESSAGE_EXTRA, note.getMessage());
intent.putExtra(MainActivity.NOTE_CATEGORY_EXTRA, note.getCategory());
startActivity(intent);
}
}
最佳答案
要获取 Activity 的上下文,请改用它
MediaPlayer mPlayer = MediaPlayer.create(getActivity().getBaseContext(), R.raw.menu_ok);
getActivity().getApplicationContext()
是应用程序上下文,将一直保留到整个应用程序生命周期未完成为止。这可能会导致内存泄漏问题。也,
private MediaPlayer mPlayer;
MediaPlayer
或onCreate
中的onCreateView
对象,然后进行准备调用。@Override
public View onCreateView(LayoutInflater inflater,最终的ViewGroup容器,
bundle 保存的InstanceState){
...
mPlayer = ... // init
mPlayer.prepare();
....
}
onListItemClick
mPlayer.seekTo(0);
mPlayer.play();
onPause
清理mPlayer.release();
这样,每次用户单击列表项时,您都不会在创建新的MediaPlayer对象时增加很多开销。您的代码不错,可以正常工作,但是您可以通过这种方式提高效率。看起来可能有很多事情要做,但是请相信我,这是值得付出的努力,以后可以避免很多问题。从经验上讲:P
编辑:更正了第3步中的错误
关于android - ListView:实现MediaPlayer用于列表项(非音乐),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38193364/