私はAndroidプログラミングの新機能であるため、YouTubeのチュートリアルシリーズからノートを取っています。シリーズ自体にはオーディオの追加は含まれていませんが、ノート(ビューグループ)をクリックするたびにサウンドを再生したいと思っています。私は生のフォルダ(R.raw.menu_ok)にサウンドを持っています。しかし、私は正直なところ、リストビューのアイテムが押されるたびにただ1つのサウンドを再生する基本的なメディアプレーヤーを実装する方法がわかりません。私はonListItemClick()にメディアプレーヤーを挿入しましたが、 "作成できないメソッドを解決できません"というエラーを受け取らないために必要なコンテキストを取得する方法がわかりません。メディアプレイヤーを実装するだけでなく、この問題がさらに深刻になった場合は、このトピックについて読んで始めたいリンクを得ることができますか?この作業を行うために余分に追加する必要がある場合は、検索の混乱を避けるためにトピックを削除します。ListView:リストアイテム用のMediaPlayerを実装する(音楽以外)
また、私は他の質問からの回答のうちの1つに行ってしまったでしょうが、リストビュー自体に表示されているオーディオを作成する方法についてのようです。複数のリストビューアイテムが押されたときに再生するには、単一のサウンドが必要です。アプリケーション自体の目的は、最初のリストのビューのいずれかが押されたときにノートビューを膨張させることですが、最初のリストのビューのいずれかが押されたときにサウンドが再生されるようにします。
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);
}
}
ステップ3でエラーを訂正:。 'MediaPlayerのMPlayerに= MediaPlayer.create(getActivityを()getApplicationContext()、R.raw。menu_ok); ' しかし、私は他のトピックで「準備」()について何かを見ています。 "準備する"ものは何のために使うのですか? – LannFyre
[メディアデータを取得またはデコードする](https://developer.android.com/guide/topics/media/mediaplayer.html)するときは、この準備をする必要があります。あなたのサウンドは生のフォーマットであり、あなたのアプリでは準備をする必要はありません。 –